![](/img/trans.png)
[英]The ForeignKey jpa/hibernate annotation does not work as exptected
[英]Does Spring Data JPA's @PersistenceConstructor annotation work in combination with Hibernate?
我希望 Hibernate 使用另一個構造函數而不是空構造函數,因為我有一些邏輯應該在對象創建時執行但取決於對象屬性。 我在這里讀到@PersistenceConstructor
解決了這個問題。
我創建了這個示例實體:
@Entity
public class TestEntity
{
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@JsonIgnore
public final Long id;
public final int width;
public final int height;
@Transient
private final double area;
@PersistenceConstructor
public TestEntity(Long id, int width, int height)
{
this.id = id;
this.width = width;
this.height = height;
this.area = width * height;
}
public double getArea()
{
return this.area;
}
public interface TestEntityRepository extends CrudRepository<TestEntity, Long>
{
}
}
但是,當我嘗試從數據庫中檢索實例時,出現以下異常:
org.hibernate.InstantiationException: No default constructor for entity
我做錯了什么或者@PersistenceConstructor
注釋在這種情況下不起作用嗎?
Spring Data @PersistenceConstructor
不適用於 JPA。 它僅適用於不使用底層數據存儲的對象映射的模塊。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.