繁体   English   中英

从Hibernate的课堂上获得oneToMany字段

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM