Hibernate DetachedCriteria 查询set 部类的字段
Hibernate DetachedCriteria 查询set 类型的字段DetachedCriteria detachedCriteriaDetachedCriteria.for
Hibernate DetachedCriteria 查询set 类型的字段
DetachedCriteria detachedCriteria=DetachedCriteria.forClass("Users");detachedCriteria.createAlias("challenges","c").add(Restrictions.eq("c.challengeid", "查询值"));
我查询的网页:
http://topic.csdn.net/u/20090616/10/3e8d5f18-7144-4248-85a4-41bf5c4e3903.html
http://stackoverflow.com/questions/1987471/nhibernate-exclude-a-property-from-a-projection-list-select-clause
http://topic.csdn.net/u/20080225/13/9eb2fbf7-117b-41f4-84d6-4b5461780388.html
希望能帮助你
public class Users implements java.io.Serializable {// Fieldsprivate Long id;private String userName;private Set challenges = new HashSet(0); ......}
表记录:
id? user_name
1 小明
2 小强
2、Challenge实体
public class Challenge implements java.io.Serializable {// Fieldsprivate Long challengeid;private Users users;private String challengeName; ......}
表记录:
id challenge_name user_id
1?????? 挑战1????? 1
2?????? 挑战2????? 1
3?????? 挑战3????? 2
3、根据challengeid查询Challenge对象,再关联出相应的users信息
// 查询条件DetachedCriteria detachedCriteria = DetachedCriteria.forClass(Challenge.class);detachedCriteria.add(Restrictions.eq("challengeid", new Long(1)));Criteria criteria = detachedCriteria.getExecutableCriteria(session);// 查询List resultList = criteria.list();if (resultList != null && resultList.size() > 0) { for (int i = 0; i < resultList.size(); i++) { Challenge challenge = (Challenge) resultList.get(i); // 打印出challenge所关联的users信息 System.out.println(challenge.getUsers().getUserName()); }}
结果输出:
小明
4、根据challengeName模糊查询查询Challenge对象,再关联出相应的users信息
// 查询条件DetachedCriteria detachedCriteria = DetachedCriteria.forClass(Challenge.class);detachedCriteria.add(Restrictions.like("challengeName", "%" + "挑战" + "%"));Criteria criteria = detachedCriteria.getExecutableCriteria(session);// 查询List resultList = criteria.list();if (resultList != null && resultList.size() > 0) { for (int i = 0; i < resultList.size(); i++) { Challenge challenge = (Challenge) resultList.get(i); // 打印出challenge所关联的users信息 System.out.println(challenge.getUsers().getUserName()); }}
结果输出:
小明
小明
小强
5、根据users查询,查询小明对应的所有Challenge信息
// 查询条件DetachedCriteria detachedCriteria = DetachedCriteria.forClass(Users.class);detachedCriteria.add(Restrictions.eq("userName", "小明"));Criteria criteria = detachedCriteria.getExecutableCriteria(session);// 查询List resultList = criteria.list();if (resultList != null && resultList.size() > 0) { Set set = ((Users) resultList.get(0)).getChallenges(); // 循环打印出小明对应的所有Challenge信息 for (Iterator it = set.iterator(); it.hasNext();) { Challenge challenge = (Challenge) it.next(); System.out.println(challenge.getChallengeName()); }}
结果输出:
挑战2
挑战1
// 查询条件DetachedCriteria detachedCriteria = DetachedCriteria.forClass(Users.class);detachedCriteria.createAlias("challenges", "cs");detachedCriteria.add(Restrictions.like("cs.challengeName", "%" + "挑战" + "%"));Criteria criteria = detachedCriteria.getExecutableCriteria(session);// 查询List resultList = criteria.list();if (resultList != null && resultList.size() > 0) { for (int i = 0; i < resultList.size(); i++) { Users user = (Users) resultList.get(i); System.out.println(user.getUserName()); }}
输出结果:
小明
小明
小强
重复结果可以再过滤,这个应该是你想要的查询方式
select t2.property1, t2.property2, ... from table1 as t1 inner join t1.table2 as t2select t1.table2.property1, t1.table2.property2, ... from table1 as t1
??