Joined: Thu Jun 19, 2014 10:12 am Posts: 1
|
For some reason my many-to-many mapping is not working from both sides. Here is the outline of my setup -
I have the following database tables - 1. accounts with acc_id as primary key 2. custom_field_list with cf_id as primary key 3. custom_field_mapping with cf_id_map and acc_id_map as primary key
Java classes:-
1. Account with a getCustomFields() method 2. CustomField with a getAccounts() method
Here are the Hibernate mappings for the 2 classes -
<hibernate-mapping> <class name="Account" table="accounts"> <id name="id" column="acc_id"> <generator class="native"/> </id>
<set name="customFields" table="CUSTOM_FIELD_MAPPING" cascade="all"> <key column="acc_id_map" /> <many-to-many column="cf_id_map" class="CustomField" /> </set> </class> </hibernate-mapping>
<hibernate-mapping> <class name="CustomField" table="CUSTOM_FIELD_LIST"> <id name="id" column="CF_ID"> <generator class="native"/> </id>
<set name="accounts" table="CUSTOM_FIELD_MAPPING" cascade="all"> <key column="cf_id_map" /> <many-to-many column="acc_id_map" class="Account"/> </set>
</class> </hibernate-mapping>
Data is getting setup correctly in the db.
The issue that I am facing that when I access the customFields from Account instance by using the the following code - List accs = session.createQuery("FROM Account").list();
things work i.e the getCustomFields() method on the Account class returns the list of custom field objects.
However when I access the accounts from the CustomField instance by using the following code -
List customFields = session.createQuery("FROM CustomField").list();
things do not work i.e the getAccounts() method on the CustomField class does not return the list of account objects.
In other words, the many-to-many mapping is working from one side. Let me know if I have setup anything incorrectly.
|
|