Hibernate @OneToOne连接列

时间:2017-06-14 11:06:24

标签: java hibernate orm hibernate-mapping one-to-one

我有2个表:用户和个人资料。用户可以不存在配置文件,但配置文件实体必须具有唯一的用户ID。

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

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

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

@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'列到用户表(它可以为null),但我试图使用这种类型的数据库表。 有什么问题?

2 个答案:

答案 0 :(得分:0)

尝试添加optional=false

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

何时:optional=false由NOT NULL指定

答案 1 :(得分:0)

因此,如果您确定,一个配置文件只属于一个用户,您的类可能如下所示:

@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;

}