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

xml Hibernate mapping, many to one with composite ids and different column names

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

所以基本上我曲目中的两个 id class 应该指向配置文件中的 id class

我的 userTrack class:

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

}

我的个人资料class:

public class UserProfile {

    private int UserProfileId;

    public int getUserProfileId() {
        return UserProfileObjectId;
    }

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

映射应如下所示。

因为 UserProfileUserTrack 之间存在 一对多关系 , 您可以在 UserProfile 中使用 Set<UserTracks> userTracks 来跟踪每个 UserProfile

UserTrack

对于 UserProfile Class

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

对于 UserTrack class

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

这两个 xml 配置文件可以集成到主 hibernate.cfg.cml 文件中,如下所示。

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

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

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

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 

}

public class UserProfile {

    // you could keep track of the UserTracks that belongs to a 
    //particular UserProfile in the Set.
    // Now a UserProfile can belong to many UserTracks. Not just 2.
    private Set<UserTrack> userTracks = new HashSet<>();

    private int UserProfileId;

    public int getUserProfileId() {
        return UserProfileObjectId;
    }

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