1. 程式人生 > >Hibernate出現No default constructor for entity: : com.demo.dataobject.ProductInfo的解決方法

Hibernate出現No default constructor for entity: : com.demo.dataobject.ProductInfo的解決方法

【問題】

在做junit測試的時候,為了方便,給object加上了帶參的建構函式,於是出現了報錯:

org.springframework.orm.jpa.JpaSystemException: No default constructor for entity:  : com.demo.dataobject.ProductInfo; nested exception is org.hibernate.InstantiationException: No default constructor for entity:  : com.demo.dataobject.ProductInfo

報錯說的很明顯:實體類沒有預設的建構函式。這是怎麼回事呢?查閱得知:

The no-argument constructor, which is also a JavaBean convention, is a requirement for all persistent classes. Hibernate needs to create objects for you, using Java Reflection. 

Hibernate利用反射為你建立物件的時候,所有的持久化類都必須需要無參的建構函式。

【解決】

當自定義了建構函式時候,需要顯示的宣告預設的建構函式。