LazyInitializationException Spring Boot

Bru*_*uel 5 java spring spring-data-jpa

我知道那里有很多类似的线程,但我无法从这些线程中弄清楚如何克服这个问题。

我有 3 个类别的汽车、品牌、颜色。一辆汽车只有一个品牌和一系列颜色。品牌有一个汽车清单。颜色没有任何关系。

为简单起见,未提供 Getter、Setter、ToString 和构造函数。我能够将对象保存到数据库中,并且数据库已经填充。

-------------------------------------------------- ------------------------------

@Entity
@Table(catalog = "spring_project")
public class Car {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

private String model;

@ManyToMany(cascade=CascadeType.ALL, fetch = FetchType.LAZY)
@JoinTable( name = "car_color", catalog = "spring_project",
            joinColumns         = { @JoinColumn(name = "car_id") },
            inverseJoinColumns  = { @JoinColumn(name = "colors_id") }
)
private List<Color> colors = new ArrayList<>();

@ManyToOne(cascade=CascadeType.ALL, fetch = FetchType.LAZY)
@JoinColumn(name="brand_id", referencedColumnName="id")
private Brand brand;
Run Code Online (Sandbox Code Playgroud)

-------------------------------------------------- ------------------------------

@Entity
@Table(catalog = "spring_project")
public class Brand {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

private String name;

@OneToMany(mappedBy = "brand", fetch = FetchType.LAZY)
private List<Car> cars = new ArrayList<>();
Run Code Online (Sandbox Code Playgroud)

-------------------------------------------------- ------------------------------

@Entity
@Table(catalog = "spring_project")
public class Color {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String name;
Run Code Online (Sandbox Code Playgroud)

-------------------------------------------------- ------------------------------

如果我像 Eager 一样获取,一切都运行得很好,但我知道这是一种不好的做法,应该改用延迟加载。但我不断收到 LazyInitializationException。

我从错误中了解到需要一个会话,但我不知道如何提供一个会话,因为我使用 Spring Data JPA 也不应该在我应该声明的地方...

@SpringBootApplication
public class SrpingJpaApplication {

private static final Logger log = 
LoggerFactory.getLogger(SrpingJpaApplication.class);

public static void main(String[] args) {
    SpringApplication.run(SrpingJpaApplication.class, args);
}

@Bean
public CommandLineRunner demo(CarRepository carRepository,
                              ColorRepository colorRepository,
                              BrandRepository brandRepository) {
    return (args) -> {

        log.info("Reads all cars....");
        for (Car c : carRepository.findAll()) {
            System.out.println(c.toString());
        }

    };
}
Run Code Online (Sandbox Code Playgroud)

}

非常感谢。

已编辑----->>>

错误是在 c.toString() 上抛出的;

错误:由:org.hibernate.LazyInitializationException 引起:无法初始化代理 [com.readiness.moita.SrpingJPA.Models.Brand#1] - 没有会话

Rad*_*ZIN 6

注释的默认设置@OneToManyFetchType.LAZY延迟加载您的集合。

为了能够在检索对象后访问集合,您需要处于事务上下文中(您需要一个打开的会话)

你打电话时:

carRepository.findAll();
Run Code Online (Sandbox Code Playgroud)

在内部创建一个新会话,检索该对象,并且一旦该findAll方法返回,会话就会关闭。

您应该做的是确保每当您访问 Car 对象中的惰性集合时都有一个打开的会话(确实如此toString)。

最简单的方法是让另一个服务处理汽车装载并showCars用 注释该方法@Transactional。由于 AOP 代理的处理方式,该方法位于另一个服务中。

@Service
public CarService {

    final CarRepository carRepository;

    public CarService(CarRepository carRepository) {
        this.carRepository = carRepository;
    }    

    @Transactional
    public void showCars(String... args) {
        for (Car c : carRepository.findAll()) {
            System.out.println(c.toString());
        }
    }   
}
Run Code Online (Sandbox Code Playgroud)

然后你打电话:

@Bean
public CommandLineRunner demo(CarService carService) {
    return (args) -> service.showCars(args);
}
Run Code Online (Sandbox Code Playgroud)

任何用 注释的方法@Transactional从被调用的那一刻起直到它在调用者中终止时都会有一个打开的会话。


mad*_*fox 5

因为FetchTypeofBrand是懒惰的,它不会通过调用自动加载到会话中fetchAll()。要让它自动加载到会话中,您需要:

改变

@ManyToOne(cascade=CascadeType.ALL, fetch = FetchType.LAZY)
@JoinColumn(name="brand_id", referencedColumnName="id")
private Brand brand;
Run Code Online (Sandbox Code Playgroud)

@ManyToOne(cascade=CascadeType.ALL, fetch = FetchType.EAGER)
Run Code Online (Sandbox Code Playgroud)

前任

@ManyToOne(cascade=CascadeType.ALL, fetch = FetchType.EAGER)
@JoinColumn(name="brand_id", referencedColumnName="id")
private Brand brand;
Run Code Online (Sandbox Code Playgroud)

如果您不想将 fetch 类型设置为eager,那么您需要将您对 toString 的调用移动到一个服务方法 Ex

@Component
public CarService implements ICarService {

    @Autowired
    CarRepository carRepository;

    @Transactional
    public void printAllCars() {
        for (Car c : carRepository.findAll()) {
            System.out.println(c.toString());
        }
    }   
}
Run Code Online (Sandbox Code Playgroud)

然而,正确的方法是编写一个标准查询或 hql