Hibernate version: 3.1.3
I have a many-to-many mapping between my domain classes Organization and Person.
Because i need an additional attribute in their association table i use a composite-element with a nested many-to-one mapping (see below for mapping files).
I want to build a query to retrieve all persons for a certain organization_id. But now i'm really lost how to form the query with hql syntax, especially in regard to the composite-element.
I would be very glad for any help from experienced hibernate developers!
Thanks in advance!
Mapping documents:
Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="mypackage">
<class name="Organization" table="organizations">
<id name="organization_id" type="long">
<column name="organization_id"/>
<generator class="native"/>
</id>
<set name="personOrganizationComponents"
table="person_organization_components"
cascade="save-update">
<key>
<column name="organization_id" not-null="true"/>
</key>
<composite-element class="PersonOrganizationComponent" >
<many-to-one name="person"
column="person_id"
class="Person"
not-null="true"
cascade="save-update"/>
<property name="someAttribute"
column="some_attribute"
type="string"
not-null="true"/>
</composite-element>
</set>
</class>
</hibernate-mapping>
Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="mypackage">
<class name="Person" table="persons">
<id name="person_id" type="long">
<column name="person_id"/>
<generator class="native"/>
</id>
<property name="firstname" column="firstname" type="string"/>
<property name="lastname" column="lastname" type="string"/>
<property name="gender" column="gender" type="character"/>
</class>
</hibernate-mapping>
Code:
public class PersonOrganizationComponent implements Serializable {
private String attribute;
private Organization organization;
private Person person;
//getter + setter here
}
Code:
public class Organization implements Serializable {
private long organization_id;
private Set<PersonOrganizationComponent> personOrganizationComponents = new HashSet<PersonOrganizationComponent>();
//getter + setter here
}