为什么Hibernate按实体ID而不是多对多关系查询?

时间:2019-04-26 14:09:57

标签: hibernate many-to-many

在获取由多对多关系过滤的先决条件列表时,我面临着一种奇怪的Hibernate行为。

当我执行以下查询时

session.createCriteria(ScanPrecondition.class)
        .createAlias("inputs", "inputs")
        .add(Restrictions.eq("inputs.id", inputId))
        .list();

我得到以下sql日志:

Hibernate: select scanprecon0_.ID as ID1_47_0_, scanprecon0_.DESCRIPTION as DESCRIPT2_47_0_ from SCAN_PRECONDITION scanprecon0_ where scanprecon0_.ID=?
Hibernate: select scanprecon0_.ID as ID1_47_0_, scanprecon0_.DESCRIPTION as DESCRIPT2_47_0_ from SCAN_PRECONDITION scanprecon0_ where scanprecon0_.ID=?
Hibernate: select scanprecon0_.ID as ID1_47_0_, scanprecon0_.DESCRIPTION as DESCRIPT2_47_0_ from SCAN_PRECONDITION scanprecon0_ where scanprecon0_.ID=?

为什么Hibernate通过实体ID而不是通过多对多关系查询?


以下是实体和hbm配置:

ScanPrecondition.java

package org.company.model;

public class ScanPrecondition {

    private String id;
    private String description;
    private Set<ScanInput> inputs = new HashSet<>(0);

    // getters and setters

    // equals and hash code by id

}

ScanInput.java

package org.company.model;

public class ScanInput {

    private String id;
    private String description;

    // getters and setters

    // equals and hash code by id

}

ScanPrecondition.hbm.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
    <class name="org.company.model.ScanPrecondition" table="SCAN_PRECONDITION">
        <id name="id" column="ID" type="string" length="17" />
        <property name="description" column="DESCRIPTION" type="string" length="255" not-null="true" />
        <set name="inputs" table="SCAN_INPUT_PRECONDITION" lazy="true">
            <key column="SCAN_PRECONDITION_ID" />
            <many-to-many class="org.company.model.ScanInput" column="SCAN_INPUT_ID" />
        </set>
    </class>
</hibernate-mapping>
  

休眠版本:4.2.6.Final

0 个答案:

没有答案