-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 9 posts ] 
Author Message
 Post subject: Strange Detached Entity Persist Problem
PostPosted: Mon Apr 28, 2008 7:13 am 
Beginner
Beginner

Joined: Fri May 21, 2004 5:22 am
Posts: 24
Note: The update and select operation is working.

Hibernate version: 3

Mapping documents:

Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated Apr 21, 2008 3:18:22 PM by Hibernate Tools 3.2.1.GA -->
<hibernate-mapping>
    <class name="com.xxx.uresdemo.bean.Result" table="result" catalog="uresdemo">
        <composite-id name="id" class="com.xxx.uresdemo.bean.ResultId">
            <key-property name="transactionId" type="string">
                <column name="transactionID" length="50" />
            </key-property>
            <key-property name="deviceId" type="string">
                <column name="deviceID" length="20" />
            </key-property>
            <key-property name="deviceParameterId" type="string">
                <column name="deviceParameterID" length="20" />
            </key-property>
        </composite-id>
        <timestamp name="timeStamp" column="timeStamp" />
        <property name="parameterName" type="string">
            <column name="parameterName" length="45" not-null="true">
                <comment>Parameter description</comment>
            </column>
        </property>
        <property name="parameterKey" type="string">
            <column name="parameterKey" length="20" not-null="true">
                <comment>Parameter key name</comment>
            </column>
        </property>
        <property name="lastUpdatedDate" type="timestamp">
            <column name="lastUpdatedDate" length="0" not-null="true">
                <comment>Last updated date time</comment>
            </column>
        </property>
        <property name="lastUpdatedBy" type="string">
            <column name="lastUpdatedBy" length="20" not-null="true">
                <comment>Last updated person</comment>
            </column>
        </property>
        <property name="status" type="string">
            <column name="status" length="2" not-null="true">
                <comment>Record status</comment>
            </column>
        </property>
        <property name="result" type="string">
            <column name="result" length="20" not-null="true">
                <comment>Result</comment>
            </column>
        </property>
    </class>
</hibernate-mapping>


Full stack trace of any exception that occurs:

Code:
org.hibernate.PersistentObjectException: detached entity passed to persist: com.xxx.uresdemo.bean.Result
   at org.hibernate.event.def.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:79)
   at org.hibernate.event.def.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:38)
   at org.hibernate.impl.SessionImpl.firePersist(SessionImpl.java:618)
   at org.hibernate.impl.SessionImpl.persist(SessionImpl.java:592)
   at org.hibernate.impl.SessionImpl.persist(SessionImpl.java:596)
   at com.xxx.uresdemo.home.EntityHome.writePersist(EntityHome.java:131)
   at com.xxx.uresdemo.home.ResultHome.persist(ResultHome.java:32)
   at com.xxx.uresdemo.logic.impl.URESDemoBusinessImpl.loginThenSubmitData(URESDemoBusinessImpl.java:81)
   at com.xxx.uresdemo.mobile.servlet.URESDemoUplink.doPost(URESDemoUplink.java:92)
   at javax.servlet.http.HttpServlet.service(HttpServlet.java:710)
   at javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
   at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
   at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
   at org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:96)
   at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
   at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
   at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:230)
   at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
   at org.jboss.web.tomcat.security.SecurityAssociationValve.invoke(SecurityAssociationValve.java:179)
   at org.jboss.web.tomcat.security.JaccContextValve.invoke(JaccContextValve.java:84)
   at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
   at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
   at org.jboss.web.tomcat.service.jca.CachedConnectionValve.invoke(CachedConnectionValve.java:157)
   at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
   at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:262)
   at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:844)
   at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:583)
   at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:446)
   at java.lang.Thread.run(Thread.java:619)


Name and version of the database you are using: MySQL 5

The generated SQL (show_sql=true): None

Debug level Hibernate log excerpt: None

These are the codes being used, started witih business logic, then Home class and EntityHome class:

Code:
public void loginThenSubmitData(DataUplink dataUplink) throws LoginException, BusinessException {
      log.debug("URESDemoBusinessImpl.loginThenSubmitData");
      LoginManager.getInstance().loginAttempt(dataUplink.getUserId(), dataUplink.getPassword());
      ResultHome resultHome = new ResultHome();
      ResultId resultId = new ResultId();
      resultId.setDeviceId(dataUplink.getDeviceId());
      resultId.setDeviceParameterId(dataUplink.getDeviceParameterId());
      Result result = new Result();
      result.setId(resultId);
      result.setTimeStamp(dataUplink.getDateTime());
      result.setParameterKey(dataUplink.getParameterKey());
      result.setParameterName(dataUplink.getParameterName());
      result.setResult(dataUplink.getResult());
      result.setLastUpdatedBy(dataUplink.getUserId());
      result.setLastUpdatedDate(GregorianCalendar.getInstance().getTime());
      StatusHome statusHome = new StatusHome();
      result.setStatus(statusHome.findById("A").getStatusDescription());
      resultHome.attachDirty(result);
   }


Code:
public void persist(Result transientInstance) {
      log.debug("persisting Result instance");
      try {
         transientInstance.getId().setTransactionId(getNextHiloSequenceInString(EntityHome.HILOSEQ_TRANSAQID));
         writePersist(transientInstance);
         log.debug("persist successful");
      } catch (RuntimeException re) {
         log.error("persist failed", re);
         throw re;
      }
   }


Code:
/**
    * Return instance of {@link Session}
    *
    * @return {@link Session}
    */
   protected Session getSessionInstance() {
      if (session == null || !session.isOpen()) {
         session = getSessionFactory().openSession();
      }
      return session;
   }

   /**
    * Return instance of {@link SessionFactory}
    *
    * @return {@link SessionFactory}
    */
   private SessionFactory getSessionFactory() {
      try {
         return (SessionFactory) new InitialContext()
               .lookup("SessionFactory");
      } catch (Exception e) {
         log.error("Could not locate SessionFactory in JNDI", e);
         throw new IllegalStateException(
               "Could not locate SessionFactory in JNDI");
      }
   }

/**
    * Overriding {@link org.hibernate.Session#persist(Object)} embed with
    * {@link org.hibernate.Session#beginTransaction()} and
    * {@link Transaction#commit()}
    *
    * @param transientInstance
    *            {@link Object}
    */
   protected void writePersist(Object transientInstance) {
      getSessionInstance().beginTransaction();
      getSessionInstance().persist(transientInstance);
      getSessionInstance().getTransaction().commit();
   }


/**
    * Return the next {@link Hilosequences#getHighvalues()}
    *
    * @param sequenceName
    *            {@link EntityHome#HILOSEQ_TOPUP} or
    *            {@link EntityHome#HILOSEQ_REG}
    * @return {@link Integer#intValue()}
    */
   protected int getNextHiloSequence(String sequenceName) {
      int nextHighValue = 1;
      HilosequencesHome hilosequencesHome = new HilosequencesHome();
      Hilosequences hilosequences = hilosequencesHome.findById(keyMap
            .get(sequenceName));
      if (hilosequences != null) {
         nextHighValue = hilosequences.getHighvalues() + 1;
         hilosequences.setHighvalues(nextHighValue);
         hilosequencesHome.attachDirty(hilosequences);
      } else {
         hilosequences = new Hilosequences();
         hilosequences.setSequencename(keyMap.get(sequenceName));
         hilosequences.setHighvalues(nextHighValue);
         hilosequencesHome.persist(hilosequences);
      }

      return nextHighValue;
   }
   
   /**
    * String implementation of {@link EntityHome#getNextHiloSequence(String)} method
    * @param sequenceName {@link String}
    * @return {@link String}
    */
   protected String getNextHiloSequenceInString(String sequenceName) {
      return String.valueOf(getNextHiloSequence(sequenceName));
   }



What had I done wrong?


Top
 Profile  
 
 Post subject: questions
PostPosted: Tue Apr 29, 2008 6:13 pm 
Senior
Senior

Joined: Sun Jun 11, 2006 10:41 am
Posts: 164
Could you pls post the code of the Result class?


Top
 Profile  
 
 Post subject:
PostPosted: Tue Apr 29, 2008 9:54 pm 
Beginner
Beginner

Joined: Fri May 21, 2004 5:22 am
Posts: 24
Here is the code of Result class:

Code:
public class Result implements java.io.Serializable {

   private String transactionId;
   private Date timeStamp;
   private Status status;
   private Parameters parameters;
   private String deviceId;
   private String parameterName;
   private String parameterKey;
   private Date lastUpdatedDate;
   private String lastUpdatedBy;
   private String result;

   public Result() {
   }

   public Result(String transactionId, Status status, Parameters parameters, String deviceId, String parameterName, String parameterKey,
         Date lastUpdatedDate, String lastUpdatedBy) {
      this.transactionId = transactionId;
      this.status = status;
      this.parameters = parameters;
      this.deviceId = deviceId;
      this.parameterName = parameterName;
      this.parameterKey = parameterKey;
      this.lastUpdatedDate = lastUpdatedDate;
      this.lastUpdatedBy = lastUpdatedBy;
   }

   public Result(String transactionId, Status status, Parameters parameters, String deviceId, String parameterName, String parameterKey,
         Date lastUpdatedDate, String lastUpdatedBy, String result) {
      this.transactionId = transactionId;
      this.status = status;
      this.parameters = parameters;
      this.deviceId = deviceId;
      this.parameterName = parameterName;
      this.parameterKey = parameterKey;
      this.lastUpdatedDate = lastUpdatedDate;
      this.lastUpdatedBy = lastUpdatedBy;
      this.result = result;
   }
   
   /**
    * Return eye position in format of X,Y e.g. 999x999,999x999
    * @return {@link String}
    */
   public String getEyePosition() {
      return result.substring(0, 14);
   }
   
   /**
    * Return mouth position in format of X e.g. 999x999
    * @return {@link String}
    */
   public String getMouthPosition() {
      return result.substring(15, 21);
   }
   
   /**
    * Return image position and size in format of X x WIDTH x HEIGHT e.g. 999x999x999x999
    * @return {@link String}
    */
   public String getImageSize() {
      return result.substring(22, 36);
   }

   public String getTransactionId() {
      return this.transactionId;
   }

   public void setTransactionId(String transactionId) {
      this.transactionId = transactionId;
   }

   public Date getTimeStamp() {
      return this.timeStamp;
   }

   public void setTimeStamp(Date timeStamp) {
      this.timeStamp = timeStamp;
   }

   public Status getStatus() {
      return this.status;
   }

   public void setStatus(Status status) {
      this.status = status;
   }

   public Parameters getParameters() {
      return this.parameters;
   }

   public void setParameters(Parameters parameters) {
      this.parameters = parameters;
   }

   public String getDeviceId() {
      return this.deviceId;
   }

   public void setDeviceId(String deviceId) {
      this.deviceId = deviceId;
   }

   public String getParameterName() {
      return this.parameterName;
   }

   public void setParameterName(String parameterName) {
      this.parameterName = parameterName;
   }

   public String getParameterKey() {
      return this.parameterKey;
   }

   public void setParameterKey(String parameterKey) {
      this.parameterKey = parameterKey;
   }

   public Date getLastUpdatedDate() {
      return this.lastUpdatedDate;
   }

   public void setLastUpdatedDate(Date lastUpdatedDate) {
      this.lastUpdatedDate = lastUpdatedDate;
   }

   public String getLastUpdatedBy() {
      return this.lastUpdatedBy;
   }

   public void setLastUpdatedBy(String lastUpdatedBy) {
      this.lastUpdatedBy = lastUpdatedBy;
   }

   public String getResult() {
      return this.result;
   }

   public void setResult(String result) {
      this.result = result;
   }

}


And this is Parameters class:

Code:
public class Parameters implements java.io.Serializable {

   private String deviceParameterId;
   private Status status;
   private String deviceId;
   private String parameterName;
   private String parameterKey;
   private Date lastUpdatedDate;
   private String lastUpdatedBy;
   private Set<Result> results = new HashSet<Result>(0);

   public Parameters() {
   }

   public Parameters(String deviceParameterId, Status status, String deviceId, String parameterName, String parameterKey, Date lastUpdatedDate,
         String lastUpdatedBy) {
      this.deviceParameterId = deviceParameterId;
      this.status = status;
      this.deviceId = deviceId;
      this.parameterName = parameterName;
      this.parameterKey = parameterKey;
      this.lastUpdatedDate = lastUpdatedDate;
      this.lastUpdatedBy = lastUpdatedBy;
   }

   public Parameters(String deviceParameterId, Status status, String deviceId, String parameterName, String parameterKey, Date lastUpdatedDate,
         String lastUpdatedBy, Set<Result> results) {
      this.deviceParameterId = deviceParameterId;
      this.status = status;
      this.deviceId = deviceId;
      this.parameterName = parameterName;
      this.parameterKey = parameterKey;
      this.lastUpdatedDate = lastUpdatedDate;
      this.lastUpdatedBy = lastUpdatedBy;
      this.results = results;
   }

   public String getDeviceParameterId() {
      return this.deviceParameterId;
   }

   public void setDeviceParameterId(String deviceParameterId) {
      this.deviceParameterId = deviceParameterId;
   }

   public Status getStatus() {
      return this.status;
   }

   public void setStatus(Status status) {
      this.status = status;
   }

   public String getDeviceId() {
      return this.deviceId;
   }

   public void setDeviceId(String deviceId) {
      this.deviceId = deviceId;
   }

   public String getParameterName() {
      return this.parameterName;
   }

   public void setParameterName(String parameterName) {
      this.parameterName = parameterName;
   }

   public String getParameterKey() {
      return this.parameterKey;
   }

   public void setParameterKey(String parameterKey) {
      this.parameterKey = parameterKey;
   }

   public Date getLastUpdatedDate() {
      return this.lastUpdatedDate;
   }

   public void setLastUpdatedDate(Date lastUpdatedDate) {
      this.lastUpdatedDate = lastUpdatedDate;
   }

   public String getLastUpdatedBy() {
      return this.lastUpdatedBy;
   }

   public void setLastUpdatedBy(String lastUpdatedBy) {
      this.lastUpdatedBy = lastUpdatedBy;
   }

   public Set<Result> getResults() {
      return this.results;
   }

   public void setResults(Set<Result> results) {
      this.results = results;
   }

}


Top
 Profile  
 
 Post subject: more questions
PostPosted: Wed Apr 30, 2008 3:11 am 
Senior
Senior

Joined: Sun Jun 11, 2006 10:41 am
Posts: 164
Pls clarify:

1. how does the code that calls persist(Result transientInstance) look like, and what exactly is being passed to this method at runtime?

2. where does the persist(Result transientInstance) method reside? Inside an EJB? What about the code that calls it? Is it an app client? maybe it's also an EJB? pls describe the architecture of your beans.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Apr 30, 2008 4:22 am 
Beginner
Beginner

Joined: Fri May 21, 2004 5:22 am
Posts: 24
Its not EJB, just a simple bean:

Code:
public void loginThenSubmitData(DataUplink dataUplink) throws LoginException, BusinessException {
      log.debug("URESDemoBusinessImpl.loginThenSubmitData");
      LoginManager.getInstance().loginAttempt(dataUplink.getUserId(), dataUplink.getPassword());
      ResultHome resultHome = new ResultHome();
      ResultId resultId = new ResultId();
      resultId.setDeviceId(dataUplink.getDeviceId());
      resultId.setDeviceParameterId(dataUplink.getDeviceParameterId());
      Result result = new Result();
      result.setId(resultId);
      result.setTimeStamp(dataUplink.getDateTime());
      result.setParameterKey(dataUplink.getParameterKey());
      result.setParameterName(dataUplink.getParameterName());
      result.setResult(dataUplink.getResult());
      result.setLastUpdatedBy(dataUplink.getUserId());
      result.setLastUpdatedDate(GregorianCalendar.getInstance().getTime());
      StatusHome statusHome = new StatusHome();
      result.setStatus(statusHome.findById("A").getStatusDescription());
      resultHome.persist(result);
   }


Top
 Profile  
 
 Post subject: thoughts
PostPosted: Wed Apr 30, 2008 4:53 am 
Senior
Senior

Joined: Sun Jun 11, 2006 10:41 am
Posts: 164
1. what hibernate version are you using?

2. examine the following URL, see it it helps.
http://javanotepad.blogspot.com/2007/05/ive-found-bug-in-hibernate.html

3. did you try merge instead of persist?

4. did u try googling for the exception?


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 13, 2008 11:23 pm 
Beginner
Beginner

Joined: Fri May 21, 2004 5:22 am
Posts: 24
Sorry for the late reply, I tried merge and its end up with infinite loop that calls the merge() method with the following stack trace:

Quote:

2008-05-14 11:09:44,693 DEBUG [com.xyz.uresdemo.home.ResultHome] merging Result instance
2008-05-14 11:09:44,693 DEBUG [com.xyz.uresdemo.home.ResultHome] merging Result instance
2008-05-14 11:09:44,694 DEBUG [com.xyz.uresdemo.home.ResultHome] merging Result instance
2008-05-14 11:09:44,694 DEBUG [com.xyz.uresdemo.home.ResultHome] merging Result instance
2008-05-14 11:09:44,694 DEBUG [com.xyz.uresdemo.home.ResultHome] merging Result instance
2008-05-14 11:09:45,227 ERROR [com.xyz.uresdemo.home.ResultHome] merge failed
java.lang.reflect.UndeclaredThrowableException
at org.apache.commons.logging.impl.Log4jProxy.log(Log4jProxy.java:309)
at org.apache.commons.logging.impl.Log4jProxy.error(Log4jProxy.java:283)
at org.apache.commons.logging.impl.Log4JLogger.error(Log4JLogger.java:208)
at com.xyz.uresdemo.home.ResultHome.merge(ResultHome.java:51)
at com.xyz.uresdemo.home.ResultHome.merge(ResultHome.java:47)
at com.xyz.uresdemo.home.ResultHome.merge(ResultHome.java:47)
at com.xyz.uresdemo.home.ResultHome.merge(ResultHome.java:47)


Top
 Profile  
 
 Post subject:
PostPosted: Wed May 14, 2008 10:08 am 
Expert
Expert

Joined: Tue May 13, 2008 3:42 pm
Posts: 919
Location: Toronto & Ajax Ontario www.hibernatemadeeasy.com
Why is the object getting detached? After all, that's your exception.

An object is being associated with the Session, the session is getting closed, or the object is getting evicted, and then you are trying to save it again, thus triggering the exception about saving the detached object.

You might want to extend the life of your session transaction to avoid the existence of the detached object.

_________________
Cameron McKenzie - Author of "Hibernate Made Easy" and "What is WebSphere?"
http://www.TheBookOnHibernate.com Check out my 'easy to follow' Hibernate & JPA Tutorials


Top
 Profile  
 
 Post subject:
PostPosted: Wed May 14, 2008 11:03 am 
Beginner
Beginner

Joined: Fri May 21, 2004 5:22 am
Posts: 24
Bingo! That is the hit on the nail. Care to point me where to look for this configuration, does not recall been thru the documentation about this?

Note: The infinite loop is invalid, that was a programmatic mistake i did.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 9 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.