Hello,
I have three relations in my database which I want to map with Hibernate:
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.xmlCode:
<?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.xmlCode:
<?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.xmlCode:
<?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