简体   繁体   中英

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. 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. 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. can someone tell me how & where should i use @OnetoOne, @OnetoMany, @ManytoMany, @ManytoOne in my code.

Student.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

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

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

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

then orm detects the relationship, but also you have annotations like @OneToMant @ManyToMany in JPA

The best way to define this relationship in your case will be Student and Course will have OneToMany relation with Booking. And Booking will have ManyToOne relation with Student and Course

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();
    }
}

You should not use primary keys of other entities in JPA!

Use @ManyToOne and Student as well as Cource instead of sid and cid.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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