-->
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: Error: Could not synchronize database state with session
PostPosted: Tue Feb 02, 2010 11:48 pm 
Newbie

Joined: Tue Feb 02, 2010 8:44 pm
Posts: 6
Hi,
Sorry I just started using Hibernate and can't get pass the first mission.
I can save (create) an entity bean, but I get this error when I try to update it. I don't understand why?
It crashes when I call commit on the transaction. The record gets successfully inserted into the database.
My configuration is auto-commit turned off

Any help is highly appreciated.

16:31:26,919 ERROR AbstractFlushingEventListener : Could not synchronize database state with session
org.hibernate.exception.GenericJDBCException: could not update: [hnz.biz.modules.dms.entitybean.DMMemo#2]
at org.hibernate.exception.SQLStateConverter.handledNonSpecificException(SQLStateConverter.java:126)
at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:114)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:66)
at org.hibernate.persister.entity.AbstractEntityPersister.update(AbstractEntityPersister.java:2453)
at org.hibernate.persister.entity.AbstractEntityPersister.updateOrInsert(AbstractEntityPersister.java:2335)
at org.hibernate.persister.entity.AbstractEntityPersister.update(AbstractEntityPersister.java:2635)
at org.hibernate.action.EntityUpdateAction.execute(EntityUpdateAction.java:115)
at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:279)


It happens when i call transaction.commit() after I successfully insert a record then trying to update it again.
This is my code:
Code:
Session session = HUtil.getCurrentSession();
session.beginTransaction();
DMMemo memo = new DMMemo();      
session.save(memo);                  
memo.setRef("dm123456");
memo.setStatus("QUERY");
session.update(memo);
session.getTransaction().commit(); //- crashes here!


This is my entity class DMMemo:
Code:
@Entity
@Table(name = "DMMEMO")
public class DMMemo implements Serializable
{
   @Id
   @GeneratedValue(strategy=GenerationType.AUTO)
        @Column(name = "DMMEMOID", updatable = false, nullable = false)
        private Long memoId = null;
   
   @Column(name="DMSTATUS",length=5, nullable = false)
   private String status = "CREQD";
   
   
   @Column(name="DMREF",length=20)
   private String ref = null;
   
   @OneToMany(   targetEntity=DMItem.class,
            cascade=CascadeType.ALL,
            fetch=FetchType.EAGER)
       @JoinColumn(name="DIMEMOID")
   private Set<DMItem> items = new HashSet<DMItem>(0);//- another entity bean
***
}


Finally, this is my configuration:
Code:
<session-factory>

        <!-- Database connection settings -->
        <property name="connection.driver_class">com.ibm.as400.access.AS400JDBCDriver</property>
        <property name="connection.url">******</property>
   <property name="connection.autocommit">false</property>
   <property name="hibernate.connection.isolation">0</property>


       
        <property name="connection.pool_size">100</property>   
        <property name="dialect">org.hibernate.dialect.DB2Dialect</property>
        <property name="current_session_context_class">thread</property>     
        <property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
        <property name="show_sql">true</property>

   <mapping class="hnz.biz.modules.dms.entitybean.DMItem"/>
   <mapping class="hnz.biz.modules.dms.entitybean.DMMemo"/>

   </session-factory>


Top
 Profile  
 
 Post subject: Re: Error: Could not synchronize database state with session
PostPosted: Wed Feb 03, 2010 12:59 am 
Senior
Senior

Joined: Mon Jul 07, 2008 4:35 pm
Posts: 141
Location: Berlin
Hi dhafir,

I think it is a quite unusual situation you produce here (while checking out the stuff I suppose).

You should recall that commiting the transaction (i.e., session.getTransaction().commit();) will actually execute all the actions you called since you started the transaction (i.e., session.beginTransaction();). So, although you called session.save(memo); this call hasn't been executed yet, i.e. the actual memo instance hasn't been persisted to the database yet. It will not be persisted unless you commit the transaction.

With this in mind - it actually does not make any sense to update an object that - one might think is saved - is not saved yet. Normally you would have a newly constructed instance persisted with something like
Code:
Session session = HUtil.getCurrentSession();
session.beginTransaction();
DMMemo memo = new DMMemo();     
session.save(memo);
session.getTransaction().commit();

Now, the DMMemo instance is right there where you expect it... in the DB. Since you still have a reference to the Java object you can now safely modify it. Note, that the Session.update() method is usually used to attach a modified, detached object to an open session. When the actual update action on the DB takes place is not known in advance. If you want to see your changes being reflected in the DB right away, you might want to prefer Session.merge().

CU
Froestel

_________________
Have you tried turning it off and on again? [Roy]


Top
 Profile  
 
 Post subject: Re: Error: Could not synchronize database state with session
PostPosted: Wed Feb 03, 2010 3:29 pm 
Newbie

Joined: Tue Feb 02, 2010 8:44 pm
Posts: 6
Thanks Froestel,
What confuses me is that the session.save(0 did actually created a new record on teh Database, even when the transaction.commit() crashes. I go to the database and I can see a new record being created. It is the change I make afterword and within the transaction that causes this crash.

The code I am using is taken from Cameron's "Hibernate Made Easy" which is exactly what I am doing. In the code you see below he saves a new object then updates other properties of the same object and then committing the transaction which is basically calling update.

Note that I get the same error even if I removed session.update(memo) before committing.

http://www.hiberbook.com/HiberBookWeb/learn.jsp?tutorial=18mappingonetomanyassociations
Code:
HibernateUtil.recreateDatabase();
    Session session=HibernateUtil.beginTransaction();

    Team team = new Team();
    Player p1 = new Player();
    Player p2 = new Player();

    session.save(team);
    session.save(p1);
    session.save(p2);

    team.setName("Pickering Atoms");
    p1.setNickName("Lefty");
    p1.setTeam(team);
    p2.setNickName("Blinky");
    p2.setTeam(team);
    HibernateUtil.commitTransaction();


Last edited by dhafir on Wed Feb 03, 2010 5:15 pm, edited 1 time in total.

Top
 Profile  
 
 Post subject: Re: Error: Could not synchronize database state with session
PostPosted: Wed Feb 03, 2010 4:56 pm 
Senior
Senior

Joined: Mon Jul 07, 2008 4:35 pm
Posts: 141
Location: Berlin
Hi dhafir,

sorry, did not mean to confuse you. And I'm sorry that I didn't tell you the right reason for the failure. First of all, a call to session.save() directly hits the database (see Hibernate documentation). So, that's the reason for having the entry appearing in your database table.

The problem should be your call to session.update(). This method does not actually do what you'd expect at first sight from it: it does not trigger an SQL UPDATE statement. It is used to re-attach a detached object (see documentation on Hibernate object states) to a session.

You should actually be able to just leave out the session.update() call. But you tell that the error persists. Hm, try session.flush() before committing. Does that help?

CU
Froestel

_________________
Have you tried turning it off and on again? [Roy]


Top
 Profile  
 
 Post subject: Re: Error: Could not synchronize database state with session
PostPosted: Wed Feb 03, 2010 5:14 pm 
Newbie

Joined: Tue Feb 02, 2010 8:44 pm
Posts: 6
Thanks Froestel, really appreciate your reply.

I found the problem. I was using Cascading constraints on my DMMemo entity, but the journalling is turned off on teh database.
If you look at my original post you will see the following in the hibernate.cfg.xml

Code:
<property name="connection.autocommit">false</property>
   <property name="hibernate.connection.isolation">0</property>


So auto commit is false because journaling is off.
And if you look at my DMMemo entity you will see that I am using cascading type ALL

Code:
@OneToMany(   targetEntity=DMItem.class,
            cascade=CascadeType.ALL,
            fetch=FetchType.EAGER)
       @JoinColumn(name="DIMEMOID")
   private Set<DMItem> items = new HashSet<DMItem>(0);//- another entity bean


I don't know why cascading requires journalling, but it is a DB issue.
By cascading I was trying to avoid having to save/update all the children (DMItem) of DMMemo. But now it looks like I still have to maintain things manually, something I was hoping to avoid using Hibernate.

Thanks for your help.

Dhafir


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.