0

报告维度.java:

package com.test.main.domain.resource;

import java.io.Serializable; 
import javax.persistence.Column; 
import javax.persistence.Entity; 
import javax.persistence.GeneratedValue;
import javax.persistence.Id; 
import javax.persistence.Table; 
import javax.validation.constraints.NotNull;

@Entity
@Table(name = "report_dimensions") 
public class ReportDimensions implements Serializable {

    public ReportDimensions() {         
        super();    
    }

    public ReportDimensions(long userId, long reportId, String reportType) {
        super();        
        this.userId = userId;       
        this.reportType = reportType;       
    }


    public long getDimensionsId() {         
        return dimensionsId;    
    }

    public void setDimensionsId(long dimensionsId) {        
        this.dimensionsId = dimensionsId;   
    }

    public long getUserId() {       
        return userId;  
    }

    public void setUserId(long userId) {        
        this.userId = userId;   
    }


    public String getReportType() {         
        return reportType;  
    }

    public void setReportType(String reportType) {      
        this.reportType =reportType;    
    }

    public int getWinWidth() {      
        return winWidth;    
    }

    public void setWinWidth(int winWidth) {         
        this.winWidth = winWidth;
    }

    public int getWinHeight() { 
        return winHeight;   
    }

    public void setWinHeight(int winHeight) {       
        this.winHeight = winHeight;     
    }

    @Id     
    @Column(name = "report_dimensions_id")  
    @GeneratedValue     
    private long dimensionsId;      

    @Column(name = "user_id")   
    @NotNull    
    private long userId;

    @Column(name = "report_type")   
    @NotNull    
    private String reportType;  

    @Column(name = "window_width")  
    @NotNull    
    private int winWidth; 

    @Column(name = "window_height")     
    @NotNull    
    private int winHeight;

    private static final long serialVersionUID = 1L;     
}

这是我的 DAOImpl:

public void updateWindowSize(Long userId, String reportType, int width, int height)     {   
               ReportDimensions dimensions = entityManager.find(ReportDimensions.class, new   ReportDimensions(userId, reportType));

    if(dimensions == null) dimensions = new ReportDimensions(userId, reportType);       

 dimensions.setWinWidth(width);  dimensions.setWinHeight(height);

 entityManager.merge(dimensions);    }

在这里,如果不存在,我正在尝试创建记录,否则更新记录。ReportDimensions 的构造函数参数没有主键,因为它是自动生成的。根据 EntityManager.find(arg1,arg2) 的文档,我们必须将主键用于第二个参数。我在任何时候都没有检索到自动生成的 ID。我应该怎么做才能让整个事情发挥作用。

4

2 回答 2

0

在 Java 中构造对象时,请确保“report_type”列的值不应为空。

于 2015-05-01T18:14:02.223 回答
0

我找到了一个解决办法......

public void updateWindowSize(Long userId, String reportType, int width, int height) {

ReportDimensions 维度 = null;

Query query = entityManager.createQuery("SELECT dim FROM " + ReportDimensions.class.getName()  + " dim WHERE dim.userId=:userId AND dim.reportType=:reportType");

try {
    dimensions = (ReportDimensions)query.setParameter("userId", new Long(userId)).setParameter("reportType", reportType).getSingleResult();
} catch (NoResultException ex) {
    dimensions = new ReportDimensions(userId, reportType);
}
dimensions.setWinWidth(width);
dimensions.setWinHeight(height);

entityManager.merge(dimensions);

}

于 2015-05-01T21:23:13.170 回答