4 Replies Latest reply on Nov 26, 2008 12:56 PM by John Harby

    Problems with JPA/Hibernate persist

    John Harby Newbie

      I am having problems with EntityManager.persist - I am not getting any errors but on this insert I'm trying below nothing is going in the database (MS SQLServer). This is running in JBoss from an SLSB so I am using the CMT. In some prior methods I am successfully populating some objects from the DB using find so I know the connection is ok. Has anyone else run into this.

      private int insertLockRecord(PolicyFundingInfo policyFundingInfo) {
       EntityManagerFactory emf = Persistence.createEntityManagerFactory("PolicyPersistenceUnit");
       EntityManager em = emf.createEntityManager();
       RecordlockEntity recordlockEntity = new RecordlockEntity();
       recordlockEntity.setUsername("PolicyServ");
       recordlockEntity.setLockdate(new Timestamp(new Date().getTime()));
       recordlockEntity.setRecordkey(Integer.toString(policyFundingInfo.getCertmainseqno()));
       recordlockEntity.setRecordtype("POL");
       try {
       em.persist(recordlockEntity);
       }
       catch (Throwable e) {
       e.printStackTrace();
       }
       Integer recordLockKey = new Integer(0);
       em.close();
       return recordLockKey.intValue();
       }
      


        • 1. Re: Problems with JPA/Hibernate persist
          John Harby Newbie

          P.S. I'm using Intellij's ORM generator to create the entity - here is the generated code for that:

          package com.csatp.model;
          
          import javax.persistence.*;
          import java.sql.Timestamp;
          
          @Entity
          @Table(catalog = "WeekOld", schema = "dbo", name = "RECORDLOCK")
          public class RecordlockEntity {
           private int recordlockseqno;
          
           @Id
           @GeneratedValue(strategy=GenerationType.AUTO)
           @Column(name = "recordlockseqno", nullable = false, length = 10)
           public int getRecordlockseqno() {
           return recordlockseqno;
           }
          
           public void setRecordlockseqno(int recordlockseqno) {
           this.recordlockseqno = recordlockseqno;
           }
          
           private String username;
          
           @Basic
           @Column(name = "username", nullable = false, length = 10)
           public String getUsername() {
           return username;
           }
          
           public void setUsername(String username) {
           this.username = username;
           }
          
           private Timestamp lockdate;
          
           @Basic
           @Column(name = "lockdate", nullable = false, length = 23, precision = 3)
           public Timestamp getLockdate() {
           return lockdate;
           }
          
           public void setLockdate(Timestamp lockdate) {
           this.lockdate = lockdate;
           }
          
           private Timestamp expirationdate;
          
           @Basic
           @Column(name = "expirationdate", length = 23, precision = 3)
           public Timestamp getExpirationdate() {
           return expirationdate;
           }
          
           public void setExpirationdate(Timestamp expirationdate) {
           this.expirationdate = expirationdate;
           }
          
           private String recordkey;
          
           @Basic
           @Column(name = "recordkey", nullable = false, length = 30)
           public String getRecordkey() {
           return recordkey;
           }
          
           public void setRecordkey(String recordkey) {
           this.recordkey = recordkey;
           }
          
           private String recordtype;
          
           @Basic
           @Column(name = "recordtype", nullable = false, length = 10)
           public String getRecordtype() {
           return recordtype;
           }
          
           public void setRecordtype(String recordtype) {
           this.recordtype = recordtype;
           }
          
           public boolean equals(Object o) {
           if (this == o) return true;
           if (o == null || getClass() != o.getClass()) return false;
          
           RecordlockEntity that = (RecordlockEntity) o;
          
           if (recordlockseqno != that.recordlockseqno) return false;
           if (expirationdate != null ? !expirationdate.equals(that.expirationdate) : that.expirationdate != null)
           return false;
           if (lockdate != null ? !lockdate.equals(that.lockdate) : that.lockdate != null) return false;
           if (recordkey != null ? !recordkey.equals(that.recordkey) : that.recordkey != null) return false;
           if (recordtype != null ? !recordtype.equals(that.recordtype) : that.recordtype != null) return false;
           if (username != null ? !username.equals(that.username) : that.username != null) return false;
          
           return true;
           }
          
           public int hashCode() {
           int result;
           result = recordlockseqno;
           result = 31 * result + (username != null ? username.hashCode() : 0);
           result = 31 * result + (lockdate != null ? lockdate.hashCode() : 0);
           result = 31 * result + (expirationdate != null ? expirationdate.hashCode() : 0);
           result = 31 * result + (recordkey != null ? recordkey.hashCode() : 0);
           result = 31 * result + (recordtype != null ? recordtype.hashCode() : 0);
           return result;
           }
          }
          
          


          • 2. Re: Problems with JPA/Hibernate persist
            John Harby Newbie

            P.S.^2 - I found a way to get this to work by specifying the transaction attribute on the bean method to be NEVER and using the EntityManager Transactions. But is there a better way to do this? It seems like a hack as is.

            • 3. Re: Problems with JPA/Hibernate persist
              Dmitry Geraskov Newbie

              Seems like your transaction never commited. Try to set for transaction autocommit to true. See documentation how to do this.

              • 4. Re: Problems with JPA/Hibernate persist
                John Harby Newbie

                Ok, thanks. It seems like relying on the container for Txns is not working possibly because I'm not using an entity bean but just an SLSB.

                "dgeraskov" wrote:
                Seems like your transaction never commited. Try to set for transaction autocommit to true. See documentation how to do this.