LazyInitializationException

LazyInitializationException

本文介绍了使用Spring CrudRepository进行Hibernate LazyInitializationException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我总是得到异常:

  org.hibernate.LazyInitializationException:未能懒惰地初始化一个角色集合:com。 knapp.vk.domain.Business.businessCategorySet,无法初始化代理 - 无会话

我不能想要设置获取渴望。什么是其他好的解决方案?



业务实体类:

  @Entity 
public class Business
{
@Id
@GeneratedValue
private int pk;

@ManyToMany
私人套餐< BusinessCategory> businessCategorySet = new HashSet< BusinessCategory>();


}

BusinessRepository接口: p>

  import org.springframework.data.repository.CrudRepository; 
import org.springframework.transaction.annotation.Transactional;

@Transactional
public interface BusinessRepository扩展CrudRepository< Business,Integer>
{

}

配置:

  import javax.persistence.EntityManager; 
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.orm.hibernate4.HibernateExceptionTranslator;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;

@Configuration
@EnableJpaRepositories(basePackages =com.knapp.vk.repositorynew)
@EnableTransactionManagement
public class JPAConfiguration
{
@Bean
public EntityManagerFactory entityManagerFactory()抛出SQLException
{
返回Persistence.createEntityManagerFactory(standardManager);

$ b $Bean
public EntManagerManager entityManager(EntityManagerFactory entityManagerFactory)
{
return entityManagerFactory.createEntityManager();

$ b @Bean
public PlatformTransactionManager transactionManager()throws SQLException
{
JpaTransactionManager txManager = new JpaTransactionManager();
txManager.setEntityManagerFactory(entityManagerFactory());
返回txManager;


@Bean
public HibernateExceptionTranslator hibernateExceptionTranslator()
{
return new HibernateExceptionTranslator();


$ / code $ / pre

解决方案

您可以通过在Service中初始化businessCategoruSet来完成此操作,而不是在存储库中使用 @Transactional 在服务中使用它。
让我们写一些方法,比如通过id或业务列表在仓库中获取业务,并从服务中访问该方法。

  @Transactional 
public Class BusinessService {

@resource
BusinessRepository businessRepository;

public Business getBusiness(){
Business business = businessRepository.getBusiness();
//这里您应该初始化BusinessCategorySet
Object object = business.getBusinessCategoriesSet()。size();


有关的详细信息@交易


I always get the exception:

org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: com.knapp.vk.domain.Business.businessCategorySet, could not initialize proxy - no Session

I don´t want to set fetch to eager. What would be other good solutions?

Business Entity class:

@Entity
public class Business
{
    @Id
    @GeneratedValue
    private int pk;

    @ManyToMany
    private Set<BusinessCategory> businessCategorySet = new HashSet<BusinessCategory>();

...
}

BusinessRepository interface:

import org.springframework.data.repository.CrudRepository;
import org.springframework.transaction.annotation.Transactional;

@Transactional
public interface BusinessRepository extends CrudRepository<Business, Integer>
{

}

Config:

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.orm.hibernate4.HibernateExceptionTranslator;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;

@Configuration
@EnableJpaRepositories(basePackages = "com.knapp.vk.repositorynew")
@EnableTransactionManagement
public class JPAConfiguration
{
    @Bean
    public EntityManagerFactory entityManagerFactory() throws SQLException
    {
        return Persistence.createEntityManagerFactory("standardManager");
    }

    @Bean
    public EntityManager entityManager(EntityManagerFactory entityManagerFactory)
    {
        return entityManagerFactory.createEntityManager();
    }

    @Bean
    public PlatformTransactionManager transactionManager() throws SQLException
    {
        JpaTransactionManager txManager = new JpaTransactionManager();
        txManager.setEntityManagerFactory(entityManagerFactory());
        return txManager;
    }

    @Bean
    public HibernateExceptionTranslator hibernateExceptionTranslator()
    {
        return new HibernateExceptionTranslator();
    }
}
解决方案

So you can do it by initializing businessCategoruSet in Service so instead of using @Transactional in Repository use it in Service.Let write some method like get business by id or list of business in repository and access that method from service.

@Transactional
public Class BusinessService{

@resource
BusinessRepository businessRepository;

public Business getBusiness(){
  Business business = businessRepository.getBusiness();
  //Here you should initialize BusinessCategorySet
  Object object = business.getBusinessCategoriesSet().size();
  }
}

For more details about @Transactional Refer this link

这篇关于使用Spring CrudRepository进行Hibernate LazyInitializationException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-25 03:06