当前位置: 首页 > 知识库问答 >
问题:

Spring-data-jpa LazyInitializationException:无会话

赵镜
2023-03-14

我对Spring-data-jpa项目有一个问题。

JavaConfig文件

...

@Configuration
@EnableJpaRepositories("it.myproject.data")
@EnableTransactionManagement(mode = AdviceMode.ASPECTJ, proxyTargetClass = true)
@PropertySource("classpath:/it/myproject/application.properties")
public
        class DBConfig
{

    private static final
            String PROPERTY_NAME_ENTITYMANAGER_PACKAGES_TO_SCAN = "entitymanager.packages.to.scan";
    @Resource
    private
            Environment environment;

    @Bean
    public
            DataSource dataSource()
    {
        EmbeddedDatabaseBuilder builder = new EmbeddedDatabaseBuilder();
        return builder.setType(EmbeddedDatabaseType.H2).build();
    }

    @Bean
    public
            EntityManagerFactory entityManagerFactory()
    {
        HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
        vendorAdapter.setGenerateDdl(true);
        vendorAdapter.setShowSql(true);
        LocalContainerEntityManagerFactoryBean factory = new LocalContainerEntityManagerFactoryBean();
        factory.setJpaVendorAdapter(vendorAdapter);
        factory.setPackagesToScan(environment.getRequiredProperty(PROPERTY_NAME_ENTITYMANAGER_PACKAGES_TO_SCAN));
        factory.setDataSource(dataSource());
        factory.afterPropertiesSet();
        return factory.getObject();
    }

    @Bean
    public
            PlatformTransactionManager transactionManager()
    {
        JpaTransactionManager txManager = new JpaTransactionManager();
        txManager.setEntityManagerFactory(entityManagerFactory());
        return txManager;
    }
}
@Transactional
    public
            void doIt()
    {
        PersonDTO created = new PersonDTO();
        created.setId(null);
        created.setFirstName("Pluto");
        created.setLastName("Paperino");
        Person pippo= repositoryPersonService.create(created);
        for (int i = 0; i < 10; i++) {
            BookDTO bookDTO = new BookDTO();
            bookDTO.setTitle("Fantasia" + i);
            bookDTO.setPerson(pippo);
            repositoryBookService.create(bookDTO);
        }
        repositoryPersonService.findAll().stream().forEach((Person t) -> {
            System.out.println(t.getBooks());
        });
    }
@Entity
@Table(name = "persons")
public
        class Person
        implements Serializable
{

    private static final
            long serialVersionUID = 198765467898765L;
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private
            Long id;
    @Column(name = "creation_time", nullable = false)
    @Temporal(javax.persistence.TemporalType.DATE)
    private
            Date creationTime;
    @Column(name = "first_name", nullable = false)
    private
            String firstName;
    @Column(name = "last_name", nullable = false)
    private
            String lastName;
    @Column(name = "modification_time", nullable = false)
    @Temporal(javax.persistence.TemporalType.DATE)
    private
            Date modificationTime;
    @OneToMany(fetch = FetchType.LAZY)
    private
            List<Book> books;
    @Version
    private
            long version = 0;

2014-04-09 12:31:54跟踪LazyInitializationException:53-未能懒散初始化角色的集合:It.MyProject.Data.Person.Person.Books,无法初始化代理-没有会话org.Hibernate.LazyInitializationException:未能懒散初始化角色的集合:It.MyProject.Data.Person.Person.Books,无法初始化代理-没有会话

你能帮帮我吗?谢谢你

共有1个答案

王磊
2023-03-14

解决了。我从配置中删除了“(mode=adviceMode.aspectJ,proxyTargetClass=true)”,现在它运行良好。他的家伙们!

之前->

@Configuration
@EnableJpaRepositories("it.myproject.data")
@EnableTransactionManagement(mode = AdviceMode.ASPECTJ, proxyTargetClass = true)
@PropertySource("classpath:/it/myproject/application.properties")

现在->

@Configuration
@EnableJpaRepositories("it.myproject.data")
@EnableTransactionManagement
@PropertySource("classpath:/it/myproject/application.properties")
 类似资料:
  • 我遵循了教程http://www.baeldung.com/spring-data-rest-relationships。我还注意到,我可以通过提供到关系的链接直接创建关联。 但是,如果我在Kotlin中使用一个数据类,我会得到以下错误 2018-04-26 14:13:43.730错误79256---[nio-8080-exec-2]B.E.H.RestResponseEntityExcepti

  • 我目前正在开发一个使用Spring Boot和Spring Data(准确地说是其接口)以及Hibernate的应用程序。 我喜欢Hiberante的一个特性是它的缓存特性--当您提交多个与特定对象匹配的查询时,您将在每次执行查询时返回该对象的相同实例(相对于Java的==运算符)。但是,在使用Spring数据和类时,情况似乎并不总是这样。因此,我假设这里有多个实例在工作。 因此,我的问题是:Sp

  • 我看了一下下面的问题 与SpringDataJPA相比,使用SpringDataREST有哪些优势? 它不太符合我的需要。我的数据库在MYSQL上,我选择了Spring-Data-JPA实现。REST能给我带来哪些我在简单的Spring-Data-JPA中找不到的额外优势?例如,如果明天,我决定实现缓存b/w我的业务和数据库模块,在这种情况下,我将不得不写较小的代码?哪个容易配置?哪一个更灵活,如

  • 问题内容: 我有一个名为EmployeeDepartment的实体,如下所示 我有一个如下定义的Spring Data Repository 此外,我注册了一个转换器,以将String转换为EmployeeDepartmentPK。 现在,对于一个由ID employeeID =“ abc123”和departmentCode =“ JBG”限定的实体,我希望在调用SDR接口时使用的ID为abc1

  • 我试图从Spring data JPA中使用CrudRepository。我想我错过了一些配置。 spring-servlet.xml ServletContext资源[/web-inf/spring-servlet.XML]中的XML文档第15行无效;嵌套异常是org.xml.sax.SAXParseException;行号:15;列号:4;CVC-ELT.1:找不到元素“beans”的声明。在