-->
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.  [ 2 posts ] 
Author Message
 Post subject: begging for advice on best way to handle entities in ejb 3.0
PostPosted: Thu May 10, 2007 11:56 am 
Newbie

Joined: Sat Nov 19, 2005 3:09 pm
Posts: 6
Upgrading to ejb 3.0 from hibernate 3.0.2 and having many woes.

First issue: Let's say I have a User object, who has a UserStatus, and many Roles. I load the User in one session to a form, it is submitted to be saved in another session.

If i call em.merge(User), I get the TransientObjectException for the UserStatus. ok, makes sense, its dettached. I guess I could remerge mapped object (either manually or through cascade.)

Ok, lets try again... Now we get a SQLGrammarException: could not get next sequence value. Hmm its trying to insert a new UserStatus after I merged. I'm stumped there since it is a reattached entity that already has its ID set.

The only thing i can think of is to reload the UserStatus object before a save with a em.find(). Try again and: voila! that works. Now i look at all the other saves i have and I am depressed thinking of all the reloading i'll have to manually in my code. There has to be a better way, right? I'm missing something obvious I'm sure.

But I move on to be able to update the Roles the user belongs to. I check off a couple of roles in the form, my servlet controller loads the User and the new Roles from the database, and then submits them to the ejb and... great, the roles are there. Now lets uncheck them. Controller loads User, clear()s the set, and on submit we get...

java.sql.BatchUpdateException: Batch entry 0 update system_roles set sysuser_id=null where ..<snip>.. was aborted.

Its trying to null out the roles that are orphaned?? I have a hibernate cascade-delete-orphan annotation on that collection. I fiddle around for another couple hours playing with different combinations of clear()'ing the set and saving, getting rid of the delete-orphan and removing the roles one by one manually, etc.. But always it tries to set the user id foreign key to null when i try to delete a role.

So there are 2 possibilities to the 2 issues i mentioned above:
1) I missed some basic concepts and I need to change the way I am dealing with saving and reattaching objects
2) I am doing things correctly but my mappings/code/whatever is misconfigured.

If its 1) please let me know what I'm doing wrong.
If it is 2) I will try to boil this down to a very simple set of mappings and code and try to post it here and hope for some help.

Thanks in advance for any advice.


Top
 Profile  
 
 Post subject:
PostPosted: Thu May 10, 2007 3:57 pm 
Newbie

Joined: Wed Aug 30, 2006 3:17 pm
Posts: 11
I avoid working with detached/reattached objects like the plague. Trying to figure out what the correct cascade settings should be and keeping track of the state each object is in makes my head hurt. I generally just save off the ID's and then reload the entities by ID when I get to the next session.

That said, I'm confused why your system_roles table has a sysuser_id column. Assuming I understand your schema correctly (since you haven't posted your mappings, I'm just guessing what they look like), there should be a many-to-many relationship between your users and roles. However, the presence inside your roles table of a foreign key column referencing your users seems to indicate that you have it mapped as one-to-many (users <-> roles). If this isn't the case, then I couldn't say what the problem is without seeing your actual mapping files/annotations.

_________________
Rich Eggert


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