I set up two datasources in Spring Boot. In a controller I want to persist a couple of parent-child entities on both. They’re almost identical bidirectional relationships, but the first one works, while the second emits a committing message but doesn’t actually persist the child entity. It seems like when UserCommit it’s added to User entity, the latter is already unmanaged. How can I log to check if that’s the case?
Entities from working datasource
@Entity
public class Brand {
private Integer id;
private Set<Line> lines = new HashSet<Line>();
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
@JsonIgnore
public Integer getId() {
return id;
}
@JsonIgnore
@OneToMany(mappedBy = "brand", cascade = CascadeType.ALL, orphanRemoval = true)
public Set<Line> getLines() {
return lines;
}
@Entity
public class Line {
private Integer id;
private Brand brand;
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
public Integer getId() {
return id;
}
@ManyToOne
@JoinColumn(name = "brand")
public Brand getBrand() {
return brand;
}
while this ones are from second datasource, that persists the parent but not the child
@Entity
public class User {
private String id;
private List<UserCommit> userCommitList = new ArrayList<UserCommit>();
@Id
public String getId() {
return id;
}
@OneToMany(mappedBy = "user", cascade = CascadeType.ALL, orphanRemoval = true)
public List<UserCommit> getUserCommitList() {
return userCommitList;
}
@Entity
public class UserCommit {
private Integer id;
private User user;
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
public Integer getId() {
return id;
}
@ManyToOne
@JoinColumn(name = "user")
public User getUser() {
return user;
}
This is the the interesting part of the controller
Brand brand = brandService.createFrom(fsDto.getBrand());
Line line = lineService.createFrom(fsDto.getLine(), brand);
AccessToken accToken = token.getAccount().getKeycloakSecurityContext().getToken();
try {
prodService.updateFrom(id, line, fsDto);
User user = userService.createFrom(accToken);
ucService.createFrom(user, id, "EDIT", "FIRST");
And these are the service methods
public Brand createFrom(String name) {
Brand foundBrand = findByName(name);
Brand brand = new Brand();
if (foundBrand == null) {
brand = brandRepo.save(brand);
brand.setName(name);
} else
brand = foundBrand;
return brand;
public Line createFrom(String name, Brand brand) {
Line foundLine = findByNameAndBrand(name, brand);
Line line = new Line();
if (foundLine == null) {
line.setName(name);
line.setBrand(brand);
line.getBrand().addLine(line);
} else
line = foundLine;
return line;
}
public User createFrom(AccessToken accToken) {
User user = new User();
user.setId(accToken.getSubject());
user.setName(accToken.getPreferredUsername());
return userRepo.save(user);
}
public UserCommit createFrom(User user, Integer prodId, String ucType, String stage) {
UserCommit uc = new UserCommit();
uc.setUser(user);
uc.getUser().addUserCommit(uc);
uc.setProdId(prodId);
uc.setUCType(UserCommit.UCType.valueOf(ucType));
uc.setStage(UserCommit.Stage.valueOf(stage));
uc.setTime(LocalDateTime.now());
return uc;
}
- Brand gets persisted.
- Line gets Persisted.
- User gets persisted.
- UserCommit DON’T get persisted.
- Everything works if I explicitly
call UserCommitService.save(), but I think it shouldn’t be needed.