gpt4 book ai didi

java - Hibernate和Spring数据存储库,保存或更新后对象不完整

转载 作者:行者123 更新时间:2023-12-02 11:47:11 26 4
gpt4 key购买 nike

通过 Spring 数据存储库(扩展了 CrudRepository)保存或更新该实体的对象后:

@Entity
@Table(name="finance_data")
public class FinanceData implements Serializable {
private static final long serialVersionUID = 1L;

@Id
@Column(unique=true, nullable=false)
@GeneratedValue(strategy=GenerationType.IDENTITY)
private Integer id;

@OneToOne(fetch=FetchType.EAGER)
@JoinColumn(name="asset_type", nullable=false)
private AssetType assetType;

它仅返回 assetType 属性的 Id。这迫使我们在更新后执行 findOne 来获取完整信息。

我如何告诉 hibernate 返回整个对象?

最佳答案

It only returns the Id for the assetType property.

这是因为您自己的操作将所有其他字段设置为 null(如评论中所述):

When I try to save a new FinanceData record, I only set the reference id for property AssetType because they exist before the FinanceData is recorded.

不要只设置 Id 并覆盖所有其他字段,而是通过 Id 加载 AssetType 并使用它。

关于java - Hibernate和Spring数据存储库,保存或更新后对象不完整,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48116528/

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