The location table has a PK made up of 3 FK fields. The only way that I can successfully add a row to the location table is if I save the customer, email, and phone records first, build the LocationId myself, associate it to the location class and then call session.save(location).
Is this normal for composite PK tables?
Code:
Phone phone = new Phone();
phone.setPhoneNumber("330-555-1212");
session.save(phone);
Email email = new Email();
email.setEmailAddr("johndoe@usa.net");
session.save(email);
Location loc = new Location();
LocationId id = new LocationId(cust, email, phone);
loc.setId(id);
loc.setAddress("999 Third Street");
loc.setCity("Younstown");
loc.setName("Younstown Office");
loc.setState("OH");
loc.setZip("44514");
session.save(loc);
...
// To access I have to go through the id.
Customer c = (Customer)session.load(Customer.class, 1);
for (Iterator i = c.getLocations().iterator(); i.hasNext();) {
Location loc = (Location)i.next();
System.out.println(loc.getAddress());
System.out.println(loc.getId().getEmail().getEmailAddr());
System.out.println(loc.getId().getPhone().getPhoneNumber());
}
Hibernate version: 3.1
Mapping documents:Code:
<hibernate-mapping>
<class name="maa.hibernate.Location" table="location" catalog="accounting">
<composite-id name="id" class="maa.hibernate.LocationId">
<key-many-to-one name="customer" class="maa.hibernate.Customer">
<column name="customer_id" />
</key-many-to-one>
<key-many-to-one name="email" class="maa.hibernate.Email">
<column name="email_id" />
</key-many-to-one>
<key-many-to-one name="phone" class="maa.hibernate.Phone">
<column name="phone_id" />
</key-many-to-one>
</composite-id>
<property name="name" type="java.lang.String">
<column name="name" length="45" not-null="true" />
</property>
<property name="address" type="java.lang.String">
<column name="address" length="45" not-null="true" />
</property>
<property name="city" type="java.lang.String">
<column name="city" length="30" not-null="true" />
</property>
<property name="state" type="java.lang.String">
<column name="state" length="2" not-null="true" />
</property>
<property name="zip" type="java.lang.String">
<column name="zip" length="10" not-null="true" />
</property>
</class>
</hibernate-mapping>
Code between sessionFactory.openSession() and session.close():
Full stack trace of any exception that occurs:
Name and version of the database you are using:
MySQL 5.0 InnoDB