[英]Get oneToMany field from class with Hibernate
我想從數據庫中檢索實體的屬性列表,但出現以下異常:
org.hibernate.PropertyNotFoundException: no appropriate constructor in class MapClass
我的實體:
public class Entity{
//properties
@OneToMany(mappedBy = "user", cascade={CascadeType.ALL})
private List<Profile> profiles = new LinkedList<Profile>();
public Entity(){}
}
映射類:
public class MapClass{
//properties
private String name;
private List<Profile> profiles ;
public MapClass(String name,List<Profile> profiles){
this.name = name;
this.profiles = profiles;
}
}
我的SQL查詢:
String sql = "SELECT new MapClass(u.name,u.profiles) FROM Entity u";
return getList(MapClass.class,sql);
如果我從MapClass構造函數和查詢中刪除配置文件,我的查詢將起作用。 我所有的班級都有空的構造函數。
Hibernate需要一個構造函數來工作:將其添加到您的類中
public class Entity{
//add the constructor
public Entity(){ }
}
空構造函數是Hibernate的要求。 它在此構造函數上使用反射來實例化所需的對象。
//empty constructor
public Entity(){
}
//empty constructor
public Profile(){
}
在您的實體bean中創建空的構造函數,即。 標注為@Entity
類。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.