Ok, so to be more precise, I did a small test.
Here are my mappings :
Code:
<class name="Customer" table="PERSON">
<id name="id" type="integer">
<generator class="foreign">
<param name="property">person</param>
</generator>
</id>
<property name="customerNumber" type="integer" />
<one-to-one name="person" class="Person" constrained="true"/>
</class>
<class name="Person" table="PERSON">
<id name="id" type="integer">
<generator class="increment" />
</id>
<property name="name" type="string" />
</class>
<class name="Employee" table="PERSON">
<id name="id" type="integer">
<generator class="foreign">
<param name="property">person</param>
</generator>
</id>
<property name="wage" type="integer" />
<one-to-one name="person" class="Person" constrained="true"/>
</class>
so here is the table structure hibernate generates :
Code:
create table PERSON (
id INT not null,
name VARCHAR(255) null,
customerNumber INT null,
wage INT null,
primary key (id)
)
Here is my code to do my test :
Code:
public static void main(String[] args)
{
try
{
SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
Session session = sessionFactory.openSession();
Person person = new Person();
person.setName("test");
Customer customer = new Customer();
customer.setCustomerNumber(new Integer(2));
customer.setPerson(person);
Employee employee = new Employee();
employee.setWage(new Integer(10000));
employee.setPerson(person);
session.save(person);
session.save(customer);
session.save(employee);
session.flush();
session.close();
}
catch (Exception ex)
{
ex.printStackTrace();
}
}
and here is the stack trace of the exception I got
Code:
net.sf.hibernate.NonUniqueObjectException: a different object with the same identifier value was already associated with the session: 1, of class: Customer
at net.sf.hibernate.impl.SessionImpl.doSave(SessionImpl.java:834)
at net.sf.hibernate.impl.SessionImpl.saveWithGeneratedIdentifier(SessionImpl.java:772)
at net.sf.hibernate.impl.SessionImpl.save(SessionImpl.java:731)
at Test.main(Test.java:24)
I read some forum"s thread where somebody said that you could not have more thant one object in the session that point on the same row. It seems that this is the problem I have.
I just want to know why there is this restriction and if I can bypass this restriction in some way ?
Thanx, Seb