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.  [ 5 posts ] 
Author Message
 Post subject: Instance Altered
PostPosted: Mon Feb 04, 2008 1:46 pm 
Newbie

Joined: Wed Oct 04, 2006 2:43 pm
Posts: 16
Location: Lexington, KY
Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp

Hibernate version:

3.x

Mapping documents:

Code:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping
   PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN"
   "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>

   <class name="xxx.pojo.PostponeDateLimit" table="POSTPONE_DATE_LIMIT">
      <composite-id class="xxx.pojo.PostponeDateLimitKey" mapped="true">
         <key-property name="postponeDate" type="calendar_date" column="TARGET_DATE"/>
         <key-property name="controlCode" column="CONTROL_CODE"/>
      </composite-id>
      <property name="maxCount" column="MAX_COUNT"/>
   </class>

</hibernate-mapping>



Code between sessionFactory.openSession() and session.close():

Full stack trace of any exception that occurs:

Name and version of the database you are using:

The generated SQL (show_sql=true):

Debug level Hibernate log excerpt:

Code:
233528 DEBUG org.hibernate.loader.Loader | total objects hydrated: 1
233528 DEBUG org.hibernate.engine.TwoPhaseLoad | resolving associations for [xxx.pojo.PostponeDateLimit#component[postponeDate,controlCode]{control
Code=050, postponeDate=17 November 2008}]
233528 DEBUG org.hibernate.engine.TwoPhaseLoad | done materializing entity [com.
xxx.pojo.PostponeDateLimit#component[postponeDate,controlCode]{controlC
ode=050, postponeDate=17 November 2008}]
233528 DEBUG org.hibernate.engine.StatefulPersistenceContext | initializing non-
lazy collections
233528 DEBUG org.hibernate.loader.Loader | done entity load
233528 DEBUG org.hibernate.jdbc.JDBCContext | after autocommit
233528 DEBUG org.hibernate.jdbc.ConnectionManager | transaction completed on session with on_close connection release mode; be sure to close the session to rele
ase JDBC resources!
233528 DEBUG org.hibernate.impl.SessionImpl | after transaction completion
233528 DEBUG org.hibernate.event.def.AbstractFlushingEventListener | flushing se
ssion
233528 DEBUG org.hibernate.event.def.AbstractFlushingEventListener | processing
flush-time cascades
233528 DEBUG org.hibernate.event.def.AbstractFlushingEventListener | dirty check
ing collections
233528 DEBUG org.hibernate.event.def.AbstractFlushingEventListener | Flushing en
tities and processing referenced collections
233528 DEBUG org.hibernate.impl.SessionImpl | closing session
233528 DEBUG org.hibernate.jdbc.ConnectionManager | performing cleanup
233528 DEBUG org.hibernate.jdbc.ConnectionManager | releasing JDBC connection [
(open PreparedStatements: 0, globally: 0) (open ResultSets: 0, globally: 0)]
233528 DEBUG org.hibernate.jdbc.JDBCContext | after transaction completion
233528 DEBUG org.hibernate.jdbc.ConnectionManager | transaction completed on ses
sion with on_close connection release mode; be sure to close the session to rele
ase JDBC resources!
233528 DEBUG org.hibernate.impl.SessionImpl | after transaction completion
233528 WARN  com.acs.gs.eJuror.web.struts.services.impl.BaselineEJurorExceptionH
andler | execute()
233544 ERROR com.acs.gs.eJuror.web.struts.services.impl.BaselineEJurorExceptionH
andler | Exception Encountered in Web Layer : identifier of an instance of xxx.pojo.PostponeDateLimit was altered from 'PostponeDate:11/17/2008 0:0
0 AM|Control Code:050' to 'PostponeDate:11/17/2008 0:00 AM|Control Code:050'; nested exception is org.hibernate.HibernateException: identifier of an instance of
xxx.pojo.PostponeDateLimit was altered from 'PostponeDate:11/17/20
08 0:00 AM|Control Code:050' to 'PostponeDate:11/17/2008 0:00 AM|Control Code:05
0'
org.springframework.orm.hibernate3.HibernateSystemException: identifier of an in
stance of xxx.pojo.PostponeDateLimit was altered from 'PostponeDate
:11/17/2008 0:00 AM|Control Code:050' to 'PostponeDate:11/17/2008 0:00 AM|Contro
l Code:050'; nested exception is org.hibernate.HibernateException: identifier of
an instance of xxx.pojo.PostponeDateLimit was altered from 'Postpo
neDate:11/17/2008 0:00 AM|Control Code:050' to 'PostponeDate:11/17/2008 0:00 AM|
Control Code:050'
Caused by:
org.hibernate.HibernateException: identifier of an instance of xxx.pojo.PostponeDateLimit was altered from 'PostponeDate:11/17/2008 0:00 AM|Control
Code:050' to 'PostponeDate:11/17/2008 0:00 AM|Control Code:050'
        at org.hibernate.event.def.DefaultFlushEntityEventListener.checkId(Defau
ltFlushEntityEventListener.java:58)
        at org.hibernate.event.def.DefaultFlushEntityEventListener.getValues(Def
aultFlushEntityEventListener.java:157)
        at org.hibernate.event.def.DefaultFlushEntityEventListener.onFlushEntity
(DefaultFlushEntityEventListener.java:113)
        at org.hibernate.event.def.AbstractFlushingEventListener.flushEntities(A
bstractFlushingEventListener.java:196)
        at org.hibernate.event.def.AbstractFlushingEventListener.flushEverything
ToExecutions(AbstractFlushingEventListener.java:76)
        at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlus
hEventListener.java:26)
        at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1000)
        at org.springframework.orm.hibernate3.HibernateAccessor.flushIfNecessary
(HibernateAccessor.java:390)
        at org.springframework.orm.hibernate3.HibernateTemplate.execute(Hibernat
eTemplate.java:373)
        at org.springframework.orm.hibernate3.HibernateTemplate.get(HibernateTem
plate.java:464)
        at org.springframework.orm.hibernate3.HibernateTemplate.get(HibernateTem
plate.java:458)
        at xxx.dao.impl.PostponeDateLimitDAOImpl.getLimitForDate(Po
stponeDateLimitDAOImpl.java:29)



Code where the error is happening (using spring):

Code:
   public long getLimitForDate(Calendar date, String divCode) {
      PostponeDateLimitKey key = new PostponeDateLimitKey(date, divCode);
      PostponeDateLimit limit =(PostponeDateLimit) getHibernateTemplate().get(PostponeDateLimit.class, key);

      if(limit==null){
         return -1;
      }
      
      return limit.getMaxCount();
   }



The POJOs:

Code:
public class PostponeDateLimit extends PostponeDateLimitKey {
   
   private static final long serialVersionUID = 1L;
   
   private long       maxCount;
   /**
    * no-arg constructor
    */
   public PostponeDateLimit() {
      super();
   }


   /**
    * copy constructor
    */
   public PostponeDateLimit(PostponeDateLimit postponeDateLimit) {
      super(postponeDateLimit);
      ReflectionUtil.copyJavaBean(postponeDateLimit, this);
   }


   /**
    * fully-qualified constructor
    * @param postponeDate the postpone date requested
    * @param control_code the control code for that date
    * @param maxCount the limit specified
    */
   public PostponeDateLimit(Calendar postponeDate, String control_code, long maxCount) {
      super(postponeDate, control_code);
      setMaxCount(maxCount);
   }
   
   
   /* (non-Javadoc)
    * @see xxx.pojo.PostponeDateLimitKey#equals(java.lang.Object)
    */
   @Override
   public boolean equals(Object obj) {
      if (obj instanceof PostponeDateLimit) {
         PostponeDateLimit limit = (PostponeDateLimit) obj;
         
         boolean ret = super.equals(limit);
         
         if(ret){
            ret = getMaxCount() == limit.getMaxCount();
         }
         
         return ret;
      }
      return false;
   }
   
   
   /* (non-Javadoc)
    * @see xxx.pojo.PostponeDateLimitKey#hashCode()
    */
   @Override
   public int hashCode() {
      return toString().hashCode();
   }


   /* (non-Javadoc)
    * @see xxx.pojo.PostponeDateLimitKey#toString()
    */
   @Override
   public String toString() {
      StringBuilder sb = new StringBuilder(super.toString());
      sb.append("|Max count:");
      sb.append(getMaxCount());
      
      return sb.toString();
   }
   
   
   /**
    * @return the maxCount
    */
   public long getMaxCount() {
      return maxCount;
   }
   
   
   /**
    * @param maxCount the maxCount to set
    */
   public void setMaxCount(long maxCount) {
      this.maxCount = maxCount;
   }


Code:
   private static final long serialVersionUID = 1L;
   
   private String       controlCode;
   private Calendar    postponeDate;

   /**
    * no-arg constructor
    */
   public PostponeDateLimitKey() {
      super();
   }

   
   /**
    * copy constructor
    */
   public PostponeDateLimitKey(Calendar date, String controlCode) {
      this();
      setControlCode(controlCode);
      setPostponeDate(date);
   }

   
   /**
    * copy constructor
    */
   public PostponeDateLimitKey(PostponeDateLimitKey key) {
      this();
      setControlCode(key.getControlCode());
      setPostponeDate(key.getPostponeDate());
   }

   
   /* (non-Javadoc)
    * @see java.lang.Object#equals(java.lang.Object)
    */
   @Override
   public boolean equals(Object obj) {
      if (obj instanceof PostponeDateLimitKey) {
         PostponeDateLimitKey limitKey = (PostponeDateLimitKey) obj;
         
         return toString().equals(limitKey.toString());
      }
      return false;
   }
   

   /* (non-Javadoc)
    * @see java.lang.Object#hashCode()
    */
   @Override
   public int hashCode() {
      return toString().hashCode();
   }
   
   
   /* (non-Javadoc)
    * @see java.lang.Object#toString()
    */
   @Override
   public String toString() {
      CalendarFormatter cf = CalendarFormatter.getFormatter();

      StringBuilder sb = new StringBuilder();
      sb.append("'PostponeDate:");
      sb.append(cf.getShortDate(getPostponeDate()));

      sb.append(" ");
      sb.append(cf.getShortTime((getPostponeDate())));

      sb.append("|Control Code:");
      sb.append(getControlCode().trim());
      sb.append("'");

      return sb.toString();
   }
   
   
   /**
    * @return the postponeDate
    */
   public Calendar getPostponeDate() {
      return postponeDate;
   }


   /**
    * @param postponeDate the postponeDate to set
    */
   public void setPostponeDate(Calendar postponeDate) {
      this.postponeDate = postponeDate;
   }


   /**
    * based on the configuration paramter, this is how we're counting the limit. e.g., by court location
    * or division code
    * @return the divisionCode
    */
   public String getControlCode() {
      return controlCode;
   }


   /**
    * based on the configuration paramter, this is how we're counting the limit. e.g., by court location
    * or division code
    * @param controlCode the divisionCode to set
    */
   public void setControlCode(String controlCode) {
      this.controlCode = controlCode;
   }
}



I find it interesting that the dump says it's comparing PostponeDateLimit objects, but the toString() method is from PostponeDateLimitKey.


Ideas?

Thanks.


Top
 Profile  
 
 Post subject: Re: Instance Altered
PostPosted: Mon Feb 04, 2008 6:44 pm 
Expert
Expert

Joined: Wed Apr 11, 2007 11:39 am
Posts: 735
Location: Montreal, QC
The only reason I can think this would happen is that:

1- The code in database is not a varchar therefore when retrieved it contains leading spaces. Note in the toString method you are trimming. That's a bad idea.

2- The calendar passed to public long getLimitForDate(Calendar date, String divCode) is not an instance of GregorianCalendar (or in general it is not the same calendar object hibernate is making for you).


The only thing you have to make sure if that these values match in the key object and what has been loaded from the database. Keep me posted on this. I like to know what the solution is.


Farzad-


Top
 Profile  
 
 Post subject: Re: Instance Altered
PostPosted: Tue Feb 05, 2008 9:23 am 
Newbie

Joined: Wed Oct 04, 2006 2:43 pm
Posts: 16
Location: Lexington, KY
farzad wrote:

1- The code in database is not a varchar therefore when retrieved it contains leading spaces. Note in the toString method you are trimming. That's a bad idea.



It is a CHAR(5). How should I handle this? I would assume that hibernate would be using my equals method which calls toString().


Top
 Profile  
 
 Post subject: Re: Instance Altered
PostPosted: Tue Feb 05, 2008 10:46 am 
Expert
Expert

Joined: Wed Apr 11, 2007 11:39 am
Posts: 735
Location: Montreal, QC
Not in this case. It gets the values from properties and compares them one by one. You could use a custom type. I do believe can google for a trimmed string hibernate custom type and find an available code.




Farzad-


Top
 Profile  
 
 Post subject: Re: Instance Altered
PostPosted: Tue Feb 05, 2008 12:35 pm 
Newbie

Joined: Wed Oct 04, 2006 2:43 pm
Posts: 16
Location: Lexington, KY
farzad wrote:
Not in this case. It gets the values from properties and compares them one by one. You could use a custom type. I do believe can google for a trimmed string hibernate custom type and find an available code.

Farzad-


Or, perhaps, trim it in the setter.

At any rate, I switched the column type to VARCHAR and it appears to have fixed the problem. I'll look into this more later.

Thanks!


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 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.