1)我有 2 个表,用户和 user_profile 与OneToOne
关系,
用户表:
id,user_name,first_name,last_name - ID 有主键,
用户资料:
id,name,user_id - ID 有主键,user_id 有外键和用户表
2) 以下是两个 JPA Entity 对象
用户:
@Entity
@Table(name = "user")
public class User{
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "id")
private Long id;
@Column(name = "user_name",unique=true,nullable=false)
private String name;
@Column(name = "first_name")
private String firstName;
@Column(name = "last_name")
private String lastName;
@OneToOne(cascade = {CascadeType.ALL})
@JoinColumn(name="user_id")
public UserProfile userProfile;
//with set/get methods
}
用户资料:
@Entity
@Table(name = "user_profile")
public class UserProfile {
@Id
@Column(name = "id")
@GeneratedValue(strategy=GenerationType.IDENTITY)
private Long id;
@Column(name = "name")
private String name;
@Column(name = "user_id")
private Long userId;
@OneToOne(mappedBy="userProfile")
private User user;
// with set/get methods
}
当我尝试插入两个表时,我看到列被添加到 USER 表中,并且在USER_PROFILE 表的列user_id
中看不到任何值。user_id
我是这个 MySQL 的新手,有人可以阐明如何解决这个问题吗?