我总是得到例外:
org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: com.knapp.vk.domain.Business.businessCategorySet, could not initialize proxy - no Session
Run Code Online (Sandbox Code Playgroud)
我不想将 fetch 设置为热切。还有什么其他好的解决方案?
业务实体类:
@Entity
public class Business
{
@Id
@GeneratedValue
private int pk;
@ManyToMany
private Set<BusinessCategory> businessCategorySet = new HashSet<BusinessCategory>();
...
}
Run Code Online (Sandbox Code Playgroud)
业务仓库接口:
import org.springframework.data.repository.CrudRepository;
import org.springframework.transaction.annotation.Transactional;
@Transactional
public interface BusinessRepository extends CrudRepository<Business, Integer>
{
}
Run Code Online (Sandbox Code Playgroud)
配置:
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") …Run Code Online (Sandbox Code Playgroud)