-->
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.  [ 4 posts ] 
Author Message
 Post subject: Deleting an element from a @CollectionOfElements
PostPosted: Tue Sep 04, 2007 12:23 pm 
Beginner
Beginner

Joined: Thu Aug 09, 2007 3:48 pm
Posts: 38
Hibernate version: hibernate-3.2.5.ga
Name and version of the database you are using: HQL

I have a class named User that holds collection of Category.
Now i want to remove one category from the user.
When i am doing that i see in the sql log performed and i see it first
deletes the entry then tries to update it and thus gets exception.

Anyone can explain this to me? (Code is following)

The code of User.java that defines category collection
Code:
@CollectionOfElements
@OrderBy("name")
@Fetch(value = FetchMode.JOIN)
@IndexColumn(name = "categories_index")
public List<Category> getCategories()
{
   return this.categories;
}


Code deleting the category from the user:
Code:
Session session = HibernateUtil.getSession();
session.beginTransaction();
user.getCategories().remove(category);
session.update(user);
session.getTransaction().commit();


The log of org.hibernate.SQL which is weird to me:
Code:
org.hibernate.SQL - delete from User_category where User_id=? and categories_index=?
org.hibernate.SQL - update User_category set categories_id=? where User_id=? and categories_index=?
org.hibernate.util.JDBCExceptionReporter - failed batch
org.hibernate.event.def.AbstractFlushingEventListener - Could not synchronize database state with session
org.hibernate.exception.GenericJDBCException: Could not execute JDBC batch update
   at org.hibernate.exception.SQLStateConverter.handledNonSpecificException(SQLStateConverter.java:103)
   at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:91)
   at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
   at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:253)

Notice first the delete and then the unexplained update....

Thanks


Top
 Profile  
 
 Post subject:
PostPosted: Wed Sep 05, 2007 12:23 pm 
Beginner
Beginner

Joined: Thu Aug 09, 2007 3:48 pm
Posts: 38
ping


Top
 Profile  
 
 Post subject:
PostPosted: Sat Sep 08, 2007 2:58 am 
Newbie

Joined: Tue Jul 13, 2004 10:00 pm
Posts: 3
It seems like your 'user' object was obtained in another session from the one in which you are trying to perform the update. You may want to investigate session.merge(Object obj) instead of or prior to session.update().


Top
 Profile  
 
 Post subject:
PostPosted: Sat Sep 08, 2007 4:49 am 
Beginner
Beginner

Joined: Thu Aug 09, 2007 3:48 pm
Posts: 38
Every object i have (user for example), I aquire in different time and with a different session.

I do something like:
Code:
Session session = HibernateUtil.getSession();
session.beginTransaction();
session.load(id_of_object);  // or query for all objects of some type
session.getTransaction().commit();

Is this wrong? I can't keep a session open for a future use, it will be a wront thing to do.

When should merge be used? If user object changed then update should do the job as i understand.

Thanks you for your help.


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