Java:休眠@OneToOne 映射

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21762328/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 10:31:57  来源:igfitidea点击:

Java: Hibernate @OneToOne mapping

javahibernatejpaone-to-onehibernate-annotations

提问by Shaun Scovil

I'm trying to get Hibernate @OneToOne annotations working and not having much success here...

我正在尝试让 Hibernate @OneToOne 注释正常工作,但在这里没有取得太大的成功......

Let's say I've got a table called statusthat looks like this:

假设我有一个名为的表status,如下所示:

+------------------------------------------------+
|                     status                     |
+------------------------------------------------+
| id | frn_user_id | frn_content_id |   status   |
+----+-------------+----------------+------------+
|  1 |     111     |        0       |  "active"  |
+----+-------------+----------------+------------+
|  2 |      0      |       222      | "inactive" |
+----+-------------+----------------+------------+

And I've got an entity for Userthat looks like this:

我有一个User看起来像这样的实体:

@Entity
@Table(name = "user")
public class User {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "id", nullable = false)
    private Integer id;

    @OneToOne(cascade = CascadeType.ALL, fetch = FetchType.EAGER, mappedBy = "userId")
    private Status status;

    // getters and setters
}

And a similar one for Content, and another entity for Statusthat looks like this:

和一个类似的 for Content,另一个实体Status看起来像这样:

@Entity
@Table(name = "status")
public class Status {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "id", nullable = false)
    private Integer id;

    @Column(name = "frn_user_id")
    private Integer userId;

    @Column(name = "frn_content_id")
    private Integer contentId;

    @Column(name = "status")
    private String status;

    // getters and setters
}

When I perform a read on User, I expect that User.getStatus()will return a Statusobject with id=1. Instead, I get an AnnotationException: "Referenced property not a (One|Many)ToOne: Status.userId in mappedBy User.status"

当我执行读取时User,我希望它User.getStatus()会返回一个Status带有id=1. 相反,我得到一个 AnnotationException:“引用的属性不是(一个|多)ToOne:映射的 User.status 中的 Status.userId”

I've poured through docs, tutorials and examples here on SO, but everything I've tried so far has failed.

我已经在 SO 上倾注了文档、教程和示例,但到目前为止我尝试过的一切都失败了。

Also worth noting: This should support a one-to-zero-or-one relationship, as some userand contentrecords will not have a reference in the statustable.

另外值得一提的是:这应该支持一到零或一的关系,因为一些usercontent记录不会有在参考status表。

Any help would be greatly appreciated!

任何帮助将不胜感激!

采纳答案by JB Nizet

Your Status entity must not have properties userIdand contentIdof type Integer, mapped with @Column. It must have properties userand contentof type User and Content, mapped with @OneToOne:

您的 Status 实体不得具有Integer 类型的属性,userIdcontentId使用@Column. 它必须具有的属性usercontent类型的用户和内容,与映射的@OneToOne

public class User {
    @OneToOne(mappedBy = "user")
    private Status status;
    // ...
}

public class Status {
    @OneToOne
    @JoinColumn(name = "frn_user_id")
    private User user;
    // ...
}

A user has one status. A status has one user.

一个用户有一个状态。一个状态有一个用户。

回答by Abhishek

use this way. Add below field in Customer entity.

使用这种方式。在客户实体中添加以下字段。

@OneToOne(fetch = FetchType.LAZY, cascade = CascadeType.ALL, mappedBy = "customer")
private Status status;

and don't add getter and setter for status field in Customer entity. And add below code to Status Entity.

并且不要为 Customer 实体中的状态字段添加 getter 和 setter。并将以下代码添加到状态实体​​。

@OneToOne(fetch = FetchType.LAZY, optional = false)
@JoinColumn(name = "customer_id", nullable = false)
private Customer customer;

Here add the getter and setter for customer field in Status Entity class. You can see one working example here.

这里为状态实体类中的客户字段添加 getter 和 setter。您可以在此处查看一个工作示例。