-->
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: Hibernate auto retrieve
PostPosted: Wed Oct 01, 2008 1:41 am 
Newbie

Joined: Wed Oct 01, 2008 1:34 am
Posts: 1
I am attempting to create a transaction involving the creation of a new user and assigning roles to that user at the same time. Every user is getting the role of "ROLE_USER". Now my question is, is there a way to get hibernate to recognize that a "ROLE_USER" in the db already and to use that one instead of creating a new one/throwing an error (depending on if i force uniqueness).

I know I can just look up ROLE_USER through hibernate every time a new user is made and add role or I could have statics that are initialized at app launch, but I figured I'd see if there was a "short cut" where if I just put user.addRole("ROLE_USER") it would be smart enough to just use the existing ROLE_USER if there was one.

Thanks!


Top
 Profile  
 
 Post subject:
PostPosted: Wed Oct 01, 2008 8:11 am 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
If the "ROLE_USER" is the primary key of the role, you should be able to do something like this:

Code:
Role role = new Role();
role.setId("ROLE_USER");
user.addRole(role);


I have used this technique sometimes, but mostly I do lookup in the database. Beware of actions that cascades from the user to the role object It may have side-effects since the role object have empty properties for everything except the primary key.


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.