开发者社区> 问答> 正文

关于hibernate指定查询集合字段的方法?报错

我们都知道hibernate可以这样通过一个构造器去查询指定列


public Channel(int id, String name) {

   this.id = id;
   this.name = name;

}



@Override
	public List<Channel> queryById(int id) {
		String hql = "select new Channel(channel.id, channel.name) from Channel as channel where channel.parent.id=:id";
		Query query = super.getSession().createQuery(hql);
		query.setParameter("id", id);
		
		return query.list();
	}


  但假如我的Channel实体里面有一个set属性呢?也就是一对多的时候,比如像下面这样


public class Channel implements Serializable {

	private static final long serialVersionUID = 1L;

	private int id;
	private String name;
	private Channel parent;
	private String screening;
	private Set<Channel> children = Sets.newHashSet();

这是我的实体类,如果现在我只需要用HQL指定查询id name parent 和 children, 我不需要查询 screening的时候,请问我该怎么写?如果我的HQL写成

select new Channel(channel.id, channel.name, channel.parent, channel.children) from Channel
然后给它这个指定查询添加一个包含有这几个字段的构造器,查询的时候是会报错的,不知道有没有解决的方法



展开
收起
爱吃鱼的程序员 2020-06-22 19:06:13 543 0
1 条回答
写回答
取消 提交回答
  • https://developer.aliyun.com/profile/5yerqm5bn5yqg?spm=a2c6h.12873639.0.0.6eae304abcjaIB

    同问

    2020-06-22 19:06:31
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
低代码开发师(初级)实战教程 立即下载
冬季实战营第三期:MySQL数据库进阶实战 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载