添加JpaTransactionManager配置时无法插入数据

时间:2018-12-26 21:22:39

标签: spring spring-boot spring-data-jpa

我将JpaTransactionManager添加到了我的应用程序中,但是由于某种原因,根本不执行INSERT查询。我使用以下配置:

  <div class="card-body">

    <div class="row align-items-center mb-4">
      <div class="col">
        <h1>Data and Tools</h1>
      </div>
    </div>

      <a href="/reports">
        <div class="row my-4 align-items-center clickable">
          <div class="col my-3">
            <span class="semi-bold">Reports</span>
            <div class="stamp-md">
              Create, run and schedule reports.
            </div>
          </div>
          <div class="col-1 text-right">
            <i class="fas fa-chevron-right gray-light small"></i>
          </div>
        </div>
      </a>
      <hr class="inner-divider">

application.properties:

@Configuration
@EnableTransactionManagement
public class ContextDatasource {

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

    @Bean
    public PlatformTransactionManager transactionManager(final EntityManagerFactory emf) {
        final JpaTransactionManager transactionManager = new JpaTransactionManager();
        transactionManager.setEntityManagerFactory(emf);
        return transactionManager;
    }

    @Bean
    public PersistenceExceptionTranslationPostProcessor exceptionTranslation() {
        return new PersistenceExceptionTranslationPostProcessor();
    }
}

插入代码:

spring.jmx.enabled=false
spring.datasource.jndi-name=java:/global/app
spring.datasource.driver-class-name=org.mariadb.jdbc.Driver
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MariaDBDialect
spring.jpa.show-sql = true
spring.jpa.hibernate.ddl-auto = update

您知道我缺少一些配置吗?

1 个答案:

答案 0 :(得分:1)

为了将EntityManager置于持久性上下文中,请更改:

@Autowired  
private EntityManager entityManager;

收件人

@PersistenceContext
private EntityManager entityManager;