休眠@OneToOne mappedBy
Hibernate @OneToOne mappedBy
我面临以下问题:
我有一个 table A 和 B,外键指向 table A。
实体具有以下字段:
答:
public class A{
@Column(name = "id_adres", nullable = false)
private Long idAddress;
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(name = "id_adres", nullable = false)
private Long idAddress;
B 实体有一个字段(以及其他字段):
public class B{
@Id
@Column(name = "id")
@GeneratedValue(strategy=GenerationType.IDENTITY)
private Long id;
@Column(name = "id_adres", nullable = false)
private Long idAddress;
@Column(name = "id_adres", nullable = false)
private Long idAddress;
如何将B类型的字段添加到A实体中?
答:
@OneToOne(mappedBy = "idAddress")
private B b
这个方案不行,查询语句后A实体中的字段B b为NULL
长并不是我有相同示例的原因,长尝试是这样的:
public class UserToken {
@OneToOne(targetEntity = User.class)
@JoinColumn(nullable = false, name = "user_id")
private long tokenId;
}
public class User {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "user_id", unique = true, nullable = false)
private long userId;
}
我面临以下问题: 我有一个 table A 和 B,外键指向 table A。 实体具有以下字段: 答:
public class A{
@Column(name = "id_adres", nullable = false)
private Long idAddress;
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(name = "id_adres", nullable = false)
private Long idAddress;
B 实体有一个字段(以及其他字段):
public class B{
@Id
@Column(name = "id")
@GeneratedValue(strategy=GenerationType.IDENTITY)
private Long id;
@Column(name = "id_adres", nullable = false)
private Long idAddress;
@Column(name = "id_adres", nullable = false)
private Long idAddress;
如何将B类型的字段添加到A实体中? 答:
@OneToOne(mappedBy = "idAddress")
private B b
这个方案不行,查询语句后A实体中的字段B b为NULL
长并不是我有相同示例的原因,长尝试是这样的:
public class UserToken {
@OneToOne(targetEntity = User.class)
@JoinColumn(nullable = false, name = "user_id")
private long tokenId;
}
public class User {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "user_id", unique = true, nullable = false)
private long userId;
}