-->
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.  [ 3 posts ] 
Author Message
 Post subject: inverse="true" in Hibernate Annotations?
PostPosted: Wed Nov 23, 2005 8:24 am 
Beginner
Beginner

Joined: Sat Oct 08, 2005 2:13 am
Posts: 47
Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp

Hibernate version:Hibernate 3.1

Mapping documents:

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

Full stack trace of any exception that occurs:

Name and version of the database you are using:MySql

The generated SQL (show_sql=true):yes

Debug level Hibernate log excerpt:


I am using annotation in hibernate and I have a Set in an entity

class User {
....
Set<User> userRoles;

@OneToMany(cascade=CascadeType.PERSIST,targetEntity=Role.class)
@JoinColumn(name="user_id")
@Basic(fetch=FetchType.EAGER)
@Cascade(org.hibernate.annotations.CascadeType.DELETE_ORPHAN)
public Set<Role> getUserRoles() {
return userRoles;
}

}


my problem is when remove an object from userRoles, Hibernate first update the role table to null:
---<console>---
Hibernate: update role set user_id=null where user_id=? and role_name=? and user_id=?
---------------

and then try to delete that object
----<console>----
Hibernate: delete from role where role_name=? and user_id=?
ERROR - BatchingBatcher.doExecuteBatch(61) | Exception executing batch:
org.hibernate.StaleStateException: Batch update returned unexpected row count from update: 0 actual row count: 0 expected: 1
at org.hibernate.jdbc.BatchingBatcher.checkRowCount(BatchingBatcher.java:93)
at org.hibernate.jdbc.BatchingBatcher.checkRowCounts(BatchingBatcher.java:79)
at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:58)
at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:193)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:230)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:145)
at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:296)
at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:27)
at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:877)
at com.hibertest.test.HiberTest.testRemoveUserRole(HiberTest.java:51)
at com.hibertest.test.HiberTest.main(HiberTest.java:39)
-------------

because it was set to null before, second statement fails. before I solve this by set the inverse="true" in hbm.xml file but dont know how to improve it in annotation.

thanks


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 23, 2005 12:34 pm 
Expert
Expert

Joined: Sat Oct 25, 2003 8:49 am
Posts: 490
Location: Vrhnika, Slovenia
Probably the keys in role are user_id and role_name?

Maybe use an additional field role_id for primary key, and user_id only for foreign_id.

Did you try defining mapping on the role side?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 24, 2005 1:43 am 
Beginner
Beginner

Joined: Sat Oct 08, 2005 2:13 am
Posts: 47
Hi
yes in table "Role" there is two field that both of them are primary key.
[ user_id + role_name ]

and user_id is also foreign key referenced from table "User".

there is no mapping in Role class and I just define two simple fields. but I want to know if I want to define a mapping in that side like this:

class Role{
User user;
String roleName
}

how to define its composite id (@IdClass)?

thank you in advance


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