У меня есть класс Product, который имеет ассоциацию 1-ко-многим (составной шаблон). Ниже приведены pojo, hbm, тестовый код и log / error соответственно. Может кто-нибудь объяснить, о чем эта ошибка. Почему я получаю эту ошибку при установке первичного ключа родителя?
Продукт pojo
public class Product
{
private Long id;
private Set<Product> children = Collections.emptySet();
public void addChild(final Product child)
{
if (children.isEmpty())
{
children = Sets.newHashSet();
}
child.setParent(this);
children.add(child);
}
}
hbm.xml
<class name="Product" table="PRODUCT">
<set name="children" lazy="false" table="PRODUCT">
<key>
<column name="ID" />
</key>
<one-to-many class="Product" />
</set>
</class>
Тест
public void save()
{
// Level 1 - mortgage LOB
Product mortgage = new Product();
mortgage.setCode("Mortgage");
Product ml = new Product();
ml.setCode("Mortgage Loan");
Product me = new Product();
me.setCode("Home Equity LOC");
//Level 2
mortgage.addChild(ml);
mortgage.addChild(me);
hibernateTemplate.save(mortgage);
}
журнал и ошибка
DEBUG [org.hibernate.SQL] insert into PRODUCT (ID, CODE, NAME, STARTDATE, ENDDATE, ISDECISIONABLE, ISSELECTABLE) values (null, ?, ?, ?, ?, ?, ?)
DEBUG [org.hibernate.type.StringType] binding 'Mortgage' to parameter: 1
DEBUG [org.hibernate.type.StringType] binding null to parameter: 2
DEBUG [org.hibernate.type.TimestampType] binding null to parameter: 3
DEBUG [org.hibernate.type.TimestampType] binding null to parameter: 4
DEBUG [org.hibernate.type.BooleanType] binding 'false' to parameter: 5
DEBUG [org.hibernate.type.BooleanType] binding 'false' to parameter: 6
DEBUG [org.hibernate.SQL] call identity()
DEBUG [org.hibernate.SQL] update PRODUCT set ID=? where ID=?
DEBUG [org.hibernate.type.LongType] binding '1' to parameter: 1
ERROR [org.hibernate.event.def.AbstractFlushingEventListener] Could not synchronize database state with session
org.hibernate.TransientObjectException: object references an unsaved transient instance - save the transient instance before flushing: com.Product
at org.hibernate.engine.ForeignKeys.getEntityIdentifierIfNotUnsaved(ForeignKeys.java:219)
at org.hibernate.type.EntityType.getIdentifier(EntityType.java:397)
at org.hibernate.type.ManyToOneType.nullSafeSet(ManyToOneType.java:78)