I am new to Hibernate and JPA (I worked mostly with stored procedure integration using JDBC.). I created two entities User and UserPassword with OneToOne relationship. I am trying to store values in both the tables (MySQL DB) but UserId (foreign_key) column of the UserPassword table stores null whereas the password gets stored. Please correct my mistake in below code:
@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;
}
}
and
@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;
}
}
These are the my JPA repositories:
public interface UserRepository extends JpaRepository<User, UUID>{
}
and
public interface PasswordRepository extends JpaRepository<Password, Long>{
}
and service layer code to save entities in database:
public void insertUsers(List<User> users) {
List<com.poc.entity.User> usersData = ObjectMapperUtils.mapAll(users, com.poc.entity.User.class);
userRepository.saveAll(usersData);
}
Also, please help me in proper designing approach for this work.
It worked by doing small modification in service layer logic.
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;
}
Still, I would appreciate more suggestions for better approach.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.