views:

3067

answers:

5

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.

A: 

Try 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>

If it works, try adding the index and cascade stuff that I left out.

cheers,
mitch

mitchnull
A: 

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.

JesperGJensen
A: 

I think you're close, but List is probably not the data structure you want in your new User class. A list implies an ordering of the child elements, which the tables don't seem to have. If you have an indexed collection like a list, the data has to have a separate column that provides the index, hence the "repeated column in mapping" issue.

The rules of thumb I use are - each mapping should include an entry for each member of the class it is mapping. So for instance your User class should have an entry for its collection of FieldData, but your FieldData mapping does not need an entry for parentuid, since that column doesn't map to an element of the FieldData class - it's just used to put the FieldData within the collection in the parent object.

If the User object really does contain an ordered collection of FieldData, then use a set instead. On the other hand, I think it's more likely you'd want a map of FieldData instead, using the 'field' name as the index. Then the FieldData class doesn't need to map the 'field' column and won't have that as a member, it'll just have the value (and any other fields from this table - of course if there really is only one column left to map, you might just end up with a map of strings to strings.

Chris
A: 

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.

    <class name="com.agetor.commons.security.User" table="ac_users">
    <id name="uid" column="uid" type="long" >
        <generator class="increment"/>
    </id>
    <property name="deleted" column="deleted" />
    <property name="username" column="username" />   
    <property name="password" column="passwd" />
    <property name="disabled" column="disabled" />
    <property name="lockout" column="lockout" />
    <property name="expires" column="expires" />
    <bag name="fieldData" lazy="extra">
  <key column="parentuid" not-null="true" />
  <one-to-many class="com.agetor.commons.fields.FieldData"/>
 </bag>
    <bag name="groups" table="ac_group_rel" access="field" lazy="extra">
        <key column="useruid"/>         
        <many-to-many column="groupuid" class="com.agetor.commons.security.Group"/>
    </bag>     
 <join table="ac_userdef_data" optional="true" fetch="join">
  <subselect>
   select
    *
   from
    ac_userdef_data data
   where
    data.objectname = 'user' and
    data.fieldname = 'firstname'
  </subselect>
  <key column="parentuid" />
  <property name="firstname" formula="(select data.value from ac_userdef_data data where data.fieldname = 'firstname' and data.uid = uid)"/>
 </join>
 <join table="ac_userdef_data" optional="true" fetch="join">
  <subselect>
   select
    *
   from
    ac_userdef_data data
   where
    data.objectname = 'user' and
    data.fieldname = 'lastname'
  </subselect>  
  <key column="parentuid" />
  <property name="lastname" formula="(select data.value from ac_userdef_data data where data.fieldname = 'lastname' and data.uid = uid)"/>
 </join>

</class>
JesperGJensen
A: 

-->

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

Try this , i just changed key coloumn as parentuid to uid.(And better use set in hibernate mappings)

cheers, Nagendra Prasad..