0

我面临以下问题:我有一个表 A 和 B 与表 A 的外键。一个实体具有以下字段: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实体?A:

@OneToOne(mappedBy = "idAddress")
private B b

此解决方案不起作用,查询语句后 A 实体中的字段 B b 为 NULL。

4

1 回答 1

2

长不是我有同样的例子的原因,像这样长时间尝试:

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;

}
于 2019-03-01T12:58:55.040 回答