我正在尝试进入休眠状态@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
}
当我执行读取操作时User
,我期望User.getStatus()
将返回一个Status
对象与id=1
。相反,我得到 AnnotationException:“引用的属性不是 (One|Many)ToOne:mappedBy User.status 中的 Status.userId”
我已经在此处详细介绍了文档、教程和示例,但到目前为止我尝试过的所有内容都失败了。
另外值得注意的是:这应该支持一对零或一的关系,因为某些user
and content
记录中不会有引用status
table.
任何帮助将不胜感激!