为了账号安全,请及时绑定邮箱和手机立即绑定

使用 Hibernate 从类中获取 oneToMany 字段

使用 Hibernate 从类中获取 oneToMany 字段

aluckdog 2022-03-10 11:00:37
我想从数据库中检索实体的属性列表,但出现以下异常: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 回答

?
饮歌长啸

TA贡献1951条经验 获得超3个赞

Hibernate 需要一个构造函数来工作:将它添加到你的类中


public class Entity{


    //add the constructor

    public Entity(){ }


}


查看完整回答
反对 回复 2022-03-10
?
蝴蝶不菲

TA贡献1810条经验 获得超4个赞

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


   //empty constructor

    public Entity(){


    }


  //empty constructor

    public Profile(){


    }

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


查看完整回答
反对 回复 2022-03-10
  • 2 回答
  • 0 关注
  • 150 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信