gpt4 book ai didi

java - 使用 Spring CrudRepository 的 Hibernate LazyInitializationException

转载 作者:行者123 更新时间:2023-12-02 14:00:12 28 4
gpt4 key购买 nike

我总是遇到异常:

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

我不想将 fetch 设置为 eager。还有什么其他好的解决方案?

业务实体类:

@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>
{

}

配置:

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();
}
}

最佳答案

您还可以在存储库中添加方法:

@Transactional(readOnly = true)
public List<Business> findAllEagerly() {
CriteriaBuilder builder = em.getCriteriaBuilder();
CriteriaQuery<Business> query = builder.createQuery(Business.class);
Root<Business> root = query.from(Business.class);
root.fetch(Business_.businessCategorySet);
return em.createQuery(query).getResultList();
}

或更容易在 JpaRepository 中使用 @Query:

@Query("SELECT b FROM Business b JOIN FETCH b.businessCategorySet")
public List<Business> findAll();

我认为好的解决方案是使用@NamedEntityGraph

@Entity
@NamedEntityGraph(name = "Business.detail",
attributeNodes = @NamedAttributeNode("businessCategorySet"))
public class Business {...}

在存储库中

public interface BusinessRepository extends CrudRepository<Business, Integer> {
@EntityGraph(value = "Business.detail", type = EntityGraphType.LOAD)
List<Business> findAll();
}

另请参阅:

Custom implementations for Spring Data repositories

Configuring Fetch- and LoadGraphs

关于java - 使用 Spring CrudRepository 的 Hibernate LazyInitializationException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27115639/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com