-->
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: one class, two tables
PostPosted: Mon Jan 09, 2006 4:08 am 
Beginner
Beginner

Joined: Mon Mar 07, 2005 12:02 pm
Posts: 39
Hi there,

Im working with hibernate3 and I just can't get my head round this mapping that I need to do:

I need to map one class to write to 2 different tables:
Class:
public class User {
private Long id;
private String username;
private String password;
private String role;

public String getRole() {
return role;
}
public void setRole(String role) {
this.role = role;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
}

Table:
User_tbl

user_id int(8)
user_name varchar(50)
user_pass varchar(50)

User_Role_tbl

user_id int(8)
user_name varchar(50)
role_name varchar(50)

Can this be done and how?


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jan 09, 2006 4:53 am 
Beginner
Beginner

Joined: Mon Mar 07, 2005 12:02 pm
Posts: 39
Sorry, I messed up the post, the roles in the User class is actually a list. A user can have multiple roles. I got the previous post right using joins but I cant get it right with multiple roles


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jan 09, 2006 8:16 am 
Expert
Expert

Joined: Tue Nov 23, 2004 7:00 pm
Posts: 570
Location: mostly Frankfurt Germany
option 1)
one to many mapping
You should find an example for this in the Hibernate reference
option 2)
mapping of user to a component
there are also examples for this in the reference.

Regards Sebastian

_________________
Best Regards
Sebastian
---
Training for Hibernate and Java Persistence
Tutorials for Hibernate, Spring, EJB, JSF...
eBook: Hibernate 3 - DeveloperGuide
Paper book: Hibernate 3 - Das Praxisbuch
http://www.laliluna.de


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jan 11, 2006 4:30 am 
Regular
Regular

Joined: Tue Dec 14, 2004 5:21 am
Posts: 104
Location: india
your user class may have something line this in case of a seperate role entity

class User {
....
private Set<Role> roles ;

}

and mapping as

<set name="roles" cascade="all">
<key column="USER_ID" />
<one-to-many class="test.Role" />
</set>

_________________
sHeRiN
thanks for your ratings ...... :)


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.