[英]Hibernate @OneToOne join column
I have 2 tables: user and profile. 我有2个表格:用户和个人资料。 User can exists without profile, but profile entity must have unique user id.
用户可以不存在个人资料而存在,但是个人资料实体必须具有唯一的用户ID。
+-----------------------------------+
| user |
+-----------------------------------+
| id | nickname | password |
+----+-------------+----------------+
| 1 | admin | pass |
+----+-------------+----------------+
+-----------------------------------+
| profile |
+-----------------------------------+
| id | user_id | status |
+----+-------------+----------------+
| 1 | 1 | java forever |
+----+-------------+----------------+
How i can connect them in @Entity class? 我如何在@Entity类中连接它们? This code does not work:
此代码不起作用:
@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;
}
Optionally i can put 'profile_id' column to user table (and it can be null), but i'm trying to use this type of DB tables. 可选地,我可以将'profile_id'列放到用户表中(它可以为null),但是我正在尝试使用这种类型的数据库表。 What is wrong?
怎么了?
try add optional=false
尝试添加
optional=false
@OneToOne(optional=false)
@JoinColumn(name = "user")
@JsonIgnore
private User user
when: optional=false
is specify by NOT NULL when:
optional=false
由NOT NULL指定
So if you are sure, one profile belongs only to one user your classes could look like this: 因此,如果您确定,一个配置文件仅属于一个用户,则您的类可能如下所示:
@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;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.