简体   繁体   English

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

[英]Hibernate and spring data repository, object incomplete after save or update

After saving or updating an object of this entity through a spring data repository (which extends CrudRepository): 在通过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;

It only returns the Id for the assetType property. 它仅返回assetType属性的Id This is forcing us to execute a findOne after the update to get the full information. 这迫使我们在更新后执行findOne以获得完整信息。

How can I tell to hibernate to return the whole object? 我如何告诉休眠返回整个对象?

It only returns the Id for the assetType property. 它仅返回assetType属性的ID。

This is because all the other fields are set to null by your own action (as described in the comments): 这是因为所有其他字段都由您自己的操作设置为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. 当我尝试保存新的FinanceData记录时,我仅设置属性AssetType的引用ID,因为它们在记录FinanceData之前就已经存在。

Instead of just setting the Id and thereby overwriting all other fields, load the AssetType by Id and use that. 不仅要设置Id从而覆盖所有其他字段,还AssetType Id加载并使用AssetType

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM