OneToOne 关系 hibernate+spring-data-jpa null value in owning schema
OneToOne relationship hibernate+spring-data-jpa null value in owning schema
我是 Hibernate 和 JPA 的新手(我主要使用 JDBC 进行存储过程集成)。我创建了两个具有 OneToOne 关系的实体 User 和 UserPassword。我正在尝试在 tables (MySQL DB) 中存储值,但 UserPassword table 的 UserId (foreign_key) 列存储空值,而密码被存储。请更正我在以下代码中的错误:
@Entity
@Table(name = "User")
public class User implements Serializable{
private static final long serialVersionUID = -3366411610525655274L;
@Column(name = "UserId", nullable = false,unique = true)
@GenericGenerator(name = "uuid2", strategy = "uuid2")
@GeneratedValue(generator = "uuid2")
@Id
@Type(type="uuid-char")
private UUID userId;
@Embedded
private Name name;
@Column(name = "DateOfBirth", nullable = false)
private Date dob;
@OneToOne(mappedBy="user", cascade=CascadeType.ALL)
private Password password;
public Password getPassword() {
return password;
}
public void setPassword(Password password) {
this.password = password;
}
public UUID getUserId() {
return userId;
}
public void setUserId(UUID userId) {
this.userId = userId;
}
public Name getName() {
return name;
}
public void setName(Name name) {
this.name = name;
}
public Date getDob() {
return dob;
}
public void setDob(Date dob) {
this.dob = dob;
}
}
和
@Entity
@Table(name= "UserPassword")
public class Password implements Serializable{
private static final long serialVersionUID = -8990341903052492314L;
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(name="PasswordId")
private Long Id;
@Column(name="Password")
private String password;
@OneToOne(cascade=CascadeType.ALL,fetch=FetchType.LAZY)
@JoinColumn(name="UserId", referencedColumnName="UserId")
private User user;
public Long getId() {
return Id;
}
public void setId(Long id) {
Id = id;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
这些是我的 JPA 存储库:
public interface UserRepository extends JpaRepository<User, UUID>{
}
和
public interface PasswordRepository extends JpaRepository<Password, Long>{
}
和用于在数据库中保存实体的服务层代码:
public void insertUsers(List<User> users) {
List<com.poc.entity.User> usersData = ObjectMapperUtils.mapAll(users, com.poc.entity.User.class);
userRepository.saveAll(usersData);
}
此外,请帮助我为这项工作设计正确的方法。
它通过对服务层逻辑进行小的修改来工作。
public void insertUsers(List<User> users) {
List<com.poc.entity.User> usersData = ObjectMapperUtils.mapAll(users, com.poc.entity.User.class);
usersData = usersData.stream().map(user->mapUserPassWordEntity(user)).collect(Collectors.toList());
userRepository.saveAll(usersData);
}
private com.poc.entity.User mapUserPassWordEntity(com.poc.entity.User user) {
Password password = new Password();
password.setPassword(user.getPassword().getPassword());
//set parent reference to child
password.setUser(user);
// set child reference to parent
user.setPassword(password);
return user;
}
不过,如果能提出更多更好的方法建议,我将不胜感激。
我是 Hibernate 和 JPA 的新手(我主要使用 JDBC 进行存储过程集成)。我创建了两个具有 OneToOne 关系的实体 User 和 UserPassword。我正在尝试在 tables (MySQL DB) 中存储值,但 UserPassword table 的 UserId (foreign_key) 列存储空值,而密码被存储。请更正我在以下代码中的错误:
@Entity
@Table(name = "User")
public class User implements Serializable{
private static final long serialVersionUID = -3366411610525655274L;
@Column(name = "UserId", nullable = false,unique = true)
@GenericGenerator(name = "uuid2", strategy = "uuid2")
@GeneratedValue(generator = "uuid2")
@Id
@Type(type="uuid-char")
private UUID userId;
@Embedded
private Name name;
@Column(name = "DateOfBirth", nullable = false)
private Date dob;
@OneToOne(mappedBy="user", cascade=CascadeType.ALL)
private Password password;
public Password getPassword() {
return password;
}
public void setPassword(Password password) {
this.password = password;
}
public UUID getUserId() {
return userId;
}
public void setUserId(UUID userId) {
this.userId = userId;
}
public Name getName() {
return name;
}
public void setName(Name name) {
this.name = name;
}
public Date getDob() {
return dob;
}
public void setDob(Date dob) {
this.dob = dob;
}
}
和
@Entity
@Table(name= "UserPassword")
public class Password implements Serializable{
private static final long serialVersionUID = -8990341903052492314L;
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(name="PasswordId")
private Long Id;
@Column(name="Password")
private String password;
@OneToOne(cascade=CascadeType.ALL,fetch=FetchType.LAZY)
@JoinColumn(name="UserId", referencedColumnName="UserId")
private User user;
public Long getId() {
return Id;
}
public void setId(Long id) {
Id = id;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
这些是我的 JPA 存储库:
public interface UserRepository extends JpaRepository<User, UUID>{
}
和
public interface PasswordRepository extends JpaRepository<Password, Long>{
}
和用于在数据库中保存实体的服务层代码:
public void insertUsers(List<User> users) {
List<com.poc.entity.User> usersData = ObjectMapperUtils.mapAll(users, com.poc.entity.User.class);
userRepository.saveAll(usersData);
}
此外,请帮助我为这项工作设计正确的方法。
它通过对服务层逻辑进行小的修改来工作。
public void insertUsers(List<User> users) {
List<com.poc.entity.User> usersData = ObjectMapperUtils.mapAll(users, com.poc.entity.User.class);
usersData = usersData.stream().map(user->mapUserPassWordEntity(user)).collect(Collectors.toList());
userRepository.saveAll(usersData);
}
private com.poc.entity.User mapUserPassWordEntity(com.poc.entity.User user) {
Password password = new Password();
password.setPassword(user.getPassword().getPassword());
//set parent reference to child
password.setUser(user);
// set child reference to parent
user.setPassword(password);
return user;
}
不过,如果能提出更多更好的方法建议,我将不胜感激。