本文介绍了在现有数据库上休眠一对多单向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好堆栈溢出专家,我需要你的专家:



我想在现有的数据库上使用Hibernate。
目前我试图加载一个User对象和一个UserData对象列表。



在DB中,(简化)布局是

  |用户| | UserData | 
---------------- ------------------------------ -----
uid |用户名| | uid | parentuid |字段|值|

因此,每个User对象都与UserData.parentuid = User.uid。



我的用户类映射文件

 < class name =com.agetor .commons.security.Usertable =ac_users> 
< id name =uidcolumn =uidtype =long>
<! - < generator class =native/> - >
< / id>
< property name =usernamecolumn =username/>

< list name =fieldDatacascade =all>
< key column =parentuidnot-null =true/>
< index column =parentuid/>
< / list>

< / class>



 < class name =com.agetor.commons.fields.FieldDatatable =ac_userdef_data> 
< id name =uidcolumn =uidtype =long>
<! - < generator class =native/> - >
< / id>
<! - < property name =parentuidcolumn =parentuid/> - >
< property name =fieldnamecolumn =fieldname/>
< property name =valuecolumn =value/>
< / class>

到目前为止,我已经尝试了许多不同的配置,并且它们都具有不同程度的失败。代码粘贴在这里,不起作用。


  • parentuid属性被注释掉了,因为Hibernate在映射中给出了重复列错误。

  • 目前,在uid字段中仍然存在映射中的重复列,我使用< list-index />

  • 我不明白我在哪里指定UserData.parentuid是外键,并且列表应该使用User.uid作为键。


我希望有人能够提供帮助。






当您定义一对多和一个多对一,这是不是让它双向?
当前的工作模型是Unidirectional,UserData没有对用户的引用。您的建议失败,因为Hibernate无法为UserData上的User找到get或set方法。



是否暗示此代码使用User.uid作为键并匹配这对于UserData.parentuid列?或者是这个事实指定在别的地方?

 < list name =fieldDatainverse =true> 
< key column =parentuidnot-null =true/>
< / list>

我仍在学习Hibernate,并通过我可以找到的文档和示例工作。

解决方案

这是这个问题的最新进展。该配置可以成功从数据库加载。保存不起作用。
我决定改变数据库的设计,所以我在这里发布这里供其他人参考,在我放弃这种方法之前。

 < class name =com.agetor.commons.security.Usertable =ac_users> 
< id name =uidcolumn =uidtype =long>
< generator class =increment/>
< / id>
< property name =deletedcolumn =deleted/>
< property name =usernamecolumn =username/>
< property name =passwordcolumn =passwd/>
< property name =disabledcolumn =disabled/>
< property name =lockoutcolumn =lockout/>
< property name =expirescolumn =expires/>
< bag name =fieldDatalazy =extra>
< key column =parentuidnot-null =true/>
< / bag>
< bag name =groupstable =ac_group_relaccess =fieldlazy =extra>
< key column =useruid/>
< / bag>
< join table =ac_userdef_dataoptional =truefetch =join>
< subselect>

中选择
*
ac_userdef_data data
其中
data.objectname ='user'和
data.fieldname ='firstname'
< / subselect>
< key column =parentuid/>
< / join>
< join table =ac_userdef_dataoptional =truefetch =join>
< subselect>

中选择
*
ac_userdef_data数据
其中
data.objectname ='user'和
data.fieldname ='lastname'
< / subselect>
< key column =parentuid/>
< / join>

< / class>


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.

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

这篇关于在现有数据库上休眠一对多单向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-19 22:37