为多个表连接创建实体类

时间:2017-12-07 04:09:01

标签: java mysql hibernate spring-boot spring-data-jpa

我正在开发一个spring boot应用程序,我需要为我的一个实体关系提供适当的设计帮助。

这些是我的表

DROP TABLE IF EXISTS user;
CREATE TABLE user
(
 User_ID   INT AUTO_INCREMENT PRIMARY KEY,
 Email     VARCHAR(1024),
 Phone     BIGINT,
 Password  VARCHAR(128),
 User_Type ENUM ('TEACHER', 'PARENT'),
 Status    ENUM ('ACTIVE', 'DEACTIVE', 'SUSPENDED')
 );
DROP TABLE IF EXISTS teacher;
CREATE TABLE teacher
(
  Teacher_ID      INT AUTO_INCREMENT PRIMARY KEY,
  Name            VARCHAR(1024),
  Email           VARCHAR(1024),
  Phone           BIGINT,
  Gender          ENUM (0, 1),
  Date_Of_Joining INT,
  Designation     VARCHAR(1024),
  Is_Active       INT
);
DROP TABLE IF EXISTS parent;
CREATE TABLE parent
(
  Parent_ID  INT AUTO_INCREMENT PRIMARY KEY,
  Name       VARCHAR(1024),
  Email      VARCHAR(1024),
  Phone      BIGINT,
  Occupation VARCHAR(1024),
  Address    VARCHAR(4048)
);
DROP TABLE IF EXISTS student;
CREATE TABLE student
(
  Student_ID    INT AUTO_INCREMENT PRIMARY KEY,
  Student_Name  VARCHAR(1024),
  Class         VARCHAR(16),
  Section       VARCHAR(16),
  Academic_Year VARCHAR(64),
  DOB           DATE,
  Gender        ENUM (0, 1),
  Parent_ID     INT
);

任何人都可以帮我创建相同的实体类。根据用户表中的用户类型,我需要加入教师或父表。例如,如果User_Type是TEACHER,那么我将加入教师表,否则我将加入用户与父表。加入条件将是电子邮件。

学生表与父表具有一对一的关系。

2 个答案:

答案 0 :(得分:0)

您可以像这样创建域模型

@Entity
@Inheritance(strategy= InheritanceType.JOINED)
public User{

    //properties

}

@Entity
@PrimaryKeyJoinColumn(name = "id")
public  Teacher extends User{

    //properties

}


@Entity
public Parent extends User{

   //properties
}

@Entity
public Students {


    @GenericGenerator(name = "generator", strategy = "foreign",parameters = @Parameter(name = "property", value = "stock"))
    @Id
    @GeneratedValue(generator = "generator")
    @Column(name = "Parent_ID", unique = true, nullable = false)
    private Integer id;

    @OneToOne(fetch = FetchType.LAZY)
    @PrimaryKeyJoinColumn
    public Parent parent;
}

答案 1 :(得分:0)

您可以使用以下类:

用户:

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Inheritance(strategy= InheritanceType.JOINED)
@Table(name = "user")
public class User {
@Id
@GeneratedValue
@Column(name = "User_ID")
private Integer userID;

@Column(name = "name", length = 1024)
private String name;

@Column(name = "email", length = 1024)
private String email;

@Column(name = "Phone")
private Long phone;

@Column(name = "Password", length = 128)
private String password;

@Column(name = "User_Type", columnDefinition = "enum ('TEACHER', 'PARENT')")
private String userType;

@Column(name = "Status", columnDefinition = "enum ('ACTIVE', 'DEACTIVE', 'SUSPENDED')")
private String status;
}

师:

@Entity
@Table(name = "teacher")
public class Teacher extends User{
@Id
@GeneratedValue
@Column(name = "teacher_ID")
private Integer teacherID;

@Column(name = "name", length = 1024)
private String name;

@Column(name = "email", length = 1024)
private String email;

@Column(name = "Phone")
private Long phone;

@Column(name = "Gender", columnDefinition = "BOOLEAN")
private Boolean gender;

@Column(name = "Date_Of_Joining")
private Integer dateOfJoining;

@Column(name = "Designation", length = 1024)
private String designation;

@Column(name = "Is_Active")
private Integer isActive;
}

父:

@Entity
@Table(name = "parent")
public class Parent extends User{
@Id
@GeneratedValue
@Column(name = "Parent_ID")
private Integer parentID;

@Column(name = "name", length = 1024)
private String name;

@Column(name = "email", length = 1024)
private String email;

@Column(name = "Phone")
private Long phone;

@Column(name = "Occupation", length = 1024)
private String occupation;

@Column(name = "Address",length=4048)
private String address;
}

学生:

@Entity
@Table(name = "student")
public class Student {
@Id
@GeneratedValue
@Column(name = "Student_ID")
private Integer studentID;

@Column(name = "student_name", length = 1024)
private String studentName;

@Column(name = "study_class", length = 16)
private String studyClass;

@Column(name = "section", length = 16)
private String section;

@Column(name = "academic_year",length = 64)
private String academicYear;

@Temporal(TemporalType.DATE)
@Column(name = "dob", length = 4048)
private Date dob;

@OneToOne(fetch = FetchType.LAZY)
@PrimaryKeyJoinColumn(name = "parent")
public Parent parent;
}

尝试一下,看看是否有帮助。