-->
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: Problem persisting over two relations
PostPosted: Tue Nov 13, 2007 11:48 am 
Beginner
Beginner

Joined: Thu Mar 29, 2007 11:57 am
Posts: 27
Hello,

I have three relations in my database which I want to map with Hibernate:
Image
So far everything is OK. I can read data.

But if I want to store a new TrainingUnit including some new WayPoints to an existing User, only the TrainingUnit is persisted, but not the corresponding WayPoints. Can someone tell me why the waypoints aren't persisted?

The Java code looks for an existing user, creates a new TrainingUnit and two Waypoints. After that it adds the Waypoints and the User to the Trainingsunit and saves the Trainingunit. I think Hibernate should also save the Waypoints because the Waypoints are an attribute of the TrainingUnit, but this does not work :(

Here is my Java code for the test:
Code:
UserHome userHome = new UserHome();
        User user = userHome.getUserByNameAndPassword("Thorsten", "pw");
       
        if (user != null) {
            System.out.println("Username: " + user.getUsername() + " Password: " + user.getPassword());
        } else {
            System.out.println("No result returned");
        }
       
        // create a new TrainingUnit
        TrainingUnit trainingUnit1 = new TrainingUnit();
       
        // create two new WayPoints
        Waypoint waypoint1 = new Waypoint();waypoint1.setAlt(111d);waypoint1.setLat(222d);waypoint1.setLat(333d); waypoint1.setHeartrate(120d);
        waypoint1.setSpeed(12d);waypoint1.setTime(2134d);
        waypoint1.setTrainingUnit(trainingUnit1);
        Waypoint waypoint2 = new Waypoint();waypoint2.setAlt(11d);waypoint2.setLat(22d);waypoint2.setLat(33d);waypoint2.setHeartrate(10d);
        waypoint2.setSpeed(12d);waypoint2.setTime(234d);
        waypoint2.setTrainingUnit(trainingUnit1);
       
        // create a set to which the newly created WayPoints are added
        HashSet<Waypoint> waypoints = new HashSet<Waypoint>();
        waypoints.add(waypoint1);
        waypoints.add(waypoint2);
       
        // Add the set of new WayPoints and the already existing user to the new TrainingUnit
        trainingUnit1.setTitle("Testeintrag fuer eine Trainingseinheit." + new Date());
        trainingUnit1.setWaypoints(waypoints);
        trainingUnit1.setUser(user);
       
        TrainingUnitHome trainingUnitHome = new TrainingUnitHome();
        trainingUnitHome.persist(trainingUnit1);


Here is the method which persists the TrainingUnit:
Code:
public void persist(TrainingUnit transientInstance) {
        log.debug("persisting TrainingUnit instance");
        try {
            Session session = HibernateUtil.getSessionFactory().getCurrentSession();
            session.beginTransaction();
            session.save(transientInstance);
            session.getTransaction().commit();
        } catch (RuntimeException re) {
            log.error("persist failed", re);
            throw re;
        }
    }


Here are my mappng files:
User.hbm.xml
Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated 13.11.2007 15:16:04 by Hibernate Tools 3.1.0.beta5 -->
<hibernate-mapping>
    <class name="org.mytrainer.db.User" table="user">
        <id name="idUser" type="int">
            <column name="id_user" />
            <generator class="native" />
        </id>
        <property name="username" type="string">
            <column name="username" length="50" not-null="true" />
        </property>
        <property name="password" type="string">
            <column name="password" length="50" not-null="true" />
        </property>
        <set name="trainingUnits" inverse="true">
            <key>
                <column name="id_user" not-null="true" />
            </key>
            <one-to-many class="org.mytrainer.db.TrainingUnit" />
        </set>
    </class>
</hibernate-mapping>


Waypoint.hmb.xml
Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated 13.11.2007 15:16:04 by Hibernate Tools 3.1.0.beta5 -->
<hibernate-mapping>
    <class name="org.mytrainer.db.Waypoint" table="waypoint">
        <id name="idWaypoint" type="int">
            <column name="id_waypoint" />
            <generator class="native" />
        </id>
        <many-to-one name="trainingUnit" class="org.mytrainer.db.TrainingUnit" fetch="select">
            <column name="id_training_unit" not-null="true" />
        </many-to-one>
        <property name="long_" type="java.lang.Double">
            <column name="long" precision="17" scale="17" />
        </property>
        <property name="lat" type="java.lang.Double">
            <column name="lat" precision="17" scale="17" />
        </property>
        <property name="alt" type="java.lang.Double">
            <column name="alt" precision="17" scale="17" />
        </property>
        <property name="time" type="java.lang.Double">
            <column name="time" precision="17" scale="17" />
        </property>
        <property name="heartrate" type="java.lang.Double">
            <column name="heartrate" precision="17" scale="17" />
        </property>
        <property name="speed" type="java.lang.Double">
            <column name="speed" precision="17" scale="17" />
        </property>
    </class>
</hibernate-mapping>


Trainingunit.hbm.xml
Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated 13.11.2007 15:16:04 by Hibernate Tools 3.1.0.beta5 -->
<hibernate-mapping>
    <class name="org.mytrainer.db.TrainingUnit" table="training_unit">
        <id name="idTrainingUnit" type="int">
            <column name="id_training_unit" />
            <generator class="native" />
        </id>
        <many-to-one name="user" class="org.mytrainer.db.User" fetch="select">
            <column name="id_user" not-null="true" />
        </many-to-one>
        <property name="date" type="date">
            <column name="date" length="13" />
        </property>
        <property name="title" type="string">
            <column name="title" />
        </property>
        <property name="distance" type="java.lang.Double">
            <column name="distance" precision="17" scale="17" />
        </property>
        <property name="avgSpeed" type="java.lang.Double">
            <column name="avg_speed" precision="17" scale="17" />
        </property>
        <property name="startTime" type="time">
            <column name="start_time" length="15" />
        </property>
        <property name="endTime" type="time">
            <column name="end_time" length="15" />
        </property>
        <set name="waypoints" inverse="true">
            <key>
                <column name="id_training_unit" not-null="true" />
            </key>
            <one-to-many class="org.mytrainer.db.Waypoint" />
        </set>
    </class>
</hibernate-mapping>


Bye,
TMK


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 13, 2007 1:27 pm 
Newbie

Joined: Tue Nov 13, 2007 1:09 pm
Posts: 7
if you want waypoint to be stored as soon as you store training_unit
then do this
1) in training.hbm.xml you should add "cascade" attribute for one to many relationship with waypoint
<set ............. cascasde="all>
...........// one to many with waypoint.
</set>

But in your the if make above change as soon as you save training_unit the child entities waypoint are also being saved.That is sole purpose of casacade.

but relation ship will not be established as you put inverse="true" attribute on the training unit side.

The inverse="true" indicates that the training_unit is the servant of the waypoint means it never maintain the foreign key relation which exists between these two in the database tables only Waypoint maintains it.So remove that inverse="true" if you want relationship to be maintained as soon as you save training unit.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 13, 2007 1:57 pm 
Beginner
Beginner

Joined: Thu Mar 29, 2007 11:57 am
Posts: 27
Rama Vadakattu wrote:
if you want waypoint to be stored as soon as you store training_unit
then do this
1) in training.hbm.xml you should add "cascade" attribute for one to many relationship with waypoint
<set ............. cascasde="all>
...........// one to many with waypoint.
</set>

But in your the if make above change as soon as you save training_unit the child entities waypoint are also being saved.That is sole purpose of casacade.

but relation ship will not be established as you put inverse="true" attribute on the training unit side.

The inverse="true" indicates that the training_unit is the servant of the waypoint means it never maintain the foreign key relation which exists between these two in the database tables only Waypoint maintains it.So remove that inverse="true" if you want relationship to be maintained as soon as you save training unit.


Thank you very much!!


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.