@OneToOne使用@JoinColumn进行双向映射 [英] @OneToOne bidirectional mapping with @JoinColumn

查看:565
本文介绍了@OneToOne使用@JoinColumn进行双向映射的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有Person

Let's say I have Person

class Person{
    @Id Integer id;

    @OneToOne
    @JoinColumn(name = "person_id")
    Job myJob;
}

和工作

and Job

class Job{
    @Id Integer id;
    Integer person_id;

    @OneToOne
    @PrimaryKeyJoinColumn(name = "person_id")
    Person currentWorker;
}

我无法将Person和Job映射到其他实体,获取。

我在做什么错误?

I'm not able to map the Person and Job to other Entity, when fetching.
What mistake am I doing ?

推荐答案

您的代码应该是:

@Entity
public class Person implements Serializable {

    @Id Integer id;

    @OneToOne
    @JoinColumn(name = "id")
    Job myJob;
}

@Entity
public class Job implements Serializable {

    @Id Integer id;

    @OneToOne(mappedBy = "myJob")
    Person currentWorker;
}  

(支付试图从Job中移除重复的colum'person_id')

(pay attemption to remove duplicated colum 'person_id' from Job)

或其他共享主键的方法:

or other approach sharing primary key:

@Entity
public class Person {
    @Id Integer id;

    @OneToOne(cascade = CascadeType.ALL)
    @PrimaryKeyJoinColumn
    Job myJob;
}            

@Entity
public class Job {
    @Id Integer id;
} 

这篇关于@OneToOne使用@JoinColumn进行双向映射的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆