4 Replies Latest reply: Mar 28, 2013 9:52 AM by cdelahun RSS

    @GenericGenerator with increment throwing EntityExistsException

    800839
      Hi,


      javax.persistence.EntityExistsException: a different object with the same identifier value was already associated with the session: [com.xxx.ips.ow.domain.Owtitle#com.xxx.ips.ow.domain.Owtitle@cb0cda57]

      Configuration in embeddable class:-

      private long owpermid;
       @GenericGenerator(name="generator", strategy = "increment")
             @GeneratedValue(generator="generator")
             //@Generated(value = "increment")
             private long owtitleseq; 
      Here when i insert the records in the respective table the Sequence is stored as zero and when i insert the second record it is throwing an error saying

      javax.persistence.EntityExistsException: a different object with the same identifier value was already associated with the session:

      I am not sure where I am going wronh with this? Please clarify.

      Thanks.
        • 1. Re: @GenericGenerator with increment throwing EntityExistsException
          cdelahun
          You will need to post the full error stack trace to be sure, but the error you are getting I believe occurs before the entity gets assigned an ID. This means the problem is not likely with sequencing, but with how you are persisting your entity - you might be calling persist or having persist cascade to a detached entity.

          If this doesn't help, please show the exception stack and the code you are using to get the exception.
          • 2. Re: @GenericGenerator with increment throwing EntityExistsException
            800839
            Thanks cdelahun , here is the code snippet:-
            @Entity
            @NamedQuery(name="titleInfo", 
            query="SELECT ow from Owtitle ow  left join fetch ow.originalwork where ow.originalwork.owpermid =?1")
            public class Owtitle implements Serializable {
                 private static final long serialVersionUID = 1L;
            
                 @EmbeddedId
                 private OwtitlePK id;
                 
                 @Column(name="CREATED_BY")
                 private String createdBy;
            
                 @Column(name="CREATED_ON")
                 @Temporal(TemporalType.TIMESTAMP)
                 private Date createdOn;
            
                 private String owtitle;
            
                 @Column(name="UPDATED_BY")
                 private String updatedBy;
            
                 @Column(name="UPDATED_ON")
                 @Temporal(TemporalType.TIMESTAMP)
                 private Date updatedOn;
            
                 //bi-directional many-to-one association to Originalwork
                 @ManyToOne(fetch=FetchType.LAZY)
                 @JoinColumn(name="OWPERMID")
                 @MapsId("owpermid")
                 private Originalwork originalwork;
            
                 //bi-directional many-to-one association to Owlanguagelookup
                 @ManyToOne(fetch=FetchType.LAZY)
                 @JoinColumn(name="LANGUAGEID")
                 private Owlanguagelookup owlanguagelookup;
            
                 //bi-directional many-to-one association to Owtitletypelookup
                 @ManyToOne(fetch=FetchType.LAZY)
                 @JoinColumn(name="OWTITLETYPEID")
                 private Owtitletypelookup owtitletypelookup;
            
                public Owtitle() {
                }
                
                 public OwtitlePK getId() {
                      return id;
                 }
            
                 public void setId(OwtitlePK id) {
                      this.id = id;
                 }
            
            // Getters & Setters
            
            }
            @Embeddable
            /*@org.hibernate.annotations.GenericGenerator(
                    name = "hilo-generator",
                    strategy = "increment")*/
            public class OwtitlePK implements Serializable {
                 //default serial version id, required for serializable classes.
                 private static final long serialVersionUID = 1L;
            
                 private long owpermid;
            
                 @GenericGenerator(name="generator", strategy = "increment")
                 @GeneratedValue(generator = "generator")
                 private long owtitleseq;
            
                public OwtitlePK() {
                }
                 public long getOwpermid() {
                      return this.owpermid;
                 }
                 public void setOwpermid(long owpermid) {
                      this.owpermid = owpermid;
                 }
                 public long getOwtitleseq() {
                      return this.owtitleseq;
                 }
                 public void setOwtitleseq(long owtitleseq) {
                      this.owtitleseq = owtitleseq;
                 }
            
                 public boolean equals(Object other) {
                      if (this == other) {
                           return true;
                      }
                      if (!(other instanceof OwtitlePK)) {
                           return false;
                      }
                      OwtitlePK castOther = (OwtitlePK)other;
                      return 
                           (this.owpermid == castOther.owpermid)
                           && (this.owtitleseq == castOther.owtitleseq);
            
                }
                
                 public int hashCode() {
                      final int prime = 31;
                      int hash = 17;
                      hash = hash * prime + ((int) (this.owpermid ^ (this.owpermid >>> 32)));
                      hash = hash * prime + ((int) (this.owtitleseq ^ (this.owtitleseq >>> 32)));
                      
                      return hash;
                }
            }
            In the Java class :-
             Owtitle  owTitle = new Owtitle();
             owTitle.setOriginalwork(originalWork);
             owTitle.setCreatedBy("USER");
             owTitle.setCreatedOn(new Date());
             owTitle.setOwlanguagelookup(owLanguageLookup);
             owTitle.setOwtitletypelookup(owTitleTypeLookup);
             owTitle.setOwtitle(title);
             owTitle.setUpdatedBy("USER");
             owTitle.setUpdatedOn(new Date());
             OwtitlePK owTitlePK = new OwtitlePK();
             //owTitlePK.setOwtitleseq(2);
             owTitlePK.setOwpermid(originalWorkTitleMessage.getOriginalWorkPermId());
             owTitle.setId(owTitlePK);
             entityManager.persist(owTitle);
            Please let me know if you have suggestions.

            Thanks.
            • 3. Re: @GenericGenerator with increment throwing EntityExistsException
              800839
              Any update on this guys?
              • 4. Re: @GenericGenerator with increment throwing EntityExistsException
                cdelahun
                I assumed since this is a TopLink JPA forum you would be using EclipseLink or TopLink, but GenericGenerator isn't a JPA annotation. Try setting up JPA sequencing, which is described in detail here:
                http://en.wikibooks.org/wiki/Java_Persistence/Identity_and_Sequencing#Example_generated_id_annotation
                or here http://wiki.eclipse.org/EclipseLink/Examples/JPA/PrimaryKey