gpt4 book ai didi

java - hibernate 查询中缺少具有空值的行

转载 作者:搜寻专家 更新时间:2023-10-30 21:33:25 24 4
gpt4 key购买 nike

我有两个带有外键引用的表:

Comm TABLE:

+----+------------+
| ID | NAME |
+----+------------+
| 1 | comm name1 |
| 2 | comm name2 |
| 3 | comm name3 |
+----+------------+

LOCATION TABLE: - COMM_ID FK to Comm --> id

+---------+------+-----+
| COMM_ID | FORM | TO |
+---------+------+-----+
| 1 | 720 | 721 |
| 1 | 725 | |
| 1 | | 766 |
| 1 | | |
| 2 | 766 | 225 |
| 3 | 766 | 222 |
+---------+------+-----+

问题是 Hibernate 返回了我的 comm 对象缺少 locationSET<location>没有 FROM 的所有行 TO (如表 LOCATION 中 COMM_ID = 1 的最后一行)丢失。

否则(如果只有 FROMTO 之一)返回该行...为什么?

Comm对象:

@ElementCollection
@CollectionTable(name="LOCATION",joinColumns=@JoinColumn(name="COMM_ID"))
public Set<LOCATION> getLocations(){
return locations;
}
public void setLocations(Set<LOCATION> locations){
this.locations=locations;
}

Location类:

@Embeddable
class Location implements java.io.Serializable {

private BigDecimal fromLocationId;
private BigDecimal toLocationId;

public Location() {
}

public Location(BigDecimal fromLocationId, BigDecimal toLocationId) {
this.fromLocationId = fromLocationId;
this.toLocationId = toLocationId;
}

@Column(name="FROM", nullable=true, precision=22, scale=0)
public BigDecimal getFromLocationId() {
return this.fromLocationId;
}

public void setFromLocationId(BigDecimal fromLocationId) {
this.fromLocationId = fromLocationId;
}

@Column(name="TO", nullable=true, precision=22, scale=0)
public BigDecimal getToLocationId() {
return this.toLocationId;
}

public void setToLocationId(BigDecimal toLocationId) {
this.toLocationId = toLocationId;
}

@Override
public int hashCode() {
return com.google.common.base.Objects.hashCode(fromLocationId, toLocationId);
}

@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null || getClass() != obj.getClass()) {
return false;
}
final LOCATION other = (LOCATION) obj;
return com.google.common.base.Objects.equal(this.fromLocationId, other.fromLocationId) && com.google.common.base.Objects.equal(this.toLocationId, other.toLocationId);
}
}

我正在使用 Hibernate - 4.3.6

日志:

org.hibernate.SQL - 
select
locations0_.COMM_ID as COMM_ID1_2_0_,
locations0_.FROM as FROM2_8_0_,
locations0_.TO as TO4_8_0_
from
LOCATION locations0_
where
locations0_.COMM_ID=1

我在我的数据库中对其进行了检查,它返回了正确的结果。

最佳答案

可嵌入对象的数据包含在其父表的多个列中。由于没有单个字段值,因此无法知道父项对可嵌入对象的引用是否为空。可以假设如果可嵌入对象的每个字段值都为空,那么引用应该为空,但是没有办法用所有空值来表示可嵌入对象。 JPA 不允许嵌入项为空,但某些 JPA 提供程序可能支持这一点。

Hibernate:加载实体时,如果可嵌入对象中的所有列都为空,则嵌入引用将设置为空。

了解更多信息 click here

因此,当您保存可为空的可嵌入值时,hibernate 允许保存,但在获取时,hibernate 会丢弃那些可为空的行。

关于java - hibernate 查询中缺少具有空值的行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30984466/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com