Java:冬眠@OneToOne映射
2022-09-01 16:30:20
我试图让Hibernate注释工作,但在这里没有取得多大成功......@OneToOne
假设我有一个名为这样的表:status
+------------------------------------------------+
| status |
+------------------------------------------------+
| id | frn_user_id | frn_content_id | status |
+----+-------------+----------------+------------+
| 1 | 111 | 0 | "active" |
+----+-------------+----------------+------------+
| 2 | 0 | 222 | "inactive" |
+----+-------------+----------------+------------+
我有一个实体,看起来像这样: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
}
一个类似的 ,另一个实体如下所示: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
}
当我执行读取时,我希望这将返回一个带有.相反,我得到一个NotessellException:“引用的属性不是(一|Many)ToOne: Status.userId in mappedBy User.status”User
User.getStatus()
Status
id=1
我已经在SO上阅读了文档,教程和示例,但是到目前为止,我尝试过的所有内容都失败了。
还值得注意的是:这应该支持一对零或一的关系,因为某些和记录在表中没有引用。user
content
status
任何帮助将不胜感激!