You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Spring Boot+Hibernate关联查询数据为null及EntityNotFoundException求助

关联字段返回null + EntityNotFoundException 问题解决

问题背景

现有两个实体类ShippingOrder和Shipment,代码如下:

ShippingOrder实体类

@Entity
@Table(name="shipping_order")
@AllArgsConstructor
@NoArgsConstructor
@Getter
@Setter
@JsonIgnoreProperties({"hibernateLazyInitializer", "handler"})
public class ShippingOrder implements Serializable {
    @JsonManagedReference
    @ManyToOne(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
    @JoinColumn(name = "shipment_id")
    private Shipment shipment;
}

Shipment实体类

@Entity
@Table(name = "shipment")
@NoArgsConstructor
@AllArgsConstructor
@Getter
@Setter
@JsonIgnoreProperties({"hibernateLazyInitializer", "handler"})
public class Shipment implements Serializable {
    @JsonBackReference
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "shipping_order_id")
    private ShippingOrder shippingOrder;
}

遇到的问题:

  1. 关联字段获取数据时显示为null,但Hibernate拦截器中存在对应数据
  2. 获取AWB数据时抛出异常:
Method threw 'javax.persistence.EntityNotFoundException' exception. Cannot evaluate com.hk.admin.entities.Awb$HibernateProxy$EQfa2R8Q.toString()

解决方案

1. 修正双向关联的注解配置

当前两个实体都用@ManyToOne是错误配置,双向关联需一端用@OneToMany、另一端用@ManyToOne,否则会形成两个独立单向关联,导致Hibernate无法维护关联关系,出现null值。

修改示例:

  • Shipment类调整为@OneToMany,指定mappedBy指向关联字段:
@Entity
@Table(name = "shipment")
@NoArgsConstructor
@AllArgsConstructor
@Getter
@Setter
@JsonIgnoreProperties({"hibernateLazyInitializer", "handler"})
public class Shipment implements Serializable {
    @JsonBackReference
    @OneToMany(mappedBy = "shipment", fetch = FetchType.LAZY)
    private List<ShippingOrder> shippingOrders;
}
  • ShippingOrder类保持@ManyToOne,确认@JoinColumn的name对应数据库真实外键字段:
@Entity
@Table(name="shipping_order")
@AllArgsConstructor
@NoArgsConstructor
@Getter
@Setter
@JsonIgnoreProperties({"hibernateLazyInitializer", "handler"})
public class ShippingOrder implements Serializable {
    @JsonManagedReference
    @ManyToOne(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
    @JoinColumn(name = "shipment_id") // 确认该字段在shipping_order表中存在且关联shipment表主键
    private Shipment shipment;
}

2. 解决EntityNotFoundException异常

该异常通常因懒加载代理在Session关闭后访问,或数据库无对应AWB记录导致:

  • 维护Session生命周期:Spring环境下用@Transactional注解保证方法内Session持续打开,避免懒加载时Session已关闭。
  • 验证数据库数据:检查AWB表中是否存在关联ID对应的记录,避免关联不存在的数据。
  • 调整加载策略:如需立即加载AWB数据,可将关联字段的fetch改为FetchType.EAGER,或查询时用JOIN FETCH显式加载:
    // 示例JPQL查询
    String jpql = "SELECT so FROM ShippingOrder so JOIN FETCH so.shipment s JOIN FETCH s.awb WHERE so.id = :id";
    

3. 优化Json序列化配置

@JsonManagedReference和@JsonBackReference配置不当可能导致关联数据无法正常序列化,可替换为@JsonIdentityInfo处理循环引用:

@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "id")
@Entity
// 其他注解...
public class ShippingOrder implements Serializable {
    // 字段定义...
}

@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "id")
@Entity
// 其他注解...
public class Shipment implements Serializable {
    // 字段定义...
}

内容的提问来源于stack exchange,提问作者AasthaChaudhary

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.23 21:47:25