Hi,
I've got a many to many structure between a person and relationship as follows:
Code:
@Entity
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name = "PARTY_ROLE_TYPE_ID", discriminatorType = DiscriminatorType.STRING)
public abstract class Person {
/** The unique ID for the entity */
@Id
@GeneratedValue
private Long id;
@ManyToMany(cascade = {CascadeType.PERSIST, CascadeType.MERGE},
fetch = FetchType.EAGER)
@JoinTable(name = "PERSON_RELATIONSHIPS",
joinColumns = @JoinColumn(name = "PERSON_ID"),
inverseJoinColumns = @JoinColumn(name = "RELATIONSHIP_ID"))
private Set<Relationship> relationships;
....
}
@Entity
@Table(name = "RELATIONSHIPS")
public class Relationship {
/* The unique identifier for a relationship */
@Id
@GeneratedValue
@Column(name = "RELATIONSHIP_ID")
private Long id;
@Column(name = "TYPE")
private String type;
@ManyToMany(cascade = {CascadeType.PERSIST, CascadeType.MERGE},
mappedBy = "relationships")
private Set<Person> parties;
...
}
I need to create a query that returns all relationships that contain all given people - this is a query which I run before creating a new relationship to prevent creating relationship duplicates.
I have both Hibernate in Action and Java Persistence with Hibernate but haven't managed to find a section where this type of query is covered.
I have tried creating multiple joins to the collection as you might do in SQL as follows:
Code:
session.createQuery("select r from Relationship r join r.people p where p = ? join r.people p2 where p2.id = ?");
but this fails to parse.
I'm fairly new to hibernate so I'm guessing that I've missed something however looking in the books and Google searches hasn't helped yet.
For reference I'm using: Jboss 4.2.1 with the bundled Hibernate 3.2.4 and Java 5. Our test database is Hypersonic but will move to Oracle 10 when development is complete.
Many thanks.
Stephen