Hi,
I'm trying to run an HQL query that should do the following :
I have a User. The User has a set of Interrest.
I want my query to find all the Users that share at least N Interests with the Person with id X.
Here is my person class :
Code:
public class User {
private Long id;
private Set<Interest> interests;
/**
* @return the id
*/
public Long getId() {
return id;
}
/**
* @param id
* the id to set
*/
public void setId(Long id) {
this.id = id;
}
/**
* @return the interests
*/
public Set<Interest> getInterests() {
return interests;
}
/**
* @param interests the interests to set
*/
public void setInterests(Set<Interest> interests) {
this.interests = interests;
}
}
Here is my Interest class :
Code:
import java.util.Set;
public class Interest {
private Long id;
private String name;
/**
* @return the id
*/
public Long getId() {
return id;
}
/**
* @param id the id to set
*/
public void setId(Long id) {
this.id = id;
}
/**
* @return the name
*/
public String getName() {
return name;
}
/**
* @param name the name to set
*/
public void setName(String name) {
this.name = name;
}
}
and here are the corresponding mappings
Code:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="model.User" table="user">
<id name="id" column="id">
<generator class="native"/>
<!--<generator class="sequence">
<param name="sequence">address_id_seq</param>
</generator>-->
</id>
<set name="interests">
<key column="userid"/>
<one-to-many class="model.Interest"/>
</set>
Code:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="model.Interest" table="interest">
<id name="id" column="id">
<generator class="native"/>
<!--<generator class="sequence">
<param name="sequence">address_id_seq</param>
</generator>-->
</id>
<property name="name" column="name"/>
</class>
</hibernate-mapping>
So a user can have many Interests.
I want to find all the users that have at least N interests in common with the User U.
I don't know if it's possible to do this with one simple HQL query or if should make multiple queries...
Any help appreciated
Thanks !