java - Why hibernate selects entities before persisting entities mapped by joined_subclass inheritance type? -


i have superclass person , 2 subclasses - parent , child. mapped joined_table inheritance type.

parent , child have bidirectional one-to-many relationship.

person.hbm.xml contains configuration:

<hibernate-mapping>     <class name="com.masterhibernate.simplehibernatedemo.person"         table="person">         <cache usage="read-write" />          <id name="id" column="id">             <generator class="assigned" />         </id>          <property name="name">             <column name="name" length="16" not-null="true" />         </property>         <property name="surname">             <column name="surname" length="36"></column>         </property>         <property name="address">             <column name="address" length="22"></column>         </property>         <joined-subclass name="com.masterhibernate.simplehibernatedemo.parent"             table="parent">             <key column="person_id" foreign-key="parent_person" />             <property name="job" column="workplace" length="22" type="string" />             <set name="children" inverse="true" cascade="save-update" lazy="true">                 <cache usage="read-write" />                 <!-- specifies foreign key column of child table -->                 <key column="parentpk" />                 <one-to-many class="com.masterhibernate.simplehibernatedemo.child" />             </set>         </joined-subclass>         <joined-subclass name="com.masterhibernate.simplehibernatedemo.child"             table="child">             <key column="person_id" foreign-key="child_person" />             <property name="toy" column="toy" length="55" type="string" />             <many-to-one name="parent"                 class="com.masterhibernate.simplehibernatedemo.parent" column="parentpk"                 foreign-key="child_parent" />         </joined-subclass>     </class> </hibernate-mapping> 

when persist parent , associated child entities hibernate issues select statements before inserting. these select statements query data of child entities child , person tables.

database recreated on each run , there nothing select.

here extract main method persists parent , associated child entities:

parent parent = new parent("volodia", "levytskyi", "mykolaiv");         parent.setid((long) 1);         set<child> children = new hashset<>();         children.add(new child((long) 2, "ivan", "mcgregor", "odessa"));         children.add(new child((long) 3, "borys", "mcrobin", "donetsk"));         children.add(new child((long) 4, "nazar", "mccrespo", "teernopil"));         parent.setchildren(children);          session session = sessionfactory.opensession();         transaction transaction = session.begintransaction();          session.save(parent);         // criteria criteria = session.createcriteria(person.class);         // list<person> list = criteria.list();         // system.out.println("list1=" + list);         transaction.commit();         session.close(); 

why select statements before insert statements?

the problem in id generator strategy of parent , child entities:

<generator class="assigned" /> 

hibernate cannot determine if entity assigned id exists in database or not. that's why issues select statements ensure no duplicate ids inserted. after moved id generator strategy native stopped selecting before inserting:

<generator class="native" /> 

Comments

Popular posts from this blog

database - VFP Grid + SQL server 2008 - grid not showing correctly -

jquery - Set jPicker field to empty value -

.htaccess - htaccess convert request to clean url and add slash at the end of the url -