Hibernate - Collection Mapping
- From: kito <juri.strumpflohner@xxxxxxxxx>
- Date: Thu, 19 Jul 2007 16:54:02 -0000
Hi,
I'm new to Hibernate and now I got stuck when trying to map a simple 1-
to-many relation.
Basically I have a class "Location" and a class "Post". Each Location
can have a number of posts for which reason the class Location holds a
collection containing Post-objects.
I gave a look to the Hibernate Reference (http://www.hibernate.org/
hib_docs/reference/en/html/collections.html) and constructed my XML
mappings like this:
LOCATION.JAVA
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="org.mps.model">
<class name="Location" table="locations">
<id name="id" column="idLocation" type="java.lang.Integer">
<generator class="increment" />
</id>
<property name="cellid" column="cellid" type="java.lang.String" not-
null="true" unique-key="Locations_cellid"/>
<property name="lat" column="lat" type="java.lang.String"/>
<property name="lon" column="lon" type="java.lang.String"/>
<set name="posts" inverse="true" lazy="false">
<key column="idLocation" not-null="true"/>
<one-to-many class="Post"/>
</set>
</class>
</hibernate-mapping>
POST.JAVA
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="org.mps.model">
<class name="org.mps.model.Post" table="posts">
<id name="id" column="idPost" type="java.lang.Integer">
<generator class="increment" />
</id>
<property name="title" column="title" type="java.lang.String"/>
<property name="body" column="body" type="java.lang.String"/>
<property name="user" column="_user" type="java.lang.String"/>
<many-to-one name="location" class="Location"
column="Locations_idLocation"/>
</class>
</hibernate-mapping>
The problem is now that if I execute the following code, only the
Location is stored into the DB, but not the contained Posts in its
collection...Am I doing something wrong??
Code:
Location l = new Location();
l.setCellid("7");
l.setLat("14,5223");
l.setLon("20,223");
Set s = new HashSet();
Post p = new Post();
p.setTitle("My post");
p.setBody("My body of post");
p.setUser("Juri");
s.add(p);
l.setPosts(s);
save(l);
The "save(l)" does nothing else than creating a Session and executing
an saveOrUpdate of the Location object
.
- Follow-Ups:
- Re: Hibernate - Collection Mapping
- From: Philipp Taprogge
- Re: Hibernate - Collection Mapping
- Prev by Date: Re: UnsatisfiedLinkError
- Next by Date: DBMS implementation of XML column
- Previous by thread: UnsatisfiedLinkError
- Next by thread: Re: Hibernate - Collection Mapping
- Index(es):
Relevant Pages
|