dexter
dexter

Reputation: 462

Hibernate relational annotations

Suppose i have student and want to map each student with a house .

Say Entity 1 : Student_master table is described as (Student_id(primary_key),Standard,grade,age)

Say Entity 2 : house_master table is described as (house_id(primary_key),house_color_house_capacity)

Say Entity 3 :student_house_mapping table is (student_house_id,Student_id(foreign_key),house_id(foreign_key)).

Now here I have to traverse through each student and get its house details ,

For Example , i get a student , now through student_house_mapping table i get the house_id and with it i get a specific house_master object.

How in hibernate could this be achieved using relational (Example : one to one, many to one)annotations , if I have the classes for all the entities

Upvotes: 0

Views: 96

Answers (3)

omkar sirra
omkar sirra

Reputation: 726

By reading the first sentence, I can say that It is OneToOne as you stated that Each Student has A House.

If you consider a scenario, Each student can have many houses. Then, the relationship would be OneToMany and achieved by only two tables students and houses with Student having OneToMany with House(POJO classes).

If you think that even the house can belong to many students. Then, you should go for ManyToMany with your present tables students, houses and students_house table(Bridge table).

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
}

If you want to create custom Bridge table with additional fields other than foreign keys of both the tables, You can create a custom Bridge class with Each class having OneToMany with the Bridge class. And Bridge class having ManyToOne with the both classes.

You can refer the stackoverflow documentation for further examples

Upvotes: 1

abdulrafique
abdulrafique

Reputation: 304

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

Upvotes: 0

LuckAss
LuckAss

Reputation: 114

One way i used has been the @JoinTable Annotation. Simple example:

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

if hibernate can't find the correct join columns on it's own (sometimes this is the case) you have to define the

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

parameters in @JoinTable too

Upvotes: 0

Related Questions