首页 > 解决方案 > 如何防止 Hibernate 获取加入的实体?

问题描述

我有一个如下所示的休眠查询

select si from SoldItem si 
join fetch si.sales s
join fetch si.stock st

我的期望是从中获取数据SoldItemSales并且Stock仅。我不想要来自其他表的数据。

下面是我的 Java 模型和 XML 映射文件

出售物品.java

public class SoldItem implements java.io.Serializable {

    private Integer idsoldItem;
    @JsonIgnoreProperties({"hibernateLazyInitializer", "handler"})
    private Sales sales;
    @JsonIgnoreProperties({"hibernateLazyInitializer", "handler"})
    private Stock stock;
    private int quantity;
    private double totalCost;

    public SoldItem() {
    }

    public SoldItem(Sales sales, Stock stock, int quantity, double totalCost) {
        this.sales = sales;
        this.stock = stock;
        this.quantity = quantity;
        this.totalCost = totalCost;
    }

    //Getters and Setters here

}

已售商品.hbm.xml

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<!-- Generated Sep 4, 2020 1:35:36 PM by Hibernate Tools 4.3.1 -->
<hibernate-mapping>
    <class name="beans.SoldItem" table="sold_item" catalog="wn_stock_system" optimistic-lock="version">
        <id name="idsoldItem" type="java.lang.Integer">
            <column name="idsold_item" />
            <generator class="identity" />
        </id>
        <many-to-one name="sales" class="beans.Sales" fetch="select">
            <column name="idsales" not-null="true" />
        </many-to-one>
        <many-to-one name="stock" class="beans.Stock" fetch="select">
            <column name="idstock" not-null="true" />
        </many-to-one>
        <property name="quantity" type="int">
            <column name="quantity" not-null="true" />
        </property>
        <property name="totalCost" type="double">
            <column name="total_cost" precision="22" scale="0" not-null="true" />
        </property>
    </class>
</hibernate-mapping>

销售.java

public class Sales implements java.io.Serializable {

    private Integer idsales;
    @JsonIgnoreProperties({"hibernateLazyInitializer", "handler"})
    private User user;
    private String customerName;
    private Date dateCreated;
    private double totalCost;

    public Sales() {
    }

    public Sales(User user, Date dateCreated, double totalCost) {
        this.user = user;
        this.dateCreated = dateCreated;
        this.totalCost = totalCost;
    }

   //Getters and Setters here

}

销售.hbm.xml

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<!-- Generated Sep 4, 2020 1:35:36 PM by Hibernate Tools 4.3.1 -->
<hibernate-mapping>
    <class name="beans.Sales" table="sales" catalog="wn_stock_system" optimistic-lock="version">
        <id name="idsales" type="java.lang.Integer">
            <column name="idsales" />
            <generator class="identity" />
        </id>
        <many-to-one name="user" class="beans.User" fetch="select">
            <column name="iduser" not-null="true">
                <comment>The person who made the sale</comment>
            </column>
        </many-to-one>
        <property name="customerName" type="string">
            <column name="customer_name" length="100" />
        </property>
        <property name="dateCreated" type="timestamp">
            <column name="date_created" length="0" not-null="true" />
        </property>
        <property name="totalCost" type="double">
            <column name="total_cost" precision="22" scale="0" not-null="true" />
        </property>
    </class>
</hibernate-mapping>

股票.java

public class Stock implements java.io.Serializable {

    private Integer idstock;
    @JsonIgnoreProperties({"hibernateLazyInitializer", "handler"})
    private Product product;
    private int quantity;
    private Date dateCreated;
    private Date lastUpdated;

    public Stock() {
    }

    public Stock(Product product, int quantity) {
        this.product = product;
        this.quantity = quantity;
    }

    public Stock(Product product, int quantity, Date dateCreated, Date lastUpdated) {
        this.product = product;
        this.quantity = quantity;
        this.dateCreated = dateCreated;
        this.lastUpdated = lastUpdated;
    }
   //Getters and setters here
}

股票.hbm.xml

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<!-- Generated Sep 4, 2020 1:35:36 PM by Hibernate Tools 4.3.1 -->
<hibernate-mapping>
    <class name="beans.Stock" table="stock" catalog="wn_stock_system" optimistic-lock="version">
        <id name="idstock" type="java.lang.Integer">
            <column name="idstock" />
            <generator class="identity" />
        </id>
        <many-to-one name="product" class="beans.Product" fetch="select">
            <column name="idproduct" not-null="true" />
        </many-to-one>
        <property name="quantity" type="int">
            <column name="quantity" not-null="true" />
        </property>
        <property name="dateCreated" type="timestamp">
            <column name="date_created" length="0" />
        </property>
        <property name="lastUpdated" type="timestamp">
            <column name="last_updated" length="0" />
        </property>
    </class>
</hibernate-mapping>

但是在我的代码中,它会从 中获取数据UserProduct依此类推。User是 的成员Sales并且Product是 的成员Stock

我怎样才能停止这种情况并仅从 ​​和 获取SoldItem数据?SalesStock

标签: javamysqlhibernatejpahql

解决方案


推荐阅读