xml Hibernate映射,多对一,具有复合ID和不同的列名

时间:2015-11-26 23:18:02

标签: mysql xml hibernate one-to-many

我是hibernate的初学者。基本上我想创建UserId的UserId1和UserId2外键。我知道我需要使用多对一和一对多,但我似乎无法理解如何使用它们,因为我的列有不同的名称。任何帮助表示赞赏!
这是我的hibernate.hbm.xml文件

<?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="objects.UserProfile" table="userProfiles">
        <id name="UserId" column="UserProfileId">
            <generator class="native"/>
        </id>

        <set name="userTracks" table="userTrack" 
            inverse="true" lazy="true" fetch="select">
            <key>
                <column name="UserProfileId" not-null="true" />
            </key>
            <one-to-many class="objects.UserTrack" />
        </set>
    </class>
    <class name="objects.UserTrack" table="userTrack">
        <composite-id>
            <key-property name="UserId1" column="UserProfileId1" type="integer" />      
            <key-property name="UserId2" column="UserProfileId2" type="integer" />           
        </composite-id>   
    </class>
</hibernate-mapping>

所以基本上我的track类中的两个id都应该指向profile类中的id

我的userTrack类:

public class UserTrack implements Serializable {

    private int UserProfileId1;
    private int UserProfileId2;


    public int getUserProfileId1() {
        return UserProfileId1;
    }
    public void setUserProfileId1(int userProfiletId1) {
        UserProfiletId1 = userProfileId1;
    }
    public int getUserProfileId2() {
        return UserProfileId2;
    }
    public void setUserProfileId2(int userProfileId2) {
        UserProfileId2 = userProfileId2;
    }

}

我的个人资料类:

public class UserProfile {

    private int UserProfileId;

    public int getUserProfileId() {
        return UserProfileObjectId;
    }

    public void setUserProfileId(int userProfileId) {
        UserProfileId = userProfileId;
    }
}

1 个答案:

答案 0 :(得分:1)

映射应如下所示。

由于UserProfileUserTrack之间存在一对多关系, 您可以使用Set<UserTracks> userTracks中的UserProfile来跟踪每个UserTrack的{​​{1}}

UserProfile

UserProfile

UserTrack

<hibernate-mapping>
    <class name="objects.UserProfile" table="userProfiles">
        <id name="userProfileId" column="UserProfileId">
            <generator class="native"/>
        </id>

        <!-- other property definitions should come here -->

        <set name="userTracks" table="userTrack" 
                inverse="true" lazy="true" fetch="select">
            <key>
                <column name="UserProfileId" not-null="true" />
            </key>
            <one-to-many class="objects.UserTrack" />
        </set>
    </class>
</hibernate-mapping>

这两个xml配置文件可以集成到主<hibernate-mapping> <class name="objects.UserTrack" table="userTrack"> <id name="userId" type="java.lang.String"> <column name="userTrackName" /> </id> <many-to-one name="userProfile" class="objects.UserProfile" fetch="select"> <column name="UserProfileId" not-null="true" /> </many-to-one> <!-- other property definitions should come here --> </class> </hibernate-mapping> 文件中,如下所示。

hibernate.cfg.cml

希望这会有所帮助。 更多信息here

编辑:我认为您的实体类应该类似于以下内容。

<hibernate-configuration>
<session-factory>
    <property name="hibernate.connection.driver_class">xxxxxxxxxx</property>
    <property name="hibernate.connection.url">xxxxxxxx</property>
    <property name="hibernate.connection.username">username</property>
    <property name="hibernate.connection.password">password</property>
    <property name="hibernate.dialect">xxxxxx</property>
    <property name="show_sql">true</property>
    <property name="format_sql">true</property>

    <mapping resource="path to this file/UserProfile.hbm.xml" />
    <mapping resource="path to this file/UserTrack.hbm.xml" />
</session-factory>
</hibernate-configuration>

public class UserTrack implements Serializable {
// consider this as the primary key for UserTrack or feel free to change.
    private String userTrackName; 

//    private int UserProfileId1;
//    private int UserProfileId2;

// other attributes related to UserTrack 

}