hibernate中三张一对多的关系表查询怎么写?我用criteria写了老是报错,告诉帮忙看看,解决后给高分
有三张表:ResourcesBooking(多对一)resources(多对一)libraryBranches,现在三张表中各有一个栏位是条件,我用Criteria去做的...
有三张表:ResourcesBooking(多对一)resources(多对一)libraryBranches,现在三张表中各有一个栏位是条件,我用Criteria去做的:代码如下:
Criteria criteria = this.getSession().createCriteria(ResourcesBooking.class);
criteria.add(Restrictions.betwee("bookingstartdate", resourcesBook.getBookingstartdate(), resourcesBook.getBookingenddate()));
criteria.add(Restrictions.in("applystate", state));
criteria.add(Restrictions.eq("resources.resourcesno", resourcesBook.getResources().getResourcesno()));
criteria.add(Restrictions.eq("resources.libraryBranches.code", resourcesBook.getResources().getLibraryBranches().getCode()));
List<ResourcesBooking> list=criteria.list();
执行到最后一个criteria.add的时候就报
org.hibernate.QueryException: could not resolve property: resources.LibraryBranches.code of: com.claridy.ils.service.moduleName.domain.ResourcesBooking
at org.hibernate.persister.entity.AbstractPropertyMapping.propertyException(AbstractPropertyMapping.java:81)
at org.hibernate.persister.entity.AbstractPropertyMapping.toColumns(AbstractPropertyMapping.java:96)
at org.hibernate.persister.entity.BasicEntityPropertyMapping.toColumns(BasicEntityPropertyMapping.java:62)
at org.hibernate.persister.entity.AbstractEntityPersister.toColumns(AbstractEntityPersister.java:1457)
at org.hibernate.loader.criteria.CriteriaQueryTranslator.getColumns(CriteriaQueryTranslator.java:483)
at org.hibernate.loader.criteria.CriteriaQueryTranslator.findColumns(CriteriaQueryTranslator.java:498)
at org.hibernate.criterion.SimpleExpression.toSqlString(SimpleExpression.java:68)
我认真检查过了,对象名称绝对没有问题。不知道是不是关联三张表criteria不能做啊?
如果可以提供HQL语句的也可以,谢谢了! 展开
Criteria criteria = this.getSession().createCriteria(ResourcesBooking.class);
criteria.add(Restrictions.betwee("bookingstartdate", resourcesBook.getBookingstartdate(), resourcesBook.getBookingenddate()));
criteria.add(Restrictions.in("applystate", state));
criteria.add(Restrictions.eq("resources.resourcesno", resourcesBook.getResources().getResourcesno()));
criteria.add(Restrictions.eq("resources.libraryBranches.code", resourcesBook.getResources().getLibraryBranches().getCode()));
List<ResourcesBooking> list=criteria.list();
执行到最后一个criteria.add的时候就报
org.hibernate.QueryException: could not resolve property: resources.LibraryBranches.code of: com.claridy.ils.service.moduleName.domain.ResourcesBooking
at org.hibernate.persister.entity.AbstractPropertyMapping.propertyException(AbstractPropertyMapping.java:81)
at org.hibernate.persister.entity.AbstractPropertyMapping.toColumns(AbstractPropertyMapping.java:96)
at org.hibernate.persister.entity.BasicEntityPropertyMapping.toColumns(BasicEntityPropertyMapping.java:62)
at org.hibernate.persister.entity.AbstractEntityPersister.toColumns(AbstractEntityPersister.java:1457)
at org.hibernate.loader.criteria.CriteriaQueryTranslator.getColumns(CriteriaQueryTranslator.java:483)
at org.hibernate.loader.criteria.CriteriaQueryTranslator.findColumns(CriteriaQueryTranslator.java:498)
at org.hibernate.criterion.SimpleExpression.toSqlString(SimpleExpression.java:68)
我认真检查过了,对象名称绝对没有问题。不知道是不是关联三张表criteria不能做啊?
如果可以提供HQL语句的也可以,谢谢了! 展开
2个回答
展开全部
从最上面查下去
ResourcesBooking 把它设置成Criteria criteria = this.getSession().createCriteria(ResourcesBooking.class);
criteria.create("resources","r");
criteria.add(Restrictions.in("r.字段", “值”));
criteria.add("r.libraryBranches.字段",值");
这样就是三个关系了
ResourcesBooking 把它设置成Criteria criteria = this.getSession().createCriteria(ResourcesBooking.class);
criteria.create("resources","r");
criteria.add(Restrictions.in("r.字段", “值”));
criteria.add("r.libraryBranches.字段",值");
这样就是三个关系了
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
Criteria 换个思路以libraryBranches去查
举个例子
Class ResourcesBooking{
Integer id;
String bookingstartdate;
Set<Resource> resources;
}
Class Resource{
Integer id;
String resourcesno;
ResourcesBooking rs;
Set<LibraryBranche> libraryBranches;
}
Class LibraryBranche{
Integer id;
Resource rc;
String code;
}
String hql = “select ResourcesBooking from ResourcesBooking,Resource,LibraryBranche where ResourcesBooking.id = Resource.rs.id and Resource.id = LibraryBranche.rc.id and
ResourcesBooking.bookingstartdate = :0 and Resource.resourcesno = :1 and LibraryBranche.code = :2”;
举个例子
Class ResourcesBooking{
Integer id;
String bookingstartdate;
Set<Resource> resources;
}
Class Resource{
Integer id;
String resourcesno;
ResourcesBooking rs;
Set<LibraryBranche> libraryBranches;
}
Class LibraryBranche{
Integer id;
Resource rc;
String code;
}
String hql = “select ResourcesBooking from ResourcesBooking,Resource,LibraryBranche where ResourcesBooking.id = Resource.rs.id and Resource.id = LibraryBranche.rc.id and
ResourcesBooking.bookingstartdate = :0 and Resource.resourcesno = :1 and LibraryBranche.code = :2”;
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询