休眠关系映射结构

时间:2020-06-21 13:53:46

标签: hibernate entity-relationship

您好,我想知道如何使用关系映射将所有表格(球员,裁判员,比赛)映射到表格(得分)中。 我应该只使用OneToOne关系吗?

ManyToMany关系创建了另一个表,但是我希望所有内容都映射到Score表中,因为那里有所有内容。

我应该重新编程mysql数据库还是可以吗?

package com.simon.MavenHibernateProject;

imports..

@Entity
@Table(name = "player")
public class Player implements Serializable{
    private static final long serialVersionUID = 1L;
    
    @Id
    @Column(name = "p_id", unique = true)
    private int p_id;
    
    @Column(name = "firstName", nullable = false)
    private String fName;
    
    @Column(name = "lastName", nullable = false)
    private String lName;
    
    @Column(name = "phone", nullable = false)
    private String phone;

    @Column(name = "address", nullable = false)
    private String address;
    

    getters, setters...
    
}

package com.simon.MavenHibernateProject;

imports...

@Entity
@Table(name = "referee")
public class Referee implements Serializable{
    private static final long serialVersionUID = 1L;
    
    @Id
    @Column(name = "r_id", unique = true)
    private int r_id;
    
    @Column(name = "firstName", nullable = false)
    private String fName;
    
    @Column(name = "lastName", nullable = false)
    private String lName;
    
    @Column(name = "phone", nullable = false)
    private String phone;

    @Column(name = "address", nullable = false)
    private String address;
    
    getters, setters...
    
}

package com.simon.MavenHibernateProject;

imports...

@Entity
@Table(name = "competition")
public class Competition implements Serializable {
    private static final long serialVersionUID = 1L;
    
    @Id
    @Column(name = "comp_id", unique = true)
    private int comp_id;

    @Column(name = "discipline", nullable = false)
    private String discipline;
    
    @Column(name = "category", nullable = false)
    private String category;
    
    @Column(name = "data", nullable = false)
    private Date data;

    @Column(name = "city", nullable = false)
    private String city;
    
    @Column(name = "country", nullable = false)
    private String country;
    
    getters, setters...
}

package com.simon.MavenHibernateProject;

imports...

@Entity
@Table(name = "score")
public class Score implements Serializable{
    private static final long serialVersionUID = 1L;

    @Id
    @Column(name = "score_id", unique = true)
    private int score_id;

    @Column(name = "comp_id", nullable = false)
    private int comp_id;
    
    @Column(name = "r_id", nullable = false)
    private int r_id;
    
    @Column(name = "p_id", nullable = false)
    private int p_id;

    @Column(name = "points", nullable = false)
    private float points;
    
    getters, setters...
        
}

Mysql:

create table player(
    p_id INT PRIMARY KEY,
    firstName VARCHAR(30) NOT NULL,
    lastName VARCHAR(30) NOT NULL,
    phone VARCHAR(30) NOT NULL,
    address VARCHAR(30) NOT NULL);
    
create table referee(
    r_id INT PRIMARY KEY,
    firstName VARCHAR(30) NOT NULL,
    lastName VARCHAR(30) NOT NULL,
    phone VARCHAR(30) NOT NULL,
    address VARCHAR(30) NOT NULL);
    
create table competition(
    comp_id INT PRIMARY KEY,
    discipline VARCHAR(30) NOT NULL,
    category VARCHAR(30) NOT NULL,
    data DATETIME NOT NULL,
    city VARCHAR(30) NOT NULL,
    country VARCHAR(30) NOT NULL);
    
create table score(
    score_id INT PRIMARY KEY,
    comp_id INT, 
    r_id INT,
    p_id INT,
    points FLOAT,
    FOREIGN KEY (comp_id) REFERENCES competition(comp_id),
    FOREIGN KEY (r_id) REFERENCES referee(r_id),
    FOREIGN KEY (p_id) REFERENCES player(p_id));

1 个答案:

答案 0 :(得分:0)

您可以使用@ManyToOne注释。 这是一个例子

package com.simon.MavenHibernateProject;

imports...

@Entity
@Table(name = "score")
public class Score implements Serializable{
    private static final long serialVersionUID = 1L;

    @Id
    @Column(name = "score_id", unique = true)
    private int score_id;
    
    @ManyToOne
    @JoinColumn(name = "comp_id", nullable = false)
    private Competition competition;

    @ManyToOne
    @JoinColumn(name = "r_id", nullable = false)
    private Referee referee;

    @ManyToOne
    @JoinColumn(name = "p_id", nullable = false)
    private Player player;

    @Column(name = "points", nullable = false)
    private float points;

    getters, setters...
    
}