Haris Mehmood
Haris Mehmood

Reputation: 902

Hibernate Insert table id as foreign key in another table

I have 2 tables PERSON and CARD

I want my table CARD to have PERSON_ID as FK how am I suppose to do it in hibernate entity class.

enter image description here

Is there a way to do it through Java Classes and annotation ?

MY PERSON ENTITY CLASS

package com.managementsystem.model;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;

@Entity(name="PERSON")
public class Person {

    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    private int id;

    private String name;
    private String card_no;
    private String rank;
    private String dte;

    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getCard_no() {
        return card_no;
    }
    public void setCard_no(String card_no) {
        this.card_no = card_no;
    }
    public String getRank() {
        return rank;
    }
    public void setRank(String rank) {
        this.rank = rank;
    }
    public String getDte() {
        return dte;
    }
    public void setDte(String dte) {
        this.dte = dte;
    }

}

MY CARD ENTITY CLASS

package com.managementsystem.model;

import javax.persistence.Entity;
import javax.persistence.Id;


@Entity(name="CARD")
public class Card {

    @Id
    private int id;

    //What to do here, how to link this to person id.
    private int person_id;
    private int serial_no;

    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public int getPerson_id() {
        return person_id;
    }
    public void setPerson_id(int person_id) {
        this.person_id = person_id;
    }
    public int getSerial_no() {
        return serial_no;
    }
    public void setSerial_no(int serial_no) {
        this.serial_no = serial_no;
    }


}

What to do in card class so that I can enter person_id in card table which is required.

Upvotes: 1

Views: 4270

Answers (2)

Learner
Learner

Reputation: 21445

In Person you want to maintain the Card information using card_no property and also in Card entity you want to have person_id property. So what you are looking for is bidirectional relationship. Assuming that you are looking for One-To-Many bidirectional relationship then the mapping between the Person and Card entities will be as follows.

A Person can have many Cards so you need a set in Person class. The mapping is OneToMany so use @OneToMany and the mappedBy attribute tells what property in Card class represents the Person entity. I have used the property name person in the Card class so the mappedBy contains value as person.

@Entity(name="PERSON")
public class Person {
    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    private int id;

    @OneToMany(mappedBy = "person")
    private Set<Card> cards;
}

Now in Card class the relationship is ManyToOne towards the Person class so use the annotation @ManyToOne, here you can specify what is the name of the foreign key in Card table that points to the primary key of Person table. Here we can give any name to the foreign key, If you want to give it as "parent_id then add the annotation @JoinColumn(name = "parent_id")

@Entity(name="CARD")
public class Card {
    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    private int id;

    @ManyToOne
    @JoinColumn(name = "parent_id")
    private Person person;
}

Upvotes: 1

DiogoSantana
DiogoSantana

Reputation: 2434

Read the docs: http://docs.jboss.org/hibernate/orm/4.3/manual/en-US/html/ch05.html#d5e3678

Change the line:

private int person_id;

For:

@ManyToOne
@JoinColumn(name = "PERSON_ID")
private Person person;

Upvotes: 0

Related Questions