为什么hibernate在持久化由joined_subclass继承类型映射的实体之前选择实体?

时间:2014-06-10 17:41:26

标签: java hibernate joined-subclass

我有超类Person和两个子类 - Parent和Child。它们使用JOINED_TABLE继承类型进行映射。

父母和孩子有双向的一对多关系。

Person.hbm.xml包含所有配置:

<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>

当我坚持使用Parent和关联的子实体时,hibernate会在插入之前发出select语句。这些select语句从子表和person表中查询子实体的数据。

每次运行都会重新创建数据库,无需选择。

以下是持久保存Parent和关联子实体的main方法的摘录:

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();

为什么在insert语句之前有那些select语句?

1 个答案:

答案 0 :(得分:0)

问题出在父母和子实体的id生成器策略中:

<generator class="assigned" />

Hibernate无法确定数据库中是否存在具有已分配id的实体。这就是为什么它发出select语句以确保不插入重复的id。 在我转移到id生成器策略native之后,它在插入之前停止了选择:

<generator class="native" />