我还想在使用 Spring 数据 JPA 保存子实体时保存父实体
I want to also save parent entity when save child entity by useing Spring data JPA
实体代码如下:
@Entity
@Table(name = "t_user")
public class User{
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
//omit other fields
@OneToOne(cascade = javax.persistence.CascadeType.ALL, mappedBy = "user")
private Student student;
}
@Entity
@Table(name = "t_student")
public class Student{
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
@OneToOne
@JoinColumn(name = "c_user_id", referencedColumnName = "id", foreignKey = @ForeignKey(ConstraintMode.NO_CONSTRAINT), columnDefinition = "INT COMMENT 'user number'")
private User user;
}
保存代码如下:
@Service
@Transactional(rollbackFor = Exception.class)
public class UserServiceImpl implements UserService {
@Autowired
private final UserRepository userRepository;
@Autowired
private final PasswordEncoder pwdEncoder;
@Override
public Optional<UserBody> add(UserParam param) {
User user = User.from(param);
if(userRepository.countByName(user.getName()) > 0){
throw new BusinessException("user already exists");
}
user.setPassword(pwdEncoder.encode(param.getPassword()));
userRepository.save(user);
user.getStudent().setUser(user); // do this for update the column `c_user_id` of student table
return Optional.of(UserBody.from(user));
}
}
如上,当我尝试保存一个用户时,它会自动保存一个学生,但是在t_student
table中,c_user_id
栏是空的。所以我必须编写代码 user.getStudent().setUser(user)
来更新列 c_user_id
.
那为什么呢?我应该怎么做才能让列 c_user_id
在保存用户
时自动填充
ps: 我不想改变 mappedBy
与 Student 的关系,我知道这可行,但我认为 User 可以是学生,也可以是另一个角色,所以如果我添加一些其他字符,它将修改 t_user
table。这导致table变得越来越臃肿
Student
得到保留。但是您指定 Student
负责维护 Student
和 User
之间的关系,并且由于 Student.user
是 null
这会在数据库中持久化。
因此,Hibernate 确实按照您的指示执行。
实体代码如下:
@Entity
@Table(name = "t_user")
public class User{
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
//omit other fields
@OneToOne(cascade = javax.persistence.CascadeType.ALL, mappedBy = "user")
private Student student;
}
@Entity
@Table(name = "t_student")
public class Student{
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
@OneToOne
@JoinColumn(name = "c_user_id", referencedColumnName = "id", foreignKey = @ForeignKey(ConstraintMode.NO_CONSTRAINT), columnDefinition = "INT COMMENT 'user number'")
private User user;
}
保存代码如下:
@Service
@Transactional(rollbackFor = Exception.class)
public class UserServiceImpl implements UserService {
@Autowired
private final UserRepository userRepository;
@Autowired
private final PasswordEncoder pwdEncoder;
@Override
public Optional<UserBody> add(UserParam param) {
User user = User.from(param);
if(userRepository.countByName(user.getName()) > 0){
throw new BusinessException("user already exists");
}
user.setPassword(pwdEncoder.encode(param.getPassword()));
userRepository.save(user);
user.getStudent().setUser(user); // do this for update the column `c_user_id` of student table
return Optional.of(UserBody.from(user));
}
}
如上,当我尝试保存一个用户时,它会自动保存一个学生,但是在t_student
table中,c_user_id
栏是空的。所以我必须编写代码 user.getStudent().setUser(user)
来更新列 c_user_id
.
那为什么呢?我应该怎么做才能让列 c_user_id
在保存用户
ps: 我不想改变 mappedBy
与 Student 的关系,我知道这可行,但我认为 User 可以是学生,也可以是另一个角色,所以如果我添加一些其他字符,它将修改 t_user
table。这导致table变得越来越臃肿
Student
得到保留。但是您指定 Student
负责维护 Student
和 User
之间的关系,并且由于 Student.user
是 null
这会在数据库中持久化。
因此,Hibernate 确实按照您的指示执行。