简体   繁体   中英

How to Map single POJO class to Multiple Tables

I'm trying to map com.chqmas.user.User pojo class to both USR table.and USR_PWD_HISTORY tables.

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
        "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping>
    <class name="com.chqmas.user.User" table="USR" batch-size="50">
        <id name="name" column="NAME"/>
        <property name="bankBranch" column="BANK_BRANCH"/>
        <property name="realName" column="REAL_NAME"/>
        <property name="pwd" column="PWD"/>
        <property name="pwdenc" column="PWD_ENC"/>
        <property name="level" column="LEVEL"/>
        <property name="url" column="URL"/>
        <property name="status" column="STATUS"/>
        <property name="lastSignOnDate" column="LAST_SIGN_ON_DATE"/>
        <property name="passWdChangedDate" column="PASS_WD_CHANGE_DATE"/>
    </class>

    <class name="com.chqmas.user.User" table="USR_PWD_HISTORY" batch-size="50">
        <id name="name" column="NAME"/>
        <property name="pwd" column="PWD"/>
        <property name="pwdenc" column="PWD_ENC"/>
        <property name="passWdChangedDate" column="PASS_WD_CHANGE_DATE"/>
    </class>
</hibernate-mapping>

But I got this error..

在此处输入图片说明

How can I map this pojo class to these two tables.

Thank you.

USR和USR_PWD_HISTORY代表两个不同的逻辑实体,那么为什么不将密码历史记录列表保留在用户对象内部呢?

This is possible, although I would Strongly advise again this approach because I don't believe that it would be an accurate representation of the data.

You can use the join element in the HBM.

<class name="com.chqmas.user.User" table="USR" ...>
    <id name="name" column="NAME"/>
    <!-- mappings for other columns from USR table -->

    <join table="USR_PWD_HISTORY">
        <!-- 
            key defines the pk column of that joined table which is assumed
            to also be the foreign-key source column referring back to 
            USR
        -->
        <key column="name"/>

        <!-- mappings for other columns from USR_PWD_HISTORY table -->
    </join>
</class>

However I would not recommend this for a 1-to-many or many-to-many relationship, where a second POJO and a bag would be more appropriate.

Additionally the column names would suggest that plain text passwords are being stored, this is a security risk, and is "bad practice".

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM