Thanks for the reply vijaybangy. I did not have an explicit mapping of the child back to the parent in the original child config file:
Code:
<hibernate-mapping>
<class name="com.xxx.orm.TravelTimeGPSPoints" table="TravelTimeGPSPoints" catalog="snaps">
<comment>
</comment>
<composite-id name="id" class="com.xxx.orm.TravelTimeGPSPointsId">
<key-property name="travelTimeSegmentId" type="long">
<column name="travelTimeSegmentId" />
</key-property>
<key-property name="latitude" type="double">
<column name="latitude" precision="22" scale="0" />
</key-property>
<key-property name="longitude" type="double">
<column name="longitude" precision="22" scale="0" />
</key-property>
<key-property name="sequenceNumber" type="int">
<column name="sequenceNumber" />
</key-property>
</composite-id>
</class>
</hibernate-mapping>
I then tried adding a many-one relationship in the child:
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 Jan 18, 2010 3:23:48 PM by Hibernate Tools 3.2.0.beta8 -->
<hibernate-mapping>
<class name="com.xxx.orm.TravelTimeGPSPoints" table="TravelTimeGPSPoints" catalog="snaps">
<comment>
</comment>
<composite-id name="id" class="com.xxx.orm.TravelTimeGPSPointsId">
<key-property name="travelTimeSegmentId" type="long">
<column name="travelTimeSegmentId" />
</key-property>
<key-property name="latitude" type="double">
<column name="latitude" precision="22" scale="0" />
</key-property>
<key-property name="longitude" type="double">
<column name="longitude" precision="22" scale="0" />
</key-property>
<key-property name="sequenceNumber" type="int">
<column name="sequenceNumber" />
</key-property>
</composite-id>
<many-to-one name="segment" column="travelTimeSegmentId" class="com.xxx.orm.TravelTimeSegment">
</many-to-one>
</class>
</hibernate-mapping>
and changed my code for setting the new parent and child to:
Code:
final TravelTimeSegment seg = new TravelTimeSegment(0, 10000, 1, 10, 0, 0, "classif", "unit test segment", (byte)0, new Date(), new HashSet<TravelTimeLink>(), new HashSet<TravelTimeGPSPoints>());
final TravelTimeGPSPoints p1 = new TravelTimeGPSPoints(new TravelTimeGPSPointsId(0, 38.4, 119.4, 0));
final Set<TravelTimeGPSPoints> gpsPoints = seg.getTravelTimeGPSPoints();
gpsPoints.add(p1);
p1.setSegment(seg);
// would like to save the segment here and have it automatically save the gps point and set the association
// of gps point to the owner segment
HibernateUtil.getSessionFactory().getCurrentSession().save(seg);
HibernateUtil.getSessionFactory().getCurrentSession().save(p1);
I then get a hibernate exception, which looks like it now thinks there are 2 foreign keys there. All I want to do is have the one foreign key in the child as I have to use an existing schema, and have Hibernate understand that the child is associated to the parent. Can anyone PLEASE help? I also included the legacy tables below the stack trace:
java.lang.ExceptionInInitializerError
at com.xxx.HibernateUtil.<clinit>(HibernateUtil.java:35)
at com.xxx.orm.TestHibernate.testAddSegment(TestHibernate.java:20)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:616)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:76)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)
at org.junit.runners.ParentRunner.run(ParentRunner.java:236)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:46)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
Caused by: org.hibernate.MappingException: Repeated column in mapping for entity: com.xxx.orm.TravelTimeGPSPoints column: travelTimeSegmentId (should be mapped with insert="false" update="false")
at org.hibernate.mapping.PersistentClass.checkColumnDuplication(PersistentClass.java:652)
at org.hibernate.mapping.PersistentClass.checkPropertyColumnDuplication(PersistentClass.java:674)
at org.hibernate.mapping.PersistentClass.checkColumnDuplication(PersistentClass.java:696)
at org.hibernate.mapping.PersistentClass.validate(PersistentClass.java:450)
at org.hibernate.mapping.RootClass.validate(RootClass.java:192)
at org.hibernate.cfg.Configuration.validate(Configuration.java:1102)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1287)
at com.xxx.HibernateUtil.<clinit>(HibernateUtil.java:27)
... 23 more
The parent:
Code:
mysql> describe TravelTimeLink;
+---------------------+---------------------+------+-----+---------------------+----------------+
| Field | Type | Null | Key | Default | Extra |
+---------------------+---------------------+------+-----+---------------------+----------------+
| id | bigint(20) unsigned | NO | PRI | NULL | auto_increment |
| listenPort | int(10) unsigned | NO | | NULL | |
| linkCapacity | int(10) unsigned | NO | | NULL | |
| maxTravelTime | int(10) unsigned | NO | | NULL | |
| maxLatency | int(10) unsigned | NO | | NULL | |
| linkName | varchar(64) | NO | | NULL | |
| isMIMO | tinyint(3) unsigned | NO | | NULL | |
| ARSEServerNodeId | bigint(20) unsigned | NO | MUL | NULL | |
| travelTimeSegmentId | bigint(20) unsigned | NO | MUL | NULL | |
| isDead | tinyint(3) unsigned | NO | | NULL | |
| modifyTime | timestamp | NO | | 0000-00-00 00:00:00 | |
+---------------------+---------------------+------+-----+---------------------+----------------+
The child:
Code:
mysql> describe TravelTimeGPSPoints;
+---------------------+---------------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------------------+---------------------+------+-----+---------+-------+
| travelTimeSegmentId | bigint(20) unsigned | NO | MUL | NULL | |
| latitude | double | NO | | NULL | |
| longitude | double | NO | | NULL | |
| sequenceNumber | int(10) unsigned | NO | | NULL | |
+---------------------+---------------------+------+-----+---------+-------+