HBM Hibernate与额外列的多对多双向连接

时间:2014-03-27 13:02:43

标签: mysql spring hibernate hbm

我google了很多,并且遵循了许多关于它的教程,但我没有让它工作。表Player和Type之间有多对多的关系。两个表都通过typeperplayer连接。在表格中,typeperplayer是一个额外的付费列,我想在hibernate中使用它。

这是数据库方案:

播放器:

Play_ID:int

Play_FirstName:string

Play_LastName:string

Typeperplayer:

Typl_ID:int

Typl_Play_ID:int

Typl_Type_ID:int

Typl_Paid:int

类型

Type_ID:int

Type_Name:string

这是我的Hibernate-Mapping: 的播放器:

<hibernate-mapping>
<class name="de.tt.treg.server.domain.Player" table="player">
    <id name="id" type="java.lang.Integer">
        <column name="Play_ID" />
        <generator class="identity" />
    </id>
    <property name="firstName" type="string">
        <column name="Play_FirstName" length="255" not-null="false" unique="false" />
    </property>
    <property name="lastName" type="string">
        <column name="Play_LastName" length="255" not-null="false" unique="false" />
    </property>
    <set name="competitions" table="typeperplayer"
            inverse="true" lazy="false" fetch="select">
        <key>
            <column name="typl_play_id" not-null="true" />
        </key>
        <one-to-many class="de.tt.treg.server.domain.PlayerCompetition" />
    </set>
</class>

PlayerCompetition(Typeperplayer)

<hibernate-mapping package="de.tt.treg.server.domain">
<class name="PlayerCompetition" table="typeperplayer">
    <composite-id name="playerCompetitionPk"
            class="PlayerCompetitionPk">
        <key-property name="id" column="Typl_Play_Id"
            type="java.lang.Integer" />
        <key-property name="id" column="Typl_Type_Id"
            type="java.lang.Integer" />
    </composite-id>
    <property name="paid" type="java.lang.Integer">
        <column name="Typl_Paid" length="10" not-null="false" unique="false" />
    </property>
    <many-to-one name="player" class="Player" insert="false" update="false"/>
    <many-to-one name="competition" class="Competition" insert="false" update="false"/>
</class>

比赛(类型):

<hibernate-mapping>
<class name="de.tt.treg.server.domain.Competition" table="type">
    <id name="id" type="java.lang.Integer">
        <column name="Type_ID" />
        <generator class="identity" />
    </id>
    <property name="name" type="string">
        <column name="Type_Name" length="255" not-null="false" unique="false" />
    </property>
     <set name="players" table="typeperplayer"
            inverse="false" lazy="true" fetch="select">
        <key>
            <column name="Type_Id" not-null="true" />
        </key>
        <one-to-many class="de.tt.treg.server.domain.PlayerCompetition" />
    </set>

</class>

此外还有我的课程:

public class Player{

private int id;
private String firstName;
private String lastName;
private Set<PlayerCompetition> competitions = new HashSet<PlayerCompetition>();

public Player() {
}

public Player(String firstName, String lastName,
        Set<PlayerCompetition> competitions) {
    this.firstName = firstName;
    this.lastName = lastName;
    this.competitions = competitions;
}

public int getId() {
    return id;
}

public void setId(int id) {
    this.id = id;
}

public String getFirstName() {
    return firstName;
}

public void setFirstName(String firstName) {
    this.firstName = firstName;
}

public String getLastName() {
    return lastName;
}

public void setLastName(String lastName) {
    this.lastName = lastName;
}

public Set<PlayerCompetition> getCompetitions() {
    return competitions;
}

public void setCompetitions(Set<PlayerCompetition> competitions) {
    this.competitions = competitions;
}

}

public class PlayerCompetition {
private int id;
private int paid;
private PlayerCompetitionPk playerCompetitionPk;

public PlayerCompetition() {

}

public PlayerCompetition(int id, int paid,
        PlayerCompetitionPk competitionPk) {
    super();
    this.id = id;
    this.paid = paid;
    this.playerCompetitionPk = competitionPk;
}

public int getId() {
    return id;
}

public void setId(int id) {
    this.id = id;
}

public int getPaid() {
    return paid;
}

public void setPaid(int paid) {
    this.paid = paid;
}

public PlayerCompetitionPk getPlayerCompetitionPk() {
    return playerCompetitionPk;
}

public void setPlayerCompetitionPk(PlayerCompetitionPk playerCompetitionPk) {
    this.playerCompetitionPk = playerCompetitionPk;
}

public Player getPlayer() {
    return getPlayerCompetitionPk().getPlayer();
}

public void setPlayer(Player player) {
    getPlayerCompetitionPk().setPlayer(player);
}

public Competition getCompetition() {
    return getPlayerCompetitionPk().getCompetition();
}

public void setCompetition(Competition competition) {
    getPlayerCompetitionPk().setCompetition(competition);
}

}

public class PlayerCompetitionPk {

private int id;
private Player player;
private Competition competition;

public PlayerCompetitionPk() {

}

public int getId() {
    return id;
}

public Player getPlayer() {
    return player;
}

public void setPlayer(Player player) {
    this.player = player;
}

public Competition getCompetition() {
    return competition;
}

public void setCompetition(Competition competition) {
    this.competition = competition;
}

public void setId(int id) {
    this.id = id;
}

}

public class Competition {

private int id;

private String name;


private Set<PlayerCompetition> players;

public Competition() {
}

public Competition(int id, String name) {
    this.id = id;
    this.name = name;
}

public int getId() {
    return id;
}

public void setId(int id) {
    this.id = id;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public Set<PlayerCompetition> getPlayers() {
    return players;
}

public void setPlayers(Set<PlayerCompetition> players) {
    this.players = players;
}

}

我希望有人可以帮助我。如果你需要一些额外的信息,你会得到它;-)。谢谢。

这是生成的sql语句: 选择player0_.Play_ID为Play1_1_2_,player0_.Play_Club_ID为Play2_1_2_,player0_.Play_User_ID为Play3_1_2_,player0_.Play_FirstName为Play4_1_2_,player0_.Play_LastName为Play5_1_2_,player0_.Play_BirthDate为Play6_1_2_,team1_.Club_ID为Club1_0_0_,team1_.Club_Name为Club2_0_0_,team1_ .Club_ShortName为Club3_0_0_,team1_.Club_Verband为Club4_0_0_,user2_.User_ID为User1_4_1_,user2_.User_Name为User2_4_1_,user2_.User_Passwort为User3_4_1_,user2_.User_RegisterDate为User4_4_1_来自玩家player0_内部加入俱乐部team1_ on player0_.Play_Club_ID = team1_.Club_ID inner在player0_.Play_User_ID = user2_.User_ID上加入用户user2_,其中player0_.Play_ID =?

在此声明中,与其他表俱乐部和用户进行了两次联接。但是没有加入via typeperplayer。

1 个答案:

答案 0 :(得分:0)

解决方案是不直接映射播放器并使用多对多关系键入实体,而是使用一个中间实体播放类型,您可以将它与一对多连接到播放器和类型。

通过这种方式,您可以在playteype实体上使用其他列作为属性。

player <1-*> playertype <*-1> type

编辑: 这是mkyong

的一个很好的例子

http://www.mkyong.com/hibernate/hibernate-many-to-many-example-join-table-extra-column-annotation/