Hibernate @OneToOne 加入列

Hibernate @OneToOne join column

我有 2 个 table:用户和个人资料。用户可以在没有个人资料的情况下存在,但个人资料实体必须具有唯一的用户 ID。

+-----------------------------------+
|                user               |
+-----------------------------------+
| id |   nickname  |    password    |
+----+-------------+----------------+
|  1 |    admin    |      pass      |
+----+-------------+----------------+

+-----------------------------------+
|               profile             |
+-----------------------------------+
| id |   user_id   |     status     |
+----+-------------+----------------+
|  1 |     1       |  java forever  |
+----+-------------+----------------+

如何在@Entity class 中连接它们? 此代码不起作用:

@Entity
@Table(name="profiles")
public class Profile {
@Id
    @Column(name="id")
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @OneToOne
    @JoinColumn(name = "user")
    @JsonIgnore
    private User user;
}

@Entity
@Table(name = "users")
public class User {

    @Id
    @Column(name = "id")
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @Column(name = "nickname", length = 50, unique = true)
    @NotNull
    private String nickname;

    @OneToOne(mappedBy = "profile")
    Profile profile;
}

我可以选择将 'profile_id' 列给用户 table(它可以为空),但我正在尝试使用这种类型的数据库 table。 怎么了?

尝试添加 optional=false

@OneToOne(optional=false)
@JoinColumn(name = "user")
@JsonIgnore
private User user

何时:optional=false 由 NOT NULL

指定

因此,如果您确定一个个人资料仅属于一个用户,您的 类 可能如下所示:

@Table(name="profiles")
public class Profile {

(.....)

@Column(name="id", nullable=false, length=10)   
@Id 
@GeneratedValue(generator="PROFILE_ID_GENERATOR")   
@org.hibernate.annotations.GenericGenerator(name="PROFILE_ID_GENERATOR", strategy="native") 
private Integer id;

@Column(name="status", nullable=true, length=20)    
private String status;

@PrimaryKeyJoinColumn   
@OneToOne(optional=false, targetEntity=User.class, fetch=FetchType.LAZY)    
@org.hibernate.annotations.Cascade({org.hibernate.annotations.CascadeType.SAVE_UPDATE, org.hibernate.annotations.CascadeType.LOCK}) 
@JoinColumns({ @JoinColumn(name="userid", referencedColumnName="id", nullable=false) }) 
private User user;

@Column(name="userid", nullable=false, insertable=false, updatable=false)   
@Id 
@GeneratedValue(generator="PROFILE_USERID_GENERATOR")   
@org.hibernate.annotations.GenericGenerator(name="PROFILE_USERID_GENERATOR", strategy="foreign", parameters=@org.hibernate.annotations.Parameter(name="property", value="user"))    
private int userId;

(....)
}


public class User {
(.....)

@Column(name="id", nullable=false, length=10)   
@Id 
@GeneratedValue(generator="USER_ID_GENERATOR")  
@org.hibernate.annotations.GenericGenerator(name="USER_ID_GENERATOR", strategy="native")    
private int id;

@Column(name="nickname", nullable=true, length=25)  
private String nickname;

@Column(name="password", nullable=true, length=25)  
private String password;

@OneToOne(mappedBy="user", targetEntity=Profile.class, fetch=FetchType.LAZY)    
@org.hibernate.annotations.Cascade({org.hibernate.annotations.CascadeType.SAVE_UPDATE, org.hibernate.annotations.CascadeType.LOCK}) 
private Profile profile;

}