-->
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: save-update delete cascade with sets
PostPosted: Tue Oct 25, 2005 7:35 pm 
Beginner
Beginner

Joined: Thu Jul 28, 2005 1:19 pm
Posts: 24
I ve been reading and reconfiguring for the last 2 days about parent children relationships. The problem is the following:

I have a user (parent) whish has children roles as set and one profile. Please see the maping files below.

I have the scenario where I need to update roles:

oldRoles = user.getUserRoles();
it = newRoles.iterator();
obj=null;
while(it.hasNext()){
obj = it.next();
if(!oldRoles.contains(obj)){
oldRoles.add(obj);
System.out.println(((UserRole)obj).getComp_id().getRolename()+" --------");
}
else{
;//do nothing
}
}

user.setUserRoles(oldRoles);



session.update(user);
session.flush();

The above peace is for testing purpose. Anyway, the system.out is executed meaning that I am adding new roles. But roles are never updated in the dataase, even with hibernate, I cann't see any update statement. Before the code above I have another peace where I update the profile and it goes through successfully. Any ideas.....Thanks in advance


-------------------------------------------------------------
mapping files
-------------------------------------------------------------
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 2.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd" >

<hibernate-mapping>
<!--
Created by the Middlegen Hibernate plugin 2.1

http://boss.bekk.no/boss/middlegen/
http://www.hibernate.org/
-->

<class
name="UserRole"
table="USER_ROLES"
>

<composite-id name="comp_id" class="UserRolePK">
<key-property
name="username"
column="USERNAME"
type="java.lang.String"
length="20"
/>
<key-property
name="rolename"
column="ROLENAME"
type="java.lang.String"
length="20"
/>
</composite-id>


<!-- Associations -->
<!-- derived association(s) for compound key -->
<!-- bi-directional many-to-one association to Role -->
<many-to-one
name="role"
class="Role"
update="false"
insert="false"
>
<column name="ROLENAME" />
</many-to-one>

<!-- bi-directional many-to-one association to User -->
<many-to-one
name="user"
class="User"
update="false"
insert="false"
>
<column name="USERNAME" />
</many-to-one>

<!-- end of derived association(s) -->


</class>
</hibernate-mapping>








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

<hibernate-mapping>
<!--
Created by the Middlegen Hibernate plugin 2.1

http://boss.bekk.no/boss/middlegen/
http://www.hibernate.org/
-->

<class
name="User"
table="USERS"
schema="WQBEMP"
>
<meta attribute="implement-equals" inherit="false">true</meta>

<id
name="username"
type="java.lang.String"
column="USERNAME"
>
<generator class="assigned" />
</id>

<property
name="password"
type="java.lang.String"
column="PASSWORD"
length="20"
/>

<!-- Associations -->
<one-to-one name="userProfile"
class="UserProfile"
cascade="save-update"
/>
<!-- bi-directional one-to-many association to UserProfile -->

<!-- bi-directional one-to-many association to UserRole -->
<set
name="userRoles"
lazy="false"
cascade="all"
inverse="true"
>
<key>
<column name="USERNAME" />
</key>
<one-to-many
class="UserRole"/>
</set>

</class>
</hibernate-mapping>


Top
 Profile  
 
 Post subject:
PostPosted: Wed Oct 26, 2005 7:00 am 
Beginner
Beginner

Joined: Thu Sep 01, 2005 7:43 am
Posts: 31
Location: León (Spain)
I think the problem is the "inverse=true" in:

<!-- bi-directional one-to-many association to UserRole -->
<set
name="userRoles"
lazy="false"
cascade="all"
inverse="true"
>

That way you are stating that Hibernate will ignore your "user.setUserRoles(oldRoles)" and expects that you do "userRole.setUser(user)" on each role to update the references. Try reversing the inverse attributes.

_________________
Please rate...

Expert on Hibernate errors... I've had them all... :P


Top
 Profile  
 
 Post subject: Caused by: java.sql.BatchUpdateException: ORA-01407: cannot
PostPosted: Wed Oct 26, 2005 1:13 pm 
Beginner
Beginner

Joined: Thu Jul 28, 2005 1:19 pm
Posts: 24
Thanks for the reply

OK I got it to a point where hibernate is actually trying to update by removing the inverse="true". I remmember I was there before but I got the following error:

Caused by: java.sql.BatchUpdateException: ORA-01407: cannot update ("WQBEMP"."USER_ROLES"."USERNAME") to NULL. What hibernate is trying to do is setting the username in the row to null leaving the other values in the record intact. Now what I want is to delete the whole record by remving from the set. Why does hibernate tries to remove only the username?

I will also search the forum for error ORA-01407. I know this is an oracle error and its very logical that Oracle throw this error, so I still think that my Hibernate configuration is wrong and thats why hibernate is acting this way

Thank You


Top
 Profile  
 
 Post subject: Re: Caused by: java.sql.BatchUpdateException: ORA-01407: can
PostPosted: Wed Oct 26, 2005 1:20 pm 
Expert
Expert

Joined: Mon Feb 14, 2005 12:32 pm
Posts: 609
Location: Atlanta, GA - USA
suleiman wrote:
Thanks for the reply

OK I got it to a point where hibernate is actually trying to update by removing the inverse="true". I remmember I was there before but I got the following error:

Caused by: java.sql.BatchUpdateException: ORA-01407: cannot update ("WQBEMP"."USER_ROLES"."USERNAME") to NULL. What hibernate is trying to do is setting the username in the row to null leaving the other values in the record intact. Now what I want is to delete the whole record by remving from the set. Why does hibernate tries to remove only the username?

I will also search the forum for error ORA-01407. I know this is an oracle error and its very logical that Oracle throw this error, so I still think that my Hibernate configuration is wrong and thats why hibernate is acting this way

Thank You


Why is Hibernate trying to UPDATE anything ? Shouldn't it either be inserting or nothing ?

_________________
Preston

Please don't forget to give credit if/when you get helpful information.


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.