![](/img/trans.png)
[英]Update a Hibernate Repository Entry after initial save in Spring Boot
[英]Hibernate and spring data repository, object incomplete after save or update
在通過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以獲得完整信息。
我如何告訴休眠返回整個對象?
它僅返回
assetType
屬性的ID。
這是因為所有其他字段都由您自己的操作設置為null
(如注釋中所述):
當我嘗試保存新的FinanceData記錄時,我僅設置屬性
AssetType
的引用ID,因為它們在記錄FinanceData
之前就已經存在。
不僅要設置Id
從而覆蓋所有其他字段,還AssetType
Id
加載並使用AssetType
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.