-->
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.  [ 6 posts ] 
Author Message
 Post subject: Mapping relationships... again
PostPosted: Mon Oct 27, 2003 6:05 pm 
Senior
Senior

Joined: Tue Oct 21, 2003 8:15 am
Posts: 186
I can understand relationships between entities (and classes) easily from a DDL or a UML diagram. But Hibernate confuses me totally. I've tried generating various DDL's but when I think I understand, I get stuck again.

Could someone explain how to realize the following in Hibernate mapping files? I'd like an explanation on these cases with and without bi-directionality as well.

I guess this is a lot to ask, but trying doesn't hurt... ;-)

Using UML notation:

Code:

CASE 1)

   1               0..1
A ---------------------- B


CASE 2)

   1               0..*
A ---------------------- B


CASE 3)

   0..1            0..*
A ---------------------- B


CASE 4)

   0..*            0..*
A ---------------------- B


CASE 5)

   1                 1
A ---------------------- B


I think that pretty much cover all the cases.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Oct 27, 2003 8:32 pm 
Regular
Regular

Joined: Tue Aug 26, 2003 3:34 pm
Posts: 54
Location: Farroupilha - Brasil
An example on MANY-TO-MANY (your case 4)

User 0...* ---------> 0...* Group (many-to-many)

Schema:

table user (
id integer PRIMARY KEY,
login VARCHAR(100),
...
)

table group (
id integer PRIMARY KEY,
name VARCHAR(100),
....
)

table user_group (
user_id INTEGER,
group_id INTEGER,
CONSTRAINT ug_pkey PRIMARY KEY (user_id,group_id),
CONSTRAINT ug_u_fkey FOREIGN KEY (user_id) REFERENCES user(id),
CONSTRAINT ug_g_fkey FOREIGN KEY (group_id) REFERENCES group(id)
)

Mapping:

<class name="com.estobel.model.User" table="user">

<id name="id" type="int" unsaved-value="null" >
<column name="id" sql-type="INTEGER" not-null="true"/>
<generator class="assigned"/>
</id>

<property name="login">
<column name="login" sql-type="varchar(20)" not-null="true" unique="true"/>
</property>

<set name="groups" table="user_group" lazy="true">
<key>
<column name="user_id" not-null="true"/>
</key>
<many-to-many class="com.estobel.model.Group">
<column name="group_id" not-null="true"/>
</many-to-many>
</set>
</class>

<class name="com.estobel.model.Group" table="group">

<id name="id" type="int" unsaved-value="null" >
<column name="id" sql-type="INTEGER" not-null="true"/>
<generator class="assigned"/>
</id>

<!-- If you want an bidirecional .... -->
<set name="users" table="user_group" inverse="true" lazy="true">
<key>
<column name="group_id" not-null="true"/>
</key>
<many-to-many class="com.estobel.model.User">
<column name="user_id" not-null="true"/>
</many-to-many>
</set>

</class>

Beans:

public class User {
int id;
String name;
Set groups;
.....
}

public class Group {
int id;
String name;
Set users; // If you want an bidirecional
}


Top
 Profile  
 
 Post subject:
PostPosted: Mon Oct 27, 2003 8:41 pm 
Regular
Regular

Joined: Tue Aug 26, 2003 3:34 pm
Posts: 54
Location: Farroupilha - Brasil
An example on MANY-TO-ONE.

Query ------------ 0...1 DataSource

I have many datasources and I can use them in many queries ... an query has one datasource.

Schema:

table query (
id INTEGER,
datasource_name VARCHAR(20),
CONSTRAINT query_ds_fkey FOREIGN KEY (datasource_name) REFERENCES datasource(name)
)

table datasource (
name VARCHAR(20),
url VARCHAR(200)
)

Mapping:

<class name="com.estobel.model.Query" table="query">
<id name="id" type="int" column="id">
<generator class="native">
<param name="sequence">nom_query_id_seq</param>
</generator>

</id>

<many-to-one name="dataSource" column="datasource_name" class="com.estobel.model.DataSource"/>
</class>

<class name="com.estobel.model.DataSource" table="nom_datasource">
<id name="name" type="string">
<column name="name" length="20"/>
<generator class="assigned"/>
</id>
<property name="url" column="username" type="string"/>
</class>

Beans:

public class Query {
int id;
DataSource datasource;
}

public class DataSource {
String name;
String url;
}


Top
 Profile  
 
 Post subject:
PostPosted: Mon Oct 27, 2003 8:53 pm 
Regular
Regular

Joined: Tue Aug 26, 2003 3:34 pm
Posts: 54
Location: Farroupilha - Brasil
An example ONE-TO-MANY ...

query 1 -------- 0...* parameter

I have a query that can have many parameters ... parameters exists only for that query:

Schema:

table query (
id INTEGER
name VARCHAR
)

table query_parameter (
query_id INTEGER NOT NULL,
param_id INTEGER NOT NULL,
name VARCHAR(100),
CONSTRAINT qp_pkey PRIMARY KEY (query_id,param_id),
CONSTRAINT qp_q_fkey FOREIGN KEY (query_id) REFERENCES nom_query(id)
)

Mapping:

<class name="com.estobel.model.Query" table="query">
<id name="id" type="int" column="id">
<generator class="native">
<param name="sequence">nom_query_id_seq</param>
</generator>

</id>
<set name="parameters" table="query_parameter" inverse="true" lazy="true"
order-by="param_id asc" cascade="delete">
<key>
<column name="query_id"/>
</key>
<one-to-many class="com.estobel.model.QueryParameter"/>
</set>

</class>

<class name="com.estobel.model.QueryParameter" table="nom_query_parameter">
<!-- The parameter id is a composite id -->
<composite-id>
<key-property name="id" column="param_id" type="int"/>
<key-many-to-one name="query" class="com.estobel.model.Query" column="query_id"/>
</composite-id>


<property name="name" />
</class>


Beans:

public class Query {
int id;
Set parameters;
}

public class QueryParameter {
int id;
Query query;
String name;
}


Top
 Profile  
 
 Post subject:
PostPosted: Mon Oct 27, 2003 8:59 pm 
Regular
Regular

Joined: Tue Aug 26, 2003 3:34 pm
Posts: 54
Location: Farroupilha - Brasil
I took these cases from my application ... I think that is a good start to clarify some doubts.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Oct 28, 2003 9:01 am 
Senior
Senior

Joined: Tue Oct 21, 2003 8:15 am
Posts: 186
THANKS! That was very useful.

Could you explain inverse="true" in UML terms as well?


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