从Hibernate的课堂上获得oneToMany字段

时间:2018-12-11 14:40:00

标签: java hibernate one-to-many

我想从数据库中检索实体的属性列表,但出现以下异常:

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构造函数和查询中删除配置文件,我的查询将起作用。我所有的班级都有空的构造函数。

2 个答案:

答案 0 :(得分:0)

Hibernate需要一个构造函数才能工作:将其添加到您的类中

public class Entity{

    //add the constructor
    public Entity(){ }

}

答案 1 :(得分:0)

空构造函数是Hibernate的要求。它在此构造函数上使用了一个反射来实例化所需的对象。

   //empty constructor
    public Entity(){

    }

  //empty constructor
    public Profile(){

    }

在您的实体bean中创建空的构造函数,即。标注为@Entity的类。

相关问题