i'am facing with interesting solution of LazyInitializationException. To prevent this (on OneToMany oder ManyToMany) one known solution is, to use JOIN FETCH Query. You can see one of severals examples her: https://thoughts-on-java.org/best-practices-for-many-to-many-associations-with-hibernate-and-jpa/
Other easier solution is, to use @Transactional from Spring. For example like this:
@DeleteMapping(value ="/product/{tagId}")
@ResponseBody
@Transactional
public String deleteProductWithoutRelation(@PathVariable String product, Model model) {
Optional<Product> pr = productService.selectProduct(product);
if (pr.isPresent()) {
tag.get().getCustomer().size(); //usualy throws LazyInitializationException,
//without JOIN-FETCH Statment or @Transactional
return deletedTagId;
}
Of course, you can place @Transactional of some method from repository service, to encapsulate this solution. So which Advantages or Disadvantages of both solutions are here?