Java:休眠@OneToOne 映射 [英] Java: Hibernate @OneToOne mapping

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

问题描述

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

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

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

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

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

我有一个 User 实体,如下所示:

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

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

Content 的类似实体,以及 Status 的另一个实体,如下所示:

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

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

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

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

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

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

另外值得注意的是:这应该支持一对零或一的关系,因为一些 usercontent 记录在 <代码>状态表.

Also worth noting: This should support a one-to-zero-or-one relationship, as some user and content records will not have a reference in the status table.

任何帮助将不胜感激!

推荐答案

您的 Status 实体不得具有 Integer 类型的属性 userIdcontentId,并使用 映射@列.它必须具有类型为 User 和 Content 的属性 usercontent,用 @OneToOne 映射:

Your Status entity must not have properties userId and contentId of type Integer, mapped with @Column. It must have properties user and content of type User and Content, mapped with @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.

这篇关于Java:休眠@OneToOne 映射的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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