2016-08-22 167 views
0

假設我有學生,並且想要爲每個學生映射一個房子。Hibernate關係註釋

說實體1:Student_master表被描述爲(student_id數據(primary_key),標準,等級,年齡)

說實體2:house_master表被描述爲(house_id(primary_key),house_color_house_capacity)

說實體3:student_house_mapping表是(student_house_id,Student_id(foreign_key),house_id(foreign_key))。

現在我在這裏有過每個學生穿越並獲取其內部細節,

例如,我得到了學生,現在通過student_house_mapping表我得到的house_id並用它,我得到一個特定的house_master對象。

如何在Hibernate中可以使用這個關係來實現(例如:一對一,多對一)的註解,如果我對所有的實體類,我用

回答

1
定義

joinColumns = {@JoinColumn(...)}, 
inverseJoinColumns = {@JoinColumn(...)}) 

參數在它自己的列

通過閱讀第一句話,我可以說這是OneToOne,正如你所說每個學生都有一所房子。

如果你考慮一個場景,每個學生可以有很多房子。 然後,這種關係將是OneToMany,並且只有兩個表的學生和學生擁有OneToMany與House(POJO班級)的房子。

如果你認爲連房子都可以屬於很多學生。 然後,你應該去ManyToMany與你目前的表學生,房屋和students_house表(橋表)。

Student.java

@Entity 
@Table(name="students") 
public class Student { 
private int studentId;// set the column name with @JoinColumn annotation if you want 
private int standard;// to be different from the variable name 
private int grade; 
private int age; 
@OneToMany 
@JoinTable(name="students_houses", 
    joinColumns = @JoinColumn(name="studentId"), 
    inverseJoinColumns = @JoinColumn(name="houseId")) 
private List<House> houses; 
// getters and setters 
} 

House.java

@Entity 
@Table(name="houses") 
public class House { 
private int houseId; 
private String color; 
private int houseCapacity; 
@OneToMany 
@JoinTable(name="students_houses", 
    joinColumns = @JoinColumn(name="houseId"), 
    inverseJoinColumns = @JoinColumn(name="studentId")) 
private List<Student> students; 
// getters and setters 
} 

如果你想比這兩個表中,你可以的外鍵與其它附加字段創建自定義的表橋創建一個自定義Bridge類,每個類具有OneToMany和Bridge類。和Bridge類有兩個類的ManyToOne。

您可以參閱計算器文檔進行進一步的例子

0

一種方法一直是@JoinTable註解。 簡單的例子:

@ManyToMany 
@JoinTable(name = "student_house_mapping") 
private List<Student> students; 

如果Hibernate無法找到正確的加入(有時是這種情況),你有@JoinTable

0
Assuming Student and House has ManyToMany relationship, you can design something like this: 

    package com.raf.prac.domain; 

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


    @Entity 
    @Table(name = "student") 
    public class Student { 
     @Id 
     @GeneratedValue(strategy = GenerationType.AUTO) 
     private int student_id; 
     @Column(name = "standard") 
     String standard; 
     @Column(name = "grade") 
     String grade; 
     @Column(name = "age") 
     int age; 

     @ManyToMany 
     @JoinTable(name = "student_house_mapping", 
       joinColumns = {@JoinColumn(name = "student_id")}, 
       inverseJoinColumns = {@JoinColumn(name = "house_id")}) 
     private Set<House> houses = new HashSet(); 

     public Student() { 
     } 

     public String getStandard() { 
      return standard; 
     } 

     public void setStandard(String standard) { 
      this.standard = standard; 
     } 

     public String getGrade() { 
      return grade; 
     } 

     public void setGrade(String grade) { 
      this.grade = grade; 
     } 

     public int getAge() { 
      return age; 
     } 

     public void setAge(int age) { 
      this.age = age; 
     } 

     public Set<House> getHouses() { 
      return houses; 
     } 

     public void setHouses(Set<House> houses) { 
      this.houses = houses; 
     } 
    } 

package com.raf.prac.domain; 

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

/** 
* Created by ar on 22/08/2016. 
*/ 
@Entity 
@Table(name = "house") 
public class House { 
    @Id 
    @GeneratedValue(strategy = GenerationType.AUTO) 
    private int house_id; 
    @Column(name = "house_color") 
    private String house_color; 
    @Column(name = "house_capacity") 
    private int house_capacity; 

    @ManyToMany(mappedBy = "houses") 
    Set<Student> students = new HashSet(); 

    public House() { 
    } 

    public int getHouse_id() { 
     return house_id; 
    } 

    public void setHouse_id(int house_id) { 
     this.house_id = house_id; 
    } 

    public String getHouse_color() { 
     return house_color; 
    } 

    public void setHouse_color(String house_color) { 
     this.house_color = house_color; 
    } 

    public int getHouse_capacity() { 
     return house_capacity; 
    } 

    public void setHouse_capacity(int house_capacity) { 
     this.house_capacity = house_capacity; 
    } 

    public Set<Student> getStudents() { 
     return students; 
    } 

    public void setStudents(Set<Student> students) { 
     this.students = students; 
    } 
}