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

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

问题描述



假设我有一个名为<$ c的表$ c> status 看起来像这样:

  + --------- --------------------------------------- + 
|状态|
+ --------------------------------------------- --- +
| id | frn_user_id | frn_content_id |状态|
+ ---- + ------------- + ---------------- + --------- --- +
| 1 | 111 | 0 | 主动|
+ ---- + ------------- + ---------------- + --------- --- +
| 2 | 0 | 222 | 无效|
+ ---- + ------------- + ---------------- + --------- --- +

我有一个用于 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)
私人状态状态;

// getters and setters
}

一个用于 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)
私人字符串状态;

// getters and setters
}

当我执行阅读 User ,我期望 User.getStatus()将返回一个 Status 具有 id = 1 的对象。相反,我得到一个AnnotationException:引用的属性不是(One | Many)ToOne:Status.userId在mappedBy User.status中



教程和示例,但是我迄今为止尝试过的所有内容都失败了。

另外值得注意的是:这应该支持一对一或者一对一关系,因为某些用户内容记录不会在状态中引用 code> table。



任何帮助将不胜感激!

您的状态实体不得具有Integer类型的属性 userId contentId ,其映射为 @Column 。它必须具有User和Content类型的属性 user content ,映射到 @OneToOne

  public class User {
@OneToOne(mappedBy =user)
私人状态状态;
// ...
}

公共班级状态{
@OneToOne
@JoinColumn(name =frn_user_id)
private用户用户;
// ...
}

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


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

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" |
+----+-------------+----------------+------------+

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
}

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
}

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"

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

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.

Any help would be greatly appreciated!

解决方案

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:Hibernate @OneToOne映射的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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