Hello Stack Overflow Experts, i need your expertice:

I'm attempting to use Hibernate with an Existing DB. Presently im attempting to load a person object and a listing of UserData objects which go along.

within the DB the (simplified) layout is

|    User      |      |          UserData               |
----------------      -----------------------------------
uid | username |      | uid | parentuid | field | value |

So each User object matches all of 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>

To date i've attempted a variety of designs and every one of them have experienced various levels of failue. The code copied and pasted here, doesn't work.

  • The parentuid rentals are said out, because Hibernate provides a "Repeated column in mapping" error otherwise.
  • Presently there's still a "Repeated column in mapping" around the uid area, i personally use for <list-index />
  • I don't understand where i specify that UserData.parentuid may be the foreign key which their email list should use User.uid as key.

I really hope someone has the capacity to help.

Do this (drycode):

<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" inverse="true">
    <key column="parentuid" not-null="true" />
    <one-to-many class="com.agetor.commons.fields.FieldData"/>
  </list>
</class>

<class name="com.agetor.commons.fields.FieldData" table="ac_userdef_data">
  <id name="uid" column="uid" type="long" >
    <!--<generator class="native"/> -->
  </id>
  <many-to-one name="user" class="com.agetor.commons.security.User" fetch="join">
     <column name="parentuid" not-null="true"/>
  </many-to-one>
  <property name="fieldname" column="fieldname" />
  <property name="value" column="value" />
</class>

Whether it works, try adding the index and cascade items that I omitted.

cheers,
mitch

Whenever you define both a 1-To-Many along with a Many-To-One, performs this not allow it to be Bi-Directional? The present working model, is Unidirectional and UserData doesn't have a mention of the User. Your suggestion fails, because Hibernate couldn't look for a get or set way of User on UserData.

Could it be implied that, this code uses User.uid like a key and matches this from the UserData.parentuid column? Or are these claims specified elsewhere?

  <list name="fieldData" inverse="true">
    <key column="parentuid" not-null="true" />
    <one-to-many class="com.agetor.commons.fields.FieldData"/>
  </list>

I'm still learning Hibernate and dealing all things in documentation and good examples i'm able to find.