Hibernate DetachedCriteria 查询set 类型的字段

Hibernate DetachedCriteria 查询set 类型的字段

例:

publicclassUsers{

privateSetchallenges=newHashSet(0);

}

publicclassChallenge{

privateIntegerchallengeid;

privateUsersusers;

}

User和Challenge是一对多的关系

现在想根据Users中的challenges.challengeid的某一个属性查询

challenges是一个属性

<script type="text/javascript"></script>
DetachedCriteria detachedCriteria=DetachedCriteria.forClass("Users");   

detachedCriteria.createAlias("challenges","c").add(Restrictions.eq("c.challengeid", "查询值"));  
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

希望能帮助你
1、Users实体
public class Users implements java.io.Serializable {   
  

    // Fields    
  

    private Long id;    

    private String userName;    

    private Set challenges = new HashSet(0);    
  
    ......   
  
}  
public class Users implements java.io.Serializable {

	// Fields

	private Long id;
	private String userName;
	private Set challenges = new HashSet(0);

    ......

}

表记录:

iduser_name

1小明

2小强

2、Challenge实体

public class Challenge implements java.io.Serializable {   
  

    // Fields    
  

    private Long challengeid;    

    private Users users;    

    private String challengeName;    
  
    ......   
  
}  
public class Challenge implements java.io.Serializable {

	// Fields

	private Long challengeid;
	private Users users;
	private String challengeName;

    ......

}

表记录:

idchallenge_nameuser_id

1挑战11

2挑战21

3挑战32

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());   
    }   
}  
// 查询条件
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());   
    }   
}  
// 查询条件
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());   
    }   
}  
// 查询条件
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());   
    }   
}  
// 查询条件
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());
    }
}

输出结果:

小明

小明

小强

重复结果可以再过滤,这个应该是你想要的查询方式

7、为了避免resultList中的重复记录, 可以转换为Set集合, Set中存放的记录不允许相同。

以下是对第6的结果进行去重复记录

Java代码 Hibernate DetachedCriteria 查询set 类型的字段
Set resultSet = new HashSet();   

// 添加以后自动去掉重复记录    
resultSet.addAll(resultList);   
  

// 循环打印出小明对应的所有Challenge信息    

for (Iterator it = resultSet.iterator(); it.hasNext();) {    
    Users user = (Users) it.next();   
    System.out.println(user.getUserName());   
}  

<hibernate-mapping>
  <class table="table1">
    <set name="table2" table="table2" lazy="true" cascade="all">
      <key column="result_id"/>
      <many-to-many column="group_id"/>
    </set>
  </class>
</hibernate-mapping>


select t2.property1, t2.property2, ... 
    from table1 as t1
    inner join t1.table2 as t2
select t1.table2.property1, t1.table2.property2, ... from table1 as t1