-->
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: How map three tables on two classes?
PostPosted: Sat Aug 05, 2006 4:47 pm 
Newbie

Joined: Sun Jun 25, 2006 12:27 pm
Posts: 5
I'm novice in hibernate and i have a little problem with mapping.
I have three tables:

CREATE TABLE USER (
ID NUMBER NOT NULL,
LOGIN VARCHAR2(20) NOT NULL,
PASSWORD VARCHAR2(128) NOT NULL,
FULLNAME VARCHAR2(128) NULL,
);

CREATE TABLE PROFILE (
ID NUMBER NOT NULL,
PROFILE_NAME VARCHAR2(64) NULL
);

CREATE TABLE USER_PROFILE (
USER_ID NUMBER NOT NULL,
PROFILE_ID NUMBER NOT NULL
);

I need to use three tables, because one user may have few profiles, and one profile may have few users.

I want to map this three tables on two classes. Can i do this or i have to use three classes.


Top
 Profile  
 
 Post subject:
PostPosted: Sun Aug 06, 2006 1:08 pm 
Newbie

Joined: Fri Sep 02, 2005 6:03 pm
Posts: 13
Konstantin,

If you set up these relationships within your "CREATE TABLE" sql and use the hibernate reverse engineering tools to create your mapping files and POJOs, you may get all you need to make this work.

I'm not sure what flavor DB you're using, but there should be a way for you to set up constraints, foreign keys, etc. such that your RDBMS enforces the relationships you desire your table records to have. Hibernate's reverse engineering tools will see what you did and build your objects for you.

I've have very good luck with doing what you're trying to do.


Top
 Profile  
 
 Post subject:
PostPosted: Sun Aug 06, 2006 7:13 pm 
Newbie

Joined: Sun Jul 30, 2006 3:36 pm
Posts: 4
This is just off the top of my head - a unidirectional association. You will only be able to retrieve the profiles from the Usr class and not vice versa. You'll probably want to turn lazy loading on. If you want to have several users for one profile, just use this very same mapping for Profile.java, Profile.hbm.xml but change the properties. Hope it helps.

Usr.java
protected List<Profile> profiles = new ArrayList<Profile>();

public List<Profile> getProfiles() {
return profiles;
}

public void setProfiles(List<Profile> profiles) {
this.profiles = profiles;
}

<!-- Usr.hbm.xml -->
<bag name="profiles" table="user_profile" cascade="all" lazy="false">
<key column="user_id" not-null="true"/>
<many-to-many class="com.blabla.Profile"
column="profile_id"/>
</bag>


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.