Hibernate @OneToOne mappedBy

481 views Asked by At

I'm facing the following problem: I have one table A and B with a foreign key to table A. An entity has the following field: 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 entity has a field (among others):

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;

How to add the field of type B to A entity? A:

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

This solution doesn't work, field B b in A entity is NULL after the query statement.

1

There are 1 answers

7
i.karayel On BEST ANSWER

the long is not the reason I have the same example with long try like this:

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;

}