首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >EntityManager.merge()

EntityManager.merge()
EN

Stack Overflow用户
提问于 2015-05-02 02:12:24
回答 2查看 523关注 0票数 0

ReportDimensions.java:

代码语言:javascript
复制
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:

代码语言:javascript
复制
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。我该怎么做才能让这整件事起作用。

EN

回答 2

Stack Overflow用户

发布于 2015-05-02 02:14:02

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

票数 0
EN

Stack Overflow用户

发布于 2015-05-02 05:23:13

我在附近找到了一份工作...

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

ReportDimensions维度=空;

代码语言:javascript
复制
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);

}

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/29992526

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档