java - 如何防止 Hibernate 获取加入的实体?
问题描述
我有一个如下所示的休眠查询
select si from SoldItem si
join fetch si.sales s
join fetch si.stock st
我的期望是从中获取数据SoldItem
,Sales
并且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>
但是在我的代码中,它会从 中获取数据User
,Product
依此类推。User
是 的成员Sales
并且Product
是 的成员Stock
。
我怎样才能停止这种情况并仅从 和 获取SoldItem
数据?Sales
Stock
解决方案
推荐阅读
- laravel - 表单提交后显示消息 laravel
- jquery - 避免 setInterval jquery 中的等待时间
- matlab - MATLAB 图像处理:我的直方图的最后一个 bin 突然出现
- c# - 缺少选项“ASP.NET Web 配置”工具
- python - 意外的关键字参数“严格”
- javascript - 如何使用工作路由器链接将组件正确添加到我的 Jhipster + Angular 项目中?
- dll - 如何在 dll 中嵌入 VCL 组件并在 VCL 应用程序中使用它?
- shell - 为 fzf 在“fd”结果前加上“~/”。Sed 抱怨“找不到匹配项”
- reactjs - 如何使用 VSCode 在 Firefox 中保留扩展并请求启动
- ruby - Ruby 循环覆盖值