Hibernate - 连接表中的新列

时间:2009-02-06 12:07:29

标签: sql hibernate mapping

我有一个类User对象。我试图使用Hibernate从数据库加载此对象。 我的SQL语句是:

SELECT
    users.uid               AS uid,
    users.deleted           AS deleted,
    users.username          AS username,
    users.passwd            AS passwd,
    users.disabled          AS disabled,
    users.lockout           AS lockout,
    users.expires           AS expires,
    data_firstname.value            AS firstname,
    data_lastname.value     AS lastname 
FROM 
    ac_users as users
LEFT JOIN
    ac_userdef_data as data_firstname
ON
    users.uid = data_firstname.parentuid AND
    data_firstname.fieldname like 'firstname'
LEFT JOIN
    ac_userdef_data as data_lastname
ON
    users.uid = data_lastname.parentuid AND
    data_lastname.fieldname like 'lastname'
WHERE
    users.deleted = 0

我对User类的映射是:

<class name="com.agetor.commons.security.User" table="ac_users">
    <id name="uid" column="uid" type="long" >
        <generator class="native"/>
    </id>
    <property name="deleted" column="deleted" />
    <property name="username" column="username" />   
    <property name="password" column="passwd" />
    <property name="firstname" column="firstname" />
    <property name="lastname" column="lastname" />
    <property name="disabled" column="disabled" />
    <property name="lockout" column="lockout" />
    <property name="expires" column="expires" />            
</class> 

我的问题是表ac_users没有列'firstname'或'lastname' 这些列仅存在于SQL-join语句的结果集中。 它们也不存在于ac_userdef_data表中。我有2个列:fieldname和value。和2行: fieldname ='firstname',值列中包含一些值 和另一行 fieldname ='lastname',在值列中有一些值

如何更改我的映射文件,以便Hibernate理解它需要将firstname和lastname列加载到我的POJO上的firstname和lastname字段中,而这些列实际上并不存在于引用的ac_users表中。?< / p>

2 个答案:

答案 0 :(得分:1)

我现在有一些工作代码。诀窍是为User类指定加载Query。然后,Hibernate将对Query的返回进行验证,而不是Table设计。

类映射

<class name="com.agetor.commons.security.User" table="ac_users">
    <id name="uid" column="uid" type="long" >
        <generator class="native"/>
    </id>
    <property name="deleted" column="deleted" />
    <property name="username" column="username" />   
    <property name="password" column="passwd" />
    <property name="firstname" column="firstname" />
    <property name="lastname" column="lastname" />
    <property name="disabled" column="disabled" />
    <property name="lockout" column="lockout" />
    <property name="expires" column="expires" />                 


    <loader query-ref="GetAllUsers" />
</class> 

我的查询声明

<sql-query name="GetAllUsers">
  <return alias="user" class="com.agetor.commons.security.User" />
  <![CDATA[ 
    SELECT
        users.uid               AS uid,
        users.deleted           AS deleted,
        users.username          AS username,
        users.passwd            AS passwd,
        users.disabled          AS disabled,
        users.lockout           AS lockout,
        users.expires           AS expires,
        data_firstname.value    AS firstname,
        data_lastname.value     AS lastname 
    FROM 
        ac_users as users
    LEFT JOIN
        ac_userdef_data as data_firstname
    ON
        users.uid = data_firstname.parentuid AND
        data_firstname.fieldname like 'firstname'
    LEFT JOIN
        ac_userdef_data as data_lastname
    ON
        users.uid = data_lastname.parentuid AND
        data_lastname.fieldname like 'lastname'
    WHERE
        users.deleted = 0
  ]]>
</sql-query> 

我必须使用行<return alias="user" class="com.agetor.commons.security.User" />,以便正确输入返回的集合

答案 1 :(得分:0)

这个问题的解决方案与我的另一个问题相同。所以我在这里链接到它并接受答案。

Hibernate One-To-Many Unidirectional on an existing DB