Hibernate在另一个表中将表id作为外键插入

时间:2014-09-25 05:28:25

标签: java hibernate jpa

我有2张桌子PERSON和CARD

我希望我的表CARD将PERSON_ID作为FK,我想在hibernate实体类中这样做。

enter image description here

有没有办法通过Java Classes和注释来实现?

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

}

我的卡片实体课程

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


}

在卡片类中做什么,以便我可以在需要的卡片表中输入person_id。

2 个答案:

答案 0 :(得分:2)

Person中,您希望使用Card属性以及card_no实体中保留Card信息,以获得person_id属性。所以你要找的是双向关系。假设您正在寻找One-To-Many双向关系,那么PersonCard实体之间的映射将如下所示。

Person可以包含多个Cards,因此您需要在Person类中设置一个集合。映射是OneToMany,因此使用@OneToManymappedBy属性告诉Card类中的哪个属性代表Person实体。我在person类中使用了属性名称Card,因此mappedBy包含的值为person

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

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

现在在Card类中,ManyToOne类的关系为Person,因此请使用注释@ManyToOne,在这里您可以指定外键的名称是什么Card表指向Person表的主键。在这里,我们可以为外键指定任何名称,如果要将其作为"parent_id,则添加注释@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;
}

答案 1 :(得分:0)

阅读文档:http://docs.jboss.org/hibernate/orm/4.3/manual/en-US/html/ch05.html#d5e3678

更改行:

private int person_id;

有关:

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