In some cases evicting a transient entity gives: java.lang.IllegalArgumentException: Non-entity object instance passed to evict
I'm aware of several Hibernate issues covering this, and I'm also aware that some of the eviction rules changed due to JPA2.1 spec. However, the behavior seen here is inconsistent: when using XML mapping files and using the entity-name attribute on the class mapping, this exception seems to appear when the parent of a one-to-many is evicted while one of it's children is not yet persisted. When using the exact same example, but instead not using entity-name (and thus referring to the entity using it's class name) the eviction seems to work without a problem.
Please see the attached example project/test case.
Summary from the attached test case:
Given this mapping:
<class name="be.test.Parent" table="PARENT">
<id name="id" type="long" column="id" access="property">
<generator class="native"/>
</id>
<bag name="children" cascade="all-delete-orphan">
<key column="parent_id" not-null="true" update="false" />
<one-to-many entity-name="SomeChild" />
</bag>
</class>
<class name="be.test.Child" table="CHILD" entity-name="SomeChild">
<id name="id" type="long" column="id" access="property">
<generator class="native"/>
</id>
<property name="name"/>
</class>
The following test case wil fail due to :java.lang.IllegalArgumentException: Non-entity object instance passed to evict : be.test.Child
Parent parent = new Parent();
sessionFactory.getCurrentSession().save(parent);
Child child = new Child();
parent.getChildren().add(child);
sessionFactory.getCurrentSession().evict(parent);
If we now alter the mapping file but use class instead of entity-name:
<class name="be.test.Parent" table="PARENT">
<id name="id" type="long" column="id" access="property">
<generator class="native"/>
</id>
<bag name="children" cascade="all-delete-orphan">
<key column="parent_id" not-null="true" update="false" />
<one-to-many class="be.test.Child" />
</bag>
</class>
<class name="be.test.Child" table="CHILD">
<id name="id" type="long" column="id" access="property">
<generator class="native"/>
</id>
<property name="name"/>
</class>
The same test case does work.
|