Hello
My problem is how to configure a many-to-many relation between 2 tables with an associative table with composite keys.
Here's my Database schema (simplified)
Code:
AREA , DOC and ROLE are tables with an ID and a VALUE (ie ID_AREA and VALUE_AREA for example)
the Table ROLE_NAME have for PK the 3 PK of the previous tables
ID_AREA
ID_DOC
ID_ROLE
VALUE_ROLE_NAME
Table SECURITY
ID_SEC (not ID_SECURITY !!)
VALUE_SECURITY
Table SECURITYGROUP
ID_AREA
ID_DOC
ID_ROLE
ID_SECURITY
Here the mapping for the ROLE NAME table
Code:
<hibernate-mapping>
<class name="Role_Name" table="ROLE_NAME">
<composite-id>
<key-many-to-one name="area" class="Area" column="ID_AREA" lazy="false"></key-many-to-one>
<key-many-to-one name="doc" class="Doc" column="ID_DOC" lazy="false"></key-many-to-one>
<key-many-to-one name="role" class="Role" column="ID_ROLE" lazy="false"></key-many-to-one>
</composite-id>
<property name="value_role_name" column="VALUE_ROLE_NAME" type="string"/>
<set name="securityGroups" table="SECURITYGROUP" fetch="select" lazy="false">
<key>
<column name="ID_AREA"></column>
<column name="ID_DOC"></column>
<column name="ID_ROLE"></column>
</key>
<many-to-many column="ID_SECURITY" class="Security" fetch="select" lazy="false" />
</set>
</class>
</hibernate-mapping>
the corresponding java class
Code:
public class Role_Name implements Serializable{
private Area area;
private Role role;
private Doc doc;
private String value_role_name;
private Set<Security> security = new HashSet<Security>(0);
+ Getters/Setters
}
My problem is that the securityGroups Set is not "loaded" (all other properties are OK) when I load Role_Name like that : For example,
Code:
Role_Name roleName = new RoleNameBDR();
Area a = new Area(1); // Contructor with ID
roleName.setArea(a);
Doc d = new Doc(1); // Contructor with ID
roleName.setDoc(d);
Role r = new Role(4); // Contructor with ID
roleName.setRole(r);
Criteria criteria = getSession().createCriteria(Role_Name.class).add(
Expression.eq("area", rn.getArea())).add(
Expression.eq("doc", rn.getDoc())).add(
Expression.eq("role", rn.getRole()));
Role_Name result = (Role_Name) criteria.uniqueResult();
What I'm doing wrong ? Is it the good way to that ?
I think that the following is not correct.
Code:
<many-to-many column="ID_SECURITY" class="Security" fetch="select" lazy="false" />
In all examples found on the web, the key name was the same for the column parameter (ie PK of SECURITY table and ID_SECURITY of SECURITYGROUP).
Is there a param to initialize ? I've tried foreign-key, property-ref but no success.
I precise that I cannot change the database schema (tables are already used)...but I made a test : change ID_SECURITY name by ID_SEC in the test database (table SECURITYGROUP) and update the mapping file like that
Code:
<many-to-many column="ID_SEC" class="Security" fetch="select" lazy="false" />
It works !! but in Production it's not possible to do so...
Help would be appreciated
thanks