I am having some difficulty mapping a section of my model but I am not sure if this is a problem with my model, my mapping or just plain unacceptable to Hibernate.
Here's a simplified extract of my classes:
Code:
class Person {
private Long ID; // PK
private PersonName personName;
private Set otherPersonNames = new HashSet();
...
}
class PersonName {
private Long ID; // PK
private String firstName;
private String lastName;
private Long personID; // FK
...
}
The idea is that every Person has an associated PersonName and 0..* PersonName's in the Set otherPersonName.
I have two tables in a MySQL database with columns directly reflecting the classes above. Both tables use the autoincrement option for key generation.
My latest attempt for the mappings are:
Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 2.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
<hibernate-mapping>
<class name="Person" table="person">
<id name="ID" unsaved-value="null">
<generator class="increment"/>
</id>
<one-to-one name="personName" class="PersonName"/>
<set name="otherPersonNames" inverse="true" lazy="true" cascade="save-update">
<key column="personID"/>
<one-to-many class="PersonName"/>
</set>
</class>
<class name="PersonName" table="personname">
<id name="ID" type="java.lang.Long" unsaved-value="null">
<generator class="increment"/>
</id>
<property name="firstName" not-null="true"/>
<property name="lastName" not-null="true"/>
<many-to-one name="personID" class="Person" column="ID"/>
</class>
</hibernate-mapping>
When I run the following code...
Code:
...
// Create a Person
Person p = new Person();
// Create a PersonName and populate
PersonName pn = new PersonName();
pn.setFirstName("John");
pn.setLastName("Doe");
// Associate PersonName with Person
p.setPersonName(pn);
// Start Hibernate and attempt to save graph
Configuration cfg = new Configuration()
.addClass(Person.class)
.addClass(PersonName.class);
SessionFactory sf = cfg.buildSessionFactory();
Session sess = sf.openSession();
Transaction t = sess.beginTransaction();
sess.save(p);
t.commit();
sess.close();
...Hibernate starts up properly but gives the following error shortly after instantiating session factory (and much to my disappointment nothing is written to the database):
Code:
net.sf.hibernate.MappingException: Repeated column in mapping for class com.dynbio.core.person.PersonName should be mapped with insert="false" update="false": ID
I am rather new to Hibernate and have (re)read the Parent/Child mapping references but just can't figure out what I am doing wrong or how to express the mapping for what I want.
Any clues would be very welcome!