Hibernate没有设置外来约束

时间:2017-02-13 13:33:15

标签: java spring hibernate jpa orm

我正在使用SpringHibernate开发应用程序。下面是我的模型bean类:

员工

@Entity
@Table(name="employee")
public class Employee {
    @Id
    @Column
    @GeneratedValue
    private int empid;
    @Column
    private String firstName;
    @Column
    private String lastName;
    @Column
    private String email;
    @Column
    private String password;
    @Column
    private boolean isAdmin;

    @Column
    private boolean isActive;

    @Column
    private boolean isLocked;

//getter and setter

PersonalDetails

public class PersonalDetails {
    @Column
    @Id
    private int empid;
    @Column
    private String personalEmail;
    @Column
    private String mob;
    @Column
    private String permenantAdress;
    @Column
    private String currentAddress;
    @Column
    private String gender;
    @Column
    private String maritialStatus;

    @OneToOne
    @PrimaryKeyJoinColumn
    @ForeignKey(name="empid")
    private Employee employee;

//getter and setter

我的表在服务器启动时自动创建,但PersonalDetails表中的外键约束未设置。

我在这里错过了什么吗?

1 个答案:

答案 0 :(得分:1)

我认为您的@OneToOne映射应如下所示:

@MapsId
@OneToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "empid", referencedColumnName = "empid")
private Employee employee;

cascade属性是可选的,但这是一个很好的做法。