Hello Stack Overflow Experts, i have need of your expertice:
I am trying to use Hibernate on an Existing DB. Currently im trying to load a User object and a list of UserData objects that go along.
in the DB the (simplified) layout is
| User | | UserData | ---------------- ----------------------------------- uid | username | | uid | parentuid | field | value |
So each User object matches all the UserData objects where UserData.parentuid = User.uid.
My User class mapping file
<class name='com.agetor.commons.security.User' table='ac_users'> <id name='uid' column='uid' type='long' > <!--<generator class='native'/>--> </id> <property name='username' column='username' /> <list name='fieldData' cascade='all'> <key column='parentuid' not-null='true' /> <index column='parentuid' /> <one-to-many class='com.agetor.commons.fields.FieldData'/> </list> </class>
Mu UserData mapping file
<class name='com.agetor.commons.fields.FieldData' table='ac_userdef_data'> <id name='uid' column='uid' type='long' > <!--<generator class='native'/> --> </id> <!--<property name='parentuid' column='parentuid' /> --> <property name='fieldname' column='fieldname' /> <property name='value' column='value' /> </class>
So far i have tried many different configurations and all of them have had various degrees of failue. The code pasted here, does not work.
- The parentuid property is commented out, because Hibernate gives a ‘Repeated column in mapping’ error otherwise.
- Currently there is still a ‘Repeated column in mapping’ on the uid field, i use for
<list-index /> - I do not understand where i specify that UserData.parentuid is the foreign key and that the list should use User.uid as key.
I hope someone is able to help.
When you define both a One-To-Many and a Many-To-One, does this not make it Bi-Directional? The current working model, is Unidirectional and UserData does not have a reference to User. Your suggestion fails, because Hibernate could not find a get or set method for User on UserData.
Is it implied that, this code uses User.uid as a key and matches this against the UserData.parentuid column? Or is this fact specified somewhere else?
<list name='fieldData' inverse='true'> <key column='parentuid' not-null='true' /> <one-to-many class='com.agetor.commons.fields.FieldData'/> </list>
I am still learning Hibernate and working my way through documentation and examples i can find.
This is the latest development on this problem. This configuration can succesfully load from the Database. Saving does not work. I have decided to alter the Database design instead, so im posting this here for reference for others, before i abandon this approach.