Home How Hibernate match entity in one-to-one relation, despite of absence of a foreign Key?
Reply: 1

How Hibernate match entity in one-to-one relation, despite of absence of a foreign Key?

fbm
1#
fbm Published in 2018-01-11 17:32:10Z

i am still learning hibernate ORM. I have created 2 entities student and address.The realtion between them is one to one bidirectional.But when i check the db i found 3 tables :Student,Address and hibernate_sequence(in which an int attribute called next_val is registered ).

Those are the table structure: student(STUDENT_ID, FIRST_NAME, LAST_NAME, SECTION) address(ADDRESS_ID, CITY, COUNTRY, STREET) hibernate_sequence(next_val)

I want to know how hibernate match the student to it's address despite the abcense of a foreign Key. By the way i tried session.find(Student.class, student3.getId()) and i got the address of that student.

This is my student class :

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.OneToOne;
import javax.persistence.Table;

@Entity
@Table(name = "STUDENT")
public class Student {

    @Id
    @GeneratedValue
    @Column(name = "STUDENT_ID")
    private long id;

    @Column(name = "FIRST_NAME")
    private String firstName;

    @Column(name = "LAST_NAME")
    private String lastName;

    @Column(name = "SECTION")
    private String section;

    @OneToOne(mappedBy="student", cascade = CascadeType.ALL)
    private Address address;

    public Student() {

    }

    public Student(String firstName, String lastName, String section) {
        this.firstName = firstName;
        this.lastName = lastName;
        this.section = section;
    }

    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public String getSection() {
        return section;
    }

    public void setSection(String section) {
        this.section = section;
    }

    public Address getAddress() {
        return address;
    }

    public void setAddress(Address address) {
        this.address = address;
    }

    @Override
    public String toString() {
        return "Student [id=" + id + ", firstName=" + firstName + ", lastName="
                + lastName + ", section=" + section + ", address=" + address
                + "]";
    }

}

This is my Address Code :

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.OneToOne;
import javax.persistence.PrimaryKeyJoinColumn;
import javax.persistence.Table;
import org.hibernate.annotations.Parameter;
import org.hibernate.annotations.GenericGenerator;


@Entity
@Table(name = "ADDRESS")
public class Address {

    @Id
    @Column(name="ADDRESS_ID")
    @GeneratedValue(generator="gen")
    @GenericGenerator(name="gen", strategy="foreign",parameters=@Parameter(name="property", value="student"))
    private long id;

    @Column(name = "STREET")
    private String street;

    @Column(name = "CITY")
    private String city;

    @Column(name = "COUNTRY")
    private String country;

    @OneToOne
    @PrimaryKeyJoinColumn
    private Student student;

    public Address() {

    }

    public Address(String street, String city, String country) {
        this.street = street;
        this.city = city;
        this.country = country;
    }

    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }

    public String getStreet() {
        return street;
    }

    public void setStreet(String street) {
        this.street = street;
    }

    public String getCity() {
        return city;
    }

    public void setCity(String city) {
        this.city = city;
    }

    public String getCountry() {
        return country;
    }

    public void setCountry(String country) {
        this.country = country;
    }

    public Student getStudent() {
        return student;
    }

    public void setStudent(Student student) {
        this.student = student;
    }

    @Override
    public String toString() {
        return "Address [id=" + id + ", street=" + street + ", city=" + city
                + ", country=" + country + "]";
    }

}
Luiggi Mendoza
2#
Luiggi Mendoza Reply to 2018-01-11 17:43:10Z

Since it's a 1 on 1 relationship, it uses the value of the key of table student for the id of the table address. You may see these details if you enable logging in trace level and (after omitting lots of messages) check the parametererized queries sent to the database and the values for these parameters.

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.434465 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO