I know I'm doing something stupid, but can't figure out what. Please help.
here's the parent's mapping:
Code:
<hibernate-mapping>
<class name="edu.ucsd.som.salpro.model.SalProEmployee" table="salpro_employee">
<id name="id" column="salpro_employee_id" type="long">
<generator class="native"/>
</id>
<property name="fullName" column="full_name" not-null="true" type="string" />
<property name="homeDepartmentCode" column="home_department_id" type="int" access="field" update="false" insert="false"/>
<property name="employeeId" column="ucsd_id" type="string"/>
<many-to-one name="homeDepartment" column="home_department_id" class="edu.ucsd.som.salpro.model.SalProDepartment" outer-join="false"/>
<one-to-one name="appointment" class="edu.ucsd.som.salpro.model.Appointment" cascade="all" access="field" />
<set name="divisions" lazy="true" table="salpro_employee_division">
<key column="employee_id"/>
<many-to-many class="edu.ucsd.som.salpro.model.SalProDivision" column="division_id"/>
</set>
</class>
</hibernate-mapping>
and the child's mapping:
Code:
<hibernate-mapping>
<class name="edu.ucsd.som.salpro.model.Appointment" table="salpro_appointment">
<id name="_id" column="appointment_id" type="long">
<!-- <generator class="native"/>-->
<generator class="foreign">
<param name="property">employee</param>
</generator>
</id>
<property name="onScale" column="on_scale" type="boolean" />
<one-to-one name="employee" class="edu.ucsd.som.salpro.model.SalProEmployee" constrained="true"/>
<many-to-one name="apuCode" column="apu_code" class="edu.ucsd.som.salpro.model.ApuCode"/>
</class>
</hibernate-mapping>
When I do this:
Code:
Appointment app = new Appointment();
SalProEmployee emp = new SalProEmployee();
app.setEmployee(emp);
PersistentObject.save(app);
what I get is a new record in the employee table, but no new record in the appointment table.
Now, if I go into Appointment.hbm.xml and change <id> from foreign to native, then an appointment record does get inserted, but the primary key value is not, obviously, syncrhoized with the parent's primary key.
What am I doing wrong?
Hibernate version:
2.1.3
Mapping documents:
see above
Code between sessionFactory.openSession() and session.close():
see above
Full stack trace of any exception that occurs:
n/a
Name and version of the database you are using:
mysql 3.1.xx
The generated SQL (show_sql=true):
see below
Debug level Hibernate log excerpt:
...
[main] DEBUG net.sf.hibernate.impl.SessionFactoryImpl - instantiated session factory
Hibernate factory inited
[main] DEBUG net.sf.hibernate.impl.SessionImpl - opened session
Openning Hibernate session net.sf.hibernate.impl.SessionImpl@1e7c5cb
[main] DEBUG net.sf.hibernate.impl.SessionImpl - saveOrUpdate() unsaved instance
[main] DEBUG net.sf.hibernate.impl.SessionImpl - saving [edu.ucsd.som.salpro.model.SalProEmployee#<null>]
[main] DEBUG net.sf.hibernate.impl.SessionImpl - executing insertions
[main] DEBUG net.sf.hibernate.engine.Cascades - processing cascades for: edu.ucsd.som.salpro.model.SalProEmployee
[main] DEBUG net.sf.hibernate.engine.Cascades - done processing cascades for: edu.ucsd.som.salpro.model.SalProEmployee
[main] DEBUG net.sf.hibernate.impl.WrapVisitor - Wrapped collection in role: edu.ucsd.som.salpro.model.SalProEmployee.divisions
[main] DEBUG net.sf.hibernate.engine.Cascades - id unsaved-value strategy NULL
[main] DEBUG net.sf.hibernate.persister.EntityPersister - Inserting entity: edu.ucsd.som.salpro.model.SalProEmployee (native id)
[main] DEBUG net.sf.hibernate.impl.BatcherImpl - about to open: 0 open PreparedStatements, 0 open ResultSets
[main] DEBUG net.sf.hibernate.SQL - insert into salpro_employee (full_name, ucsd_id, home_department_id) values (?, ?, ?)
[main] DEBUG net.sf.hibernate.impl.BatcherImpl - preparing statement
[main] DEBUG net.sf.hibernate.persister.EntityPersister - Dehydrating entity: [edu.ucsd.som.salpro.model.SalProEmployee#<null>]
[main] DEBUG net.sf.hibernate.type.StringType - binding 'Jonny Smith' to parameter: 1
[main] DEBUG net.sf.hibernate.type.StringType - binding null to parameter: 2
[main] DEBUG net.sf.hibernate.engine.Cascades - id unsaved-value strategy NULL
[main] DEBUG net.sf.hibernate.type.StringType - binding '34' to parameter: 3
[main] DEBUG net.sf.hibernate.persister.AbstractEntityPersister - Natively generated identity: 2
[main] DEBUG net.sf.hibernate.impl.BatcherImpl - done closing: 0 open PreparedStatements, 0 open ResultSets
[main] DEBUG net.sf.hibernate.impl.BatcherImpl - closing statement
[main] DEBUG net.sf.hibernate.engine.Cascades - processing cascades for: edu.ucsd.som.salpro.model.SalProEmployee
[main] DEBUG net.sf.hibernate.engine.Cascades - cascading to saveOrUpdate()
[main] DEBUG net.sf.hibernate.impl.SessionImpl - saveOrUpdate() unsaved instance
[main] DEBUG net.sf.hibernate.impl.SessionImpl - generated identifier: 2
[main] DEBUG net.sf.hibernate.impl.SessionImpl - saving [edu.ucsd.som.salpro.model.Appointment#2]
[main] DEBUG net.sf.hibernate.engine.Cascades - done processing cascades for: edu.ucsd.som.salpro.model.SalProEmployee