solution is to use @LazyCollection(LazyCollectionOption.FALSE) instead of FetchType
This commit is contained in:
parent
de7cc57fd4
commit
ee092b86e1
BIN
data.mv.db
BIN
data.mv.db
Binary file not shown.
@ -1,7 +1,5 @@
|
||||
package ru.ulstu.is.sbapp.repair.model;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonIgnore;
|
||||
|
||||
import javax.persistence.*;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
@ -18,18 +16,18 @@ public class Component {
|
||||
@Column(name = "amount")
|
||||
private Integer amount;
|
||||
|
||||
@ManyToMany(mappedBy = "componentsList")
|
||||
private List<Favor> favorsList;
|
||||
@ManyToMany(fetch = FetchType.EAGER, mappedBy = "componentsList", cascade = CascadeType.REMOVE)
|
||||
private List<Favor> favorsListFromComponents;
|
||||
|
||||
public Component() {
|
||||
favorsList = new ArrayList<>();
|
||||
favorsListFromComponents = new ArrayList<>();
|
||||
|
||||
}
|
||||
|
||||
public Component(String componentName, Integer amount) {
|
||||
this.componentName = componentName;
|
||||
this.amount = amount;
|
||||
favorsList = new ArrayList<>();
|
||||
favorsListFromComponents = new ArrayList<>();
|
||||
}
|
||||
|
||||
public Long getId() {
|
||||
@ -53,14 +51,14 @@ public class Component {
|
||||
}
|
||||
|
||||
public List<Favor> getFavors() {
|
||||
return favorsList;
|
||||
return favorsListFromComponents;
|
||||
}
|
||||
|
||||
public void addFavor(Favor favor) {
|
||||
if (favorsList == null)
|
||||
favorsList = new ArrayList<>();
|
||||
if (!favorsList.contains(favor)){
|
||||
favorsList.add(favor);
|
||||
if (favorsListFromComponents == null)
|
||||
favorsListFromComponents = new ArrayList<>();
|
||||
if (!favorsListFromComponents.contains(favor)){
|
||||
favorsListFromComponents.add(favor);
|
||||
}
|
||||
if (!favor.getComponents().contains(this)) {
|
||||
favor.addComponent(this);
|
||||
|
@ -1,6 +1,8 @@
|
||||
package ru.ulstu.is.sbapp.repair.model;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonIgnore;
|
||||
import org.hibernate.annotations.LazyCollection;
|
||||
import org.hibernate.annotations.LazyCollectionOption;
|
||||
|
||||
import javax.persistence.*;
|
||||
import java.util.ArrayList;
|
||||
@ -18,10 +20,11 @@ public class Favor {
|
||||
@Column(name = "price")
|
||||
private Integer price;
|
||||
|
||||
@ManyToMany(fetch = FetchType.EAGER, mappedBy = "favorsList")
|
||||
@ManyToMany(fetch = FetchType.EAGER)
|
||||
private List<Order> ordersList;
|
||||
|
||||
@ManyToMany //(fetch = FetchType.EAGER)
|
||||
@ManyToMany
|
||||
@LazyCollection(LazyCollectionOption.FALSE)
|
||||
private List<Component> componentsList;
|
||||
|
||||
public Favor() {
|
||||
|
@ -17,10 +17,7 @@ public class Order {
|
||||
@Column(name = "date")
|
||||
private Date date;
|
||||
|
||||
@ManyToMany (fetch = FetchType.EAGER)
|
||||
@JoinTable(name = "order_favor",
|
||||
joinColumns = @JoinColumn(name = "order_fk"),
|
||||
inverseJoinColumns = @JoinColumn(name = "favor_fk"))
|
||||
@ManyToMany (fetch = FetchType.EAGER, mappedBy = "ordersList", cascade = CascadeType.REMOVE)
|
||||
private List<Favor> favorsList;
|
||||
|
||||
public Order(){
|
||||
|
@ -26,13 +26,15 @@ class SbappApplicationTests {
|
||||
@Test
|
||||
void testOrder(){
|
||||
componentService.deleteAllComponent();
|
||||
//orderService.deleteAllOrder();
|
||||
//favorService.deleteAllFavor(); why?
|
||||
orderService.deleteAllOrder();
|
||||
favorService.deleteAllFavor();
|
||||
|
||||
final Favor favor = favorService.addFavor("Favor1", 100);
|
||||
|
||||
final Order order0 = orderService.addOrder("11.02.2023");
|
||||
final Order order1 = orderService.findOrder(order0.getId());
|
||||
final Component component1 = componentService.addComponent("comp", 10);
|
||||
favor.addComponent(component1);
|
||||
Assertions.assertEquals(order0, order1);
|
||||
|
||||
orderService.addFavorToOrder(order0.getId(), favor);
|
||||
@ -58,7 +60,7 @@ class SbappApplicationTests {
|
||||
|
||||
favorService.addComponentToFavor(favor0.getId(), component);
|
||||
Assertions.assertEquals(favorService.findFavor(favor0.getId()).getComponents().size(), 1);
|
||||
//Assertions.assertEquals(componentService.findComponent(component.getId()).getFavor(), favor0);
|
||||
Assertions.assertEquals(componentService.findComponent(component.getId()).getFavors().size(), 1);
|
||||
|
||||
favorService.deleteFavor(favor0.getId());
|
||||
Assertions.assertThrows(EntityNotFoundException.class, () -> favorService.findFavor(favor0.getId()));
|
||||
|
Loading…
x
Reference in New Issue
Block a user