如何在JPA中使用表之间的关系

时间:2018-10-09 10:45:07

标签: java database hibernate jpa spring-data-jpa

我通过使用在线教程和尝试可能的示例来自己学习jpa,但是现在我对如何使用表之间的关系几乎不感到困惑。我有3个带有@Entity注释的类,这意味着jpa将基于这些类创建表。我在Student,Course,Booking类中具有id字段,它们将是各个表的主键。 我需要的帮助是,在Booking类中有sid和cid字段,我希望它们被引用,例如sid(Student.java)= sid(Booking.java)和cid(Course.java)= cid(Booking.java ),情况是每个学生可以预订一门或多门课程的一个或多个预订。有人可以告诉我在代码中如何使用@ OnetoOne,@ OnetoMany,@ ManytoMany,@ ManytoOne。

Student.java

<EntityType Name="user" BaseType="microsoft.graph.directoryObject" OpenType="true">
    <Property Name="accountEnabled" Type="Edm.Boolean" />
    <Property Name="ageGroup" Type="Edm.String" />
    <Property Name="assignedLicenses" Type="Collection(microsoft.graph.assignedLicense)" Nullable="false" />
    <Property Name="assignedPlans" Type="Collection(microsoft.graph.assignedPlan)" Nullable="false" />
    <Property Name="businessPhones" Type="Collection(Edm.String)" Nullable="false" />
    <Property Name="city" Type="Edm.String" />
    <Property Name="companyName" Type="Edm.String" />
    <Property Name="consentProvidedForMinor" Type="Edm.String" />
    <Property Name="country" Type="Edm.String" />
    <Property Name="department" Type="Edm.String" />
    <Property Name="deviceKeys" Type="Collection(microsoft.graph.deviceKey)" Nullable="false" />
    <Property Name="displayName" Type="Edm.String" />
    <Property Name="employeeId" Type="Edm.String" />
    <Property Name="givenName" Type="Edm.String" />
    ...
  </EntityType>

Course.java

package com.testapp;

import java.util.List;
import javax.persistence.*;

@Entity
public class Student{

@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private int sid;
private String name;
private int salary;

//Getters and Setters....
  ..

public Student() {
    super();
}

public Student(int sid, String name, float salary) {
    super();
    this.sid = sid;
    this.name = name;
    this.salary = salary;
}

public Student(String name, float salary) {
    super();
    this.name = name;
    this.salary = salary;
}
}

Booking.java

package com.testapp;

import java.util.List;
import javax.persistence.*;

@Entity
public class Course {
    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    private int cid;
    private String cname;
    private int price;

    //Getters and Setters....
      ..

    public Course() {
        super();
    }

    public Course(int cid, String cname, int price) {
        super();
        this.cid = cid;
        this.cname = cname;
        this.price = price;
    }
    public Course(String cname, int price) {
        super();
        this.cname = cname;
        this.price = price;
    }
}

谢谢..

3 个答案:

答案 0 :(得分:0)

只需在您的班级中定义对象,例如一个涉及许多Cource的学生示例,然后您就可以在学生班级上定义属性,如下所示:

public class Student{
private List<Cource> cources;
}

然后orm会检测到这种关系,但是您在JPA中也有@OneToMant @ManyToMany之类的批注

答案 1 :(得分:0)

在您的案例中定义此关系的最佳方法是学生,而课程将与预订具有OneToMany关系。并且预订将与学生和课程建立ManyToOne关系

Student.java

@OneToMany(mappedBy = "student", cascade = CascadeType.ALL, orphanRemoval = true)
public Set< Booking >   getBookings() {
    return bookings;
}

Course.java

@OneToMany(mappedBy = "course", cascade = CascadeType.ALL, orphanRemoval = true)
public Set<Booking>   getBookings() {
    return bookings;
}

Booking.java

    @Entity
    public class Booking {
    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    private int bid;
    private String date;
    private Student student;
    private Course course;

    @Id
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "sid")
    public Student getStudent() {
       return student;
    }

    @Id
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "cid")
    public Course getCourse() {
       return course;
    }
    //Getters and Setters....
      ..

    public Booking() {
        super();
    }
}

答案 2 :(得分:0)

您不应在JPA中使用其他实体的主键!

使用@ManyToOne和Student以及Cource代替sid和cid。