简体   繁体   English

jpa中如何使用表之间的关系

[英]How to use relationships between tables in jpa

I am learning jpa on my own by using online tutorials & trying out possible examples but now i am little confused about how to use relationships between tables.我正在通过使用在线教程和尝试可能的示例来自己学习 jpa,但现在我对如何使用表之间的关系感到有些困惑。 I have 3 classes having @Entity annotation which means jpa will create table based on these classes.i have id field in Student, Course, Booking classes and they will be primary key for respective tables.我有 3 个具有 @Entity 注释的类,这意味着 jpa 将基于这些类创建表。我在学生、课程、预订类中有 id 字段,它们将是各个表的主键。 The help i need is, in Booking class there is sid & cid fields and i want them to be referenced such as sid(Student.java)=sid(Booking.java) & cid(Course.java)=cid(Booking.java) and the scenario is each student can one or multiple bookings of one or multiple course.我需要的帮助是,在 Booking 类中有 sid 和 cid 字段,我希望它们被引用,例如 sid(Student.java)=sid(Booking.java) & cid(Course.java)=cid(Booking.java) ) 并且场景是每个学生可以一次或多次预订一门或多门课程。 can someone tell me how & where should i use @OnetoOne, @OnetoMany, @ManytoMany, @ManytoOne in my code.有人可以告诉我我应该如何以及在哪里使用@OnetoOne、@OnetoMany、@ManytoMany、@ManytoOne 在我的代码中。

Student.java学生.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;
}
}

Course.java课程.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;
    }
}

Booking.java预订.java

package com.testapp;

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

@Entity
public class Booking {
    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    private int bid;
    private String date;
    private int sid;
    private int cid;

    //Getters and Setters....
      ..

    public Booking() {
        super();
    }

    public Booking(int bid, String date, int sid, int cid) {
        super();
        this.bid = bid;
        this.date= date;
        this.sid = sid;
        this.cid = cid;
    }

    public Booking(String date, int sid, int cid) {
        super();
        this.date = date;
        this.sid = sid;
        this.cid = cid;
    }
}

Thank You..谢谢你..

Just define object in you class, as an example student involving many Cource , then you can define property on student class like below只需在您的班级中定义对象,例如学生涉及许多 Cource ,然后您就可以在学生班级上定义属性,如下所示

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

then orm detects the relationship, but also you have annotations like @OneToMant @ManyToMany in JPA然后 orm 检测到这种关系,但你也有 JPA 中的 @OneToMant @ManyToMany 之类的注释

The best way to define this relationship in your case will be Student and Course will have OneToMany relation with Booking.在您的情况下定义这种关系的最佳方法是 Student 和 Course 将与 Booking 具有 OneToMany 关系。 And Booking will have ManyToOne relation with Student and Course并且 Booking 将与 Student 和 Course 有 ManyToOne 关系

Student.java学生.java

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

Course.java课程.java

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

Booking.java预订.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();
    }
}

You should not use primary keys of other entities in JPA!您不应该在 JPA 中使用其他实体的主键!

Use @ManyToOne and Student as well as Cource instead of sid and cid.使用@ManyToOne 和Student 以及Cource 代替sid 和cid。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM