我是冬眠初学者。基本上我想让UserId1和UserId2成为UserId的外键。我知道我需要使用多对一和一对多,但我似乎不明白如何使用它们,因为我的列有不同的名称。感谢您的帮助!
这是我的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;
}
}
最佳答案
映射应如下所示。
因为UserProfile
和UserTrack
之间有一对多的关系,
您可以使用Set<UserTracks> userTracks
中的UserProfile
来跟踪每个UserTrack
的UserProfile
s
对于UserProfile类
<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类
<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
编辑:我认为您的实体类应该类似于以下内容。
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;
}
}
关于mysql - xml Hibernate映射,具有复合ID和不同列名的多对一映射,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/33948315/