即使在要保存的对象中的字段中未null时,Hibernate也会插入null

发布于 2025-01-25 02:21:00 字数 1226 浏览 2 评论 0原文

我正面临一个奇怪的问题,即使在Java对象中设置了所有字段,但当我保存对象Hibernate试图将NULL值插入字段中时。

当我进一步调试时,我看到在合并新实体时,请 this 行hibernate生成一个空对象并设置为目标,而不是设置给定实体的目标。这会导致插入无效值的插入查询。

我在这里错过了一些配置吗?以下是具有与我案件相似的关联的示例实体。

class Vehicle {
    
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Integer id;

    @Column(nullable = false)
    @Enumerated(EnumType.STRING)
    @EqualsAndHashCode.Include
    private VehicleType vehicleType;

    @OneToOne(mappedBy="vehicle", fetch=FetchType.LAZY)
    private Car car;

    @OneToOne(mappedBy="vehicle", fetch=FetchType.LAZY)
    private Truck truck;
}

class Car {
    @Id
    private Integer id;
    
    @OneToOne(fetch = FetchType.LAZY, optional = false)
    @MapsId
    @JoinColumn(name = "vehicle_id")
    private Vehicle vehicle;

    ...
}

class Truck {

    @Id
    private Integer id;
    
    @OneToOne(fetch = FetchType.LAZY, optional = false)
    @MapsId
    @JoinColumn(name = "vehicle_id")
    private Vehicle vehicle;

    ...
}

I am facing a weird issue where even though all fields are set in the java object, when I save the object hibernate tries to insert null values in the fields.

When I further debugged, I saw that while merging the new entity at this line hibernate generates an empty object and sets to the target instead of setting given entity to the target. This results in insert query with null values.

Am I missing some configuration here? Below are the example entities having associations similar to my case.

class Vehicle {
    
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Integer id;

    @Column(nullable = false)
    @Enumerated(EnumType.STRING)
    @EqualsAndHashCode.Include
    private VehicleType vehicleType;

    @OneToOne(mappedBy="vehicle", fetch=FetchType.LAZY)
    private Car car;

    @OneToOne(mappedBy="vehicle", fetch=FetchType.LAZY)
    private Truck truck;
}

class Car {
    @Id
    private Integer id;
    
    @OneToOne(fetch = FetchType.LAZY, optional = false)
    @MapsId
    @JoinColumn(name = "vehicle_id")
    private Vehicle vehicle;

    ...
}

class Truck {

    @Id
    private Integer id;
    
    @OneToOne(fetch = FetchType.LAZY, optional = false)
    @MapsId
    @JoinColumn(name = "vehicle_id")
    private Vehicle vehicle;

    ...
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

百合的盛世恋 2025-02-01 02:21:00

我遇到了同样的问题,就我的情况而言,我有一个应用程序:

 public class Claim extends BaseEntity<Integer> implements Serializable {
      
      @OneToOne(cascade = CascadeType.ALL)
      @JoinColumn(name = "claimdetailsid", referencedColumnName = "id")
      private ClaimDetails claimDetails;
    
      @OneToOne(cascade = CascadeType.PERSIST)
      @JoinColumn(name = "beneficiaryid", referencedColumnName = "id")
      private Beneficiary beneficiary;

      ....
}

当我保存索赔实体时,索赔和索赔对象被正确插入。除了ID和创建日期外,其他实体除所有字段无效。

我尝试将CascadeType更改为CascadeType.All,这解决了我的插入问题。

但是删除级联现在不起作用。

I encountered the same problem, in my case I have an application with:

 public class Claim extends BaseEntity<Integer> implements Serializable {
      
      @OneToOne(cascade = CascadeType.ALL)
      @JoinColumn(name = "claimdetailsid", referencedColumnName = "id")
      private ClaimDetails claimDetails;
    
      @OneToOne(cascade = CascadeType.PERSIST)
      @JoinColumn(name = "beneficiaryid", referencedColumnName = "id")
      private Beneficiary beneficiary;

      ....
}

When I saved the Claim entity, the Claim and ClaimDetails objects were inserted correctly. The other entities had all the fields null, except the id and the creation date.

I tried changing CascadeType.PERSIST to CascadeType.ALL, that solved my insert problem.

But the delete cascade doesn't work now.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文