I do have two entities called School and Level with a Many-To-Many relationship.
@Data
@Entity
public class School {
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "school_generator")
@SequenceGenerator(name="school_generator", sequenceName = "school_seq", allocationSize=1)
@Column(name = "school_id")
private Long id;
@NotBlank
private String name;
@NotBlank
private String city;
@ManyToMany(cascade = {CascadeType.PERSIST, CascadeType.MERGE})
@JoinTable(
name = "school_level",
joinColumns = @JoinColumn(name = "school_id"),
inverseJoinColumns = @JoinColumn(name = "level_id"))
private Set<Level> levels = new HashSet<>();
}
@Data
@Entity
public class Level implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "level_generator")
@SequenceGenerator(name = "level_generator", sequenceName = "level_seq", allocationSize = 1)
@Column(name = "level_id")
private Long id;
@NotBlank
@Column(nullable = false, unique = true)
private String name;
}
My SchoolController
, Rest controller, has a create method which takes a SchoolDTO (for now it has the same structure as the entity).
When I post the DTO, the nested child list Levels are populated only with existing Levels ids (since Levels are created previously).
When I try to save through the JPARepository save method...An exception with this message is thrown:
"detached entity passed to persist: com.salimrahmani.adawat.domain.Level; nested exception is org.hibernate.PersistentObjectException: detached entity passed to persist: com.salimrahmani.adawat.domain.Level",
"trace": "org.springframework.dao.InvalidDataAccessApiUsageException: detached entity passed to persist:
The problem is solved by iterating through each Level Id ...get the references by using the JPA Repository method getOne
(which calls entityManager.getReference
) and then loads the right model and then persist successfully.
@PostMapping("/{id}/grades")
public ResponseEntity<SchoolDTO> addLevelToPackage(@PathVariable Long id, @RequestBody @Valid LevelDTO levelDTO) {
return schoolService.findById(id)
.map(school -> {
Level level = levelMapper.map(levelDTO);
if(level.getId() != null) {
level = levelService.getOne(level.getId());
school.getLevels().add(level);
} // else error
School saved = schoolService.save(school);
return ResponseEntity.ok(schoolMapper.map(saved));
}).orElseGet(() -> ResponseEntity.notFound().build());
}
My question is: Is this the "only" correct way to posting association in Spring Data? Or, is there any better way?
In the addLevelToPackage
function Why using the map
schoolService.findById(id).map..
if as I think the schoolService will return only one instance of the entity School with the given id. I think replacing the map with the following would be enough:
...
School theSchool = schoolService.findById(id);
Level theLevel = levelService.getOne(levelDTO.getId());
theSchool.getLevels().add(theLevel);
School saved = schoolService.save(theSchool);
return ResponseEntity.ok(schoolMapper.map(saved));
...
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.