微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Spring:TransactionRequiredException:entitymanager.flush() 没有正在进行的事务

如何解决Spring:TransactionRequiredException:entitymanager.flush() 没有正在进行的事务

我知道有很多关于这个错误的帖子。但似乎没有什么对我有用。我在 entitymanager.flush() 处收到 TransactionrequiredException

我有配置类`

@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(
    entityManagerFactoryRef = “msEntityManagerFactory",transactionManagerRef = “msTransactionManager",basePackages = {
        x,y,z 
    })
public class msConfig {
    @Bean(name = “msDataSource")
    @ConfigurationProperties(prefix = "postgres.datasource.hikari")
    public DataSource msDataSource() {
        return DataSourceBuilder.create().build();
    }

    @Bean(name = “msEntityManagerFactory")
    public LocalContainerEntityManagerfactorybean entityManagerFactory(
                                                                       EntityManagerFactoryBuilder entityManagerFactoryBuilder,@Qualifier(“msDataSource") DataSource dataSource) {
        Properties properties = new Properties();
        properties.setProperty("hibernate.dialect","org.hibernate.dialect.PostgresqlDialect");

        LocalContainerEntityManagerfactorybean entityManager = entityManagerFactoryBuilder
                                                                                          .dataSource(dataSource)
                                                                                          .packages(“x”,”y”,“z”)
                                                                                          .persistenceUnit("postgres")
                                                                                          .build();

        entityManager.setJpaProperties(properties);

        return entityManager;
    }

    @Bean(name = “msTransactionManager")
    public PlatformTransactionManager transactionManager(@Qualifier(“msEntityManagerFactory") EntityManagerFactory entityManagerFactory) {
        return new JpaTransactionManager(entityManagerFactory);
    }
}

`

在我的服务类中,我从上面的类中注入 EntityManager

public Dummy(@Qualifier(“msEntityManagerFactory") EntityManager entityManager) {
    this.entityManager = entityManager;
}

在同一个类中,我有一个方法标记为@Transactional 并且有 entityManager.flush()

缺少什么?

编辑:

javax.persistence.TransactionrequiredException: no transaction is in progress
    at org.hibernate.internal.AbstractSharedSessionContract.checkTransactionNeededForUpdateOperation(AbstractSharedSessionContract.java:413)
    at org.hibernate.internal.SessionImpl.checkTransactionNeededForUpdateOperation(SessionImpl.java:3398)
    at org.hibernate.internal.SessionImpl.doFlush(SessionImpl.java:1355)
    at org.hibernate.internal.SessionImpl.flush(SessionImpl.java:1350)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:566)
    at org.springframework.orm.jpa.ExtendedEntityManagerCreator$ExtendedEntityManagerInvocationHandler.invoke(ExtendedEntityManagerCreator.java:366)
    at com.sun.proxy.$Proxy220.flush(UnkNown Source)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:566)
    at org.springframework.orm.jpa.SharedEntityManagerCreator$SharedEntityManagerInvocationHandler.invoke(SharedEntityManagerCreator.java:314)
    at com.sun.proxy.$Proxy220.flush(UnkNown Source)

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。