-->
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.  [ 5 posts ] 
Author Message
 Post subject: How to map one-to-many relationship?
PostPosted: Fri Oct 27, 2006 1:44 pm 
Newbie

Joined: Thu Oct 19, 2006 7:53 pm
Posts: 10
How is the following one-to-many relationship mapped in Hibernate?

For example, suppose we have two entities: a US_State entity with attributes abbreviation and name, with abbreviation being a key, and an Address entity with attributes street, city, zip and state_abbrev. With the restriction that every state_abbrev value occurs as a value of abbreviation in some US_State instance, we have a one-to-many relationship from US_State to Address (See figure.) http://www2002.org/CDROM/alternate/698/


Top
 Profile  
 
 Post subject: Foreign Key Many-to-One mapping
PostPosted: Fri Oct 27, 2006 3:23 pm 
Newbie

Joined: Sat Jul 29, 2006 4:51 am
Posts: 11
dekalaked,

What you have there are two tables defined as follows:
(MySQL Code used to demonstrate)

Code:
create table US_State (
abbreviation VARCHAR(2) NOT NULL PRIMARY KEY,
name VARCHAR(64) NOT NULL ) type=MyISAM;

create table Address (
street VARCHAR(128) NOT NULL,
city VARCHAR(32) NOT NULL,
zip VARCHAR(16) NOT NULL,
state_abbrev VARCHAR(2) NOT NULL ) type=MyISAM;


Given the following SQL snippet what you have is a many-to-one relationship between the Address tables state_abbrev and the US_State tables abbreviation field therefore you can map it in hibernate like so:

Code:
<class name="Address" table="Address">
<!-- map the other fields here -->
<many-to-one name="stateAbbrev" class="US_State" column="state_abbrev" not-null="true" />
</class>


Most (note: I said MOST) one-to-many mappings can also be thought of as many-to-one mappings.

I hope this clarifies things for you!

------------------------------------------------
Nathan


Top
 Profile  
 
 Post subject:
PostPosted: Fri Oct 27, 2006 3:55 pm 
Newbie

Joined: Thu Oct 19, 2006 7:53 pm
Posts: 10
Hibernate seems to want to update the US_State table when the Address table is updated. Can Hibernate be configured to not update thet US_State table?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Oct 27, 2006 7:11 pm 
Newbie

Joined: Sat Jul 29, 2006 4:51 am
Posts: 11
dekalaked,

Hibernate will only update the US_State table when the instance of an Address object that you are persisting has a US_State object attached to the getStateAbbrev() field that is not in the database already.

It would be helpful if you could post some code snippets of where you are creating your objects and where you are persisting them.

Also, some snippets of your mapping files and actual object construction would be helpful also.

-----------------------------------------------
Nathan


Top
 Profile  
 
 Post subject:
PostPosted: Sun Oct 29, 2006 12:55 am 
Newbie

Joined: Thu Oct 19, 2006 7:53 pm
Posts: 10
Here are two mappings: Property and Office.
This works: <many-to-one name="office" class="Office" column="office_id" cascade="none" />
The error below is generated if cascade="none" is removed from
<many-to-one name="office" class="Office" column="office_id" />

21:51:26,453 DEBUG DefaultFlushEntityEventListener:229 - Updating entity: [domain.Property#2]
21:51:26,468 DEBUG DefaultFlushEntityEventListener:229 - Updating entity: [domain.Office#1]
21:51:26,468 WARN RequestProcessor:528 - Unhandled Exception thrown: class org.hibernate.PropertyValueException

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping
default-cascade="save-update"
auto-import="true"
package="domain"
default-lazy="false">

<class
name="domain.Property"
table="property"
>

<id
name="id"
type="java.lang.Integer"
column="property_id"
>
<generator class="increment" />
</id>

<many-to-one name="office" class="Office" column="office_id" cascade="none" />

<property
name="price"
type="java.lang.Integer"
column="price"
/>

</class>
</hibernate-mapping>

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping
auto-import="true"
package="domain"
default-lazy="false">

<class
name="domain.Office"
table="office"
>

<id
name="id"
type="java.lang.Integer"
column="office_id"
>
<generator class="increment" />
</id>

<property
name="name"
type="java.lang.String"
column="name"
not-null="true"
length="30"
/>

</class>
</hibernate-mapping>


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