Hibernate - Collection Mapping



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

.



Relevant Pages

  • Re: No "Hibernate" Button is present
    ... According to the PC experts I listen to while hibernate is fast to shut ... DON'T MAKE EXCUSES to respond to RTQ. ... Please DO NOT REPOST RTQ message bodies and hyperlinks. ... The majority of their posts are dedicated to attempting to match wits ...
    (misc.transport.trucking)
  • Re: No "Hibernate" Button is present
    ... According to the PC experts I listen to while hibernate is fast to shut down ... DON'T MAKE EXCUSES to respond to RTQ. ... Please DO NOT REPOST RTQ message bodies and hyperlinks. ... The majority of their posts are dedicated to attempting to match wits ...
    (misc.transport.trucking)
  • Re: OT but lots of clever guys hang here! - Bluetooth and security in cars
    ... 'hibernate' and the Sat-Nav also fully OFF (and not in standby so it ... Never believe posts in a newsgroup as you ...
    (uk.tech.broadcast)
  • Re: "Using hdparm in limited user accounts" or "starting progs with admin rights before waking up fr
    ... when I use Hibernate Trigger programs are not executed with admin ... returning from hibernation to my limited account. ... simply be but into a batch file and placed in the start-up folder. ... execute the command as part of your startup process. ...
    (microsoft.public.windowsxp.general)
  • Re: Endless Preparing to Hibernate
    ... Enquire, plan and execute ... Derek Harvey wrote: ... 'Preparing to Hibernate' but never gets any further and the machine ...
    (microsoft.public.windowsxp.help_and_support)