gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 23:43:55 26 4
gpt4 key购买 nike

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

最佳答案

映射应如下所示。

因为 UserProfile 之间存在一对多关系UserTrack ,你可以使用 Set<UserTracks> userTracksUserProfile跟踪 UserTrack每个UserProfile

对于 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com