Reputation: 907
I have two entities:
User.java
@Entity
@Data
@AllArgsConstructor
@NoArgsConstructor
@Builder
public class User implements UserDetails {
@Id
@GeneratedValue
Long id;
@NotBlank
String username;
@NotBlank
String password;
@ElementCollection
@Builder.Default
List<String> roles = new ArrayList<>();
@Override
public Collection<? extends GrantedAuthority> getAuthorities() {
return this.roles.stream().map(SimpleGrantedAuthority::new).collect(toList());
}
@Override
public String getUsername() {
return username;
}
@Override
public boolean isAccountNonExpired() {
return false;
}
@Override
public boolean isAccountNonLocked() {
return false;
}
@Override
public boolean isCredentialsNonExpired() {
return false;
}
@Override
public boolean isEnabled() {
return false;
}
}
Student.java
@Entity
@Data
public class Student {
@Id
String id;
String name;
String firstName;
@OneToOne
@JoinColumn(name = "user_id")
User user;
public void addUser(User user) {
this.user = user;
}
}
The StudentService
should add a new Student
, create a new User
and add it to the Student
@Override
public boolean addStudent(StudentDTO student) {
if (!studentRepository.existsById(student.getId())) {
Student s = modelMapper.map(student, Student.class);
studentRepository.save(s);
String username = String.format("s%s", student.getId());
List<String> roles = Arrays.asList("ROLE_STUDENT");
User user = userService.createUser(username, roles);
userService.addUser(user);
s.addUser(user);
return true;
}
return false;
}
The User
is being created by UserService
@Override
public User createUser(String username, List<String> roles) {
String password = RandomStringUtils.randomAlphanumeric(16);
User u = User.builder()
.username(username)
.password(encoder.encode(password))
.roles(roles)
.build();
if (addUser(u)) {
return u;
} else {
throw new UserAlreadyExistingException(username);
}
}
@Override
public boolean addUser(User user) {
boolean alreadyExists = userRepository.findByUsername(user.getUsername()).isPresent();
if (true) {
userRepository.save(user);
return true;
}
return false;
}
Despite the User
and the Student
are being added to my database, the user_id
column in the student
table is always set to null. I also checked if the User
being added to the Student
is properly initialized and filled with all the information but the transaction is committed.
Upvotes: 0
Views: 493
Reputation: 1438
user_id
is set to null because you are not saving mapping to database.
Changed code with comments:
@Override
public boolean addStudent(StudentDTO student) {
if (!studentRepository.existsById(student.getId())) {
Student s = modelMapper.map(student, Student.class);
// don't save student here
String username = String.format("s%s", student.getId());
List<String> roles = Arrays.asList("ROLE_STUDENT");
User user = userService.createUser(username, roles);
userService.addUser(user);
s.addUser(user);
//save student here after adding user.
studentRepository.save(s)
return true;
}
return false;
}
Upvotes: 1