0

ReportDimensions.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;     
}

Here is my 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);    }

Here, I am trying to create a record if not exists else update the record. The constructor arguments for ReportDimensions don't have primary key as it is auto generated. According to the documentation for EntityManager.find(arg1,arg2), we must use primary key for 2nd argument. I did not retrieve auto generated ID at any point of time. What should I do for this whole thing to work.

Jop.pop
  • 335
  • 4
  • 18

2 Answers2

0

Make sure value for "report_type" column should not be null, at time of constructing an object in Java.

Arpit Aggarwal
  • 27,626
  • 16
  • 90
  • 108
0

I found a work around....

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

ReportDimensions dimensions = 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);

}

Jop.pop
  • 335
  • 4
  • 18