简体   繁体   中英

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

I have superclass Person and two subclasses - Parent and Child. They are mapped with JOINED_TABLE inheritance type.

Parent and Child have bidirectional one-to-many relationship.

Person.hbm.xml contains all 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 I persist Parent and associated Child entities hibernate issues select statements before inserting. These select statements query for data of Child entities from child and person tables.

Database is recreated on each run and there is nothing to select.

Here is extract from main method that persists Parent and 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 are those select statements before insert statements?

The problem was in id generator strategy of Parent and Child entities:

<generator class="assigned" />

Hibernate cannot determine if entity with assigned id exists in database or not. That's why it issues select statements to ensure that no duplicate ids are inserted. After I moved to id generator strategy native it stopped selecting before inserting:

<generator class="native" />

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM