2017-06-17 114 views
1

我遇到插入无法正常工作的问题。读取(选择)工作正常。但是,在我的@Transactional方法结束时没有执行插入操作。即使我看到实体管理器上正在调用事务提交/关闭。我尝试了不同的配置,但仍然无法在事务结束时获取记录(插入)。我没有看到任何错误正在产生。我启用了hibernate.transaction DEBUG,并且没有看到任何hibernate事务消息。Spring启动JPA休眠+ persistence.xml不插入

我正在使用Tomcat作为WAR可执行文件运行Spring引导(.1.5.3)。我在Spring Boot中使用persistence.xml(hibernate5-ddl-maven-plugin <需要在生成过程中生成SQL DDL)。在调试时,我看到JpaTransactionManager创建新的事务(使用name创建新事务...打开新的EntityManager ,,,将JPA事务公开为JDBC事务),加入其他@Transactional方法(找到线程绑定的EntityManager ..参与现有事务)并提交TX(Initiating transaction commit ...在EntityManager上提交JPA事务),并关闭JPA EM(关闭JPA EntityManager)。这一切都根据@Transactional规范进行。但是,该记录未插入到数据库中。我没有看到任何休眠事务消息。

下面是我的一些配置。

的persistence.xml:

<persistence version="2.1" 
     xmlns="http://xmlns.jcp.org/xml/ns/persistence" 
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
     xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd"> 

<!-- 
This file is needed to generate the DDL. 
--> 

<persistence-unit 
    name="EzListaPersistence" 
    transaction-type="RESOURCE_LOCAL"> 

    <description> 
     The set of entity types that can be managed by a 
     given entity manager is defined by a persistence unit. A 
     persistence unit defines the set of all classes that are 
     related or grouped by the application, and which must be 
     collocated in their mapping to a single data store. 
    </description> 

    <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider> 

    <!-- 
     List of fully qualified Entity Classes 
    --> 

    <class>com.ezlista.domain.account.Account</class> 
    ...... NOT DISPLAYED .... 
    <class>com.ezlista.domain.useraccount.Notification</class> 

    <properties> 
     <!-- Hibernate Connection Settings --> 
     <property 
      name="hibernate.connection.provider_class" 
      value="org.hibernate.hikaricp.internal.HikariCPConnectionProvider" /> 
     <property 
      name="hibernate.hikari.dataSourceClassName" 
      value="com.mysql.jdbc.jdbc2.optional.MysqlDataSource" /> 
     <property 
      name="hibernate.hikari.dataSource.url" 
      value="jdbc:mysql://localhost:3306/EZLISTA?verifyServerCertificate=false&amp;useSSL=false" /> 
     <property 
      name="hibernate.hikari.dataSource.user" 
      value="rubens" /> 
     <property 
      name="hibernate.hikari.dataSource.password" 
      value="***MASKED***" /> 
     <property 
      name="hibernate.hikari.dataSource.cachePrepStmts" 
      value="true" /> 
     <property 
      name="hibernate.hikari.dataSource.prepStmtCacheSize" 
      value="250" /> 
     <property 
      name="hibernate.hikari.dataSource.prepStmtCacheSqlLimit" 
      value="2048" /> 
     <property 
      name="hibernate.hikari.minimumIdle" 
      value="5" /> 
     <property 
      name="hibernate.hikari.maximumPoolSize" 
      value="10" /> 
     <property 
      name="hibernate.hikari.idleTimeout" 
      value="30000" /> 


     <!-- SQL Settings --> 
     <property name="hibernate.dialect" 
        value="org.hibernate.dialect.MySQL5InnoDBDialect" /> 
     <property name="hibernate.show_sql" 
        value="false" /> 
     <property name="hibernate.format_sql" 
        value="true" /> 
     <property name="hibernate.use_sql_comments" 
        value="false" /> 
     <property name="hibernate.ddl-auto" 
        value="none" /> 

     <!-- Hibernate Cache Settings --> 
     <property name="hibernate.javax.cache.provider" 
        value="org.ehcache.jsr107.EhcacheCachingProvider" /> 
     <property name="hibernate.cache.region.factory_class" 
        value="org.hibernate.cache.jcache.JCacheRegionFactory" /> 
     <property name="hibernate.cache.use_second_level_cache" 
        value="true" /> 
     <property name="hibernate.cache.use_query_cache" 
        value="false" /> 
     <property name="hibernate.cache.use_structured_entries" 
        value="true" /> 
     <property name="hibernate.cache.use_minimal_puts" 
        value="true" /> 
    </properties> 

</persistence-unit> 

Java的配置:

@EntityScan("com.ezlista.domain") 
@EnableTransactionManagement 

/** 
* Spring Bootstrap Repository Configuration. 
* 
* @author Rubens Gomes 
*/ 
@Configuration 
public class RepositoryConfiguration 
{ 
private final static Logger logger = LoggerFactory.getLogger(RepositoryConfiguration.class); 
private final static String PERSISTENCE_UNIT = "EzListaPersistence"; 

@Inject 
private Environment env; 

public RepositoryConfiguration() 
{ 
    super(); 
    logger.debug("Constructed"); 
} 


// DataSource 
@Bean(name = "dataSource") 
public DataSource dataSource() 
{ 
    logger.info("Registering HikariDataSource bean."); 
    HikariDataSource ds = new HikariDataSource(); 

    ds.setDataSourceClassName(env.getRequiredProperty("hikari.dataSourceClassName")); 
    ds.setMinimumIdle(env.getRequiredProperty("hikari.minimumIdle", Integer.class)); 
    ds.setMaximumPoolSize(env.getRequiredProperty("hikari.maximumPoolSize", Integer.class)); 
    ds.setIdleTimeout(env.getRequiredProperty("hikari.idleTimeout", Integer.class)); 
    ds.setPoolName(env.getRequiredProperty("hikari.poolName")); 

    ds.addDataSourceProperty("user", env.getRequiredProperty("hikari.dataSource.user")); 
    ds.addDataSourceProperty("password", env.getRequiredProperty("hikari.dataSource.password")); 
    ds.addDataSourceProperty("databaseName", env.getRequiredProperty("hikari.dataSource.databaseName")); 
    ds.addDataSourceProperty("serverName", env.getRequiredProperty("hikari.dataSource.serverName")); 
    ds.addDataSourceProperty("cachePrepStmts", env.getRequiredProperty("hikari.dataSource.cachePrepStmts", Boolean.class)); 
    ds.addDataSourceProperty("prepStmtCacheSize", env.getRequiredProperty("hikari.dataSource.prepStmtCacheSize", Integer.class)); 
    ds.addDataSourceProperty("prepStmtCacheSqlLimit", env.getRequiredProperty("hikari.dataSource.prepStmtCacheSqlLimit", Integer.class)); 

    return ds; 
} 


// EntityManagerFactory 
@Bean(name = "entityManagerFactory") 
public EntityManagerFactory entityManagerFactory() 
{ 
    logger.info("Registering EntityManagerFactory bean."); 
    JpaVendorAdapter hibernateJpavendorAdapter = new HibernateJpaVendorAdapter(); 
    JpaDialect hibernateJpaDialect = new HibernateJpaDialect(); 

    LocalContainerEntityManagerFactoryBean emfBean = 
      new LocalContainerEntityManagerFactoryBean(); 
    emfBean.setJpaVendorAdapter(hibernateJpavendorAdapter); 
    emfBean.setJpaDialect(hibernateJpaDialect); 
    emfBean.setPersistenceUnitName(PERSISTENCE_UNIT); 
    emfBean.setDataSource(dataSource()); 
    emfBean.afterPropertiesSet(); 
    return emfBean.getObject(); 
} 


// TransactionManager 
@Bean(name = "transactionManager") 
public PlatformTransactionManager transactionManager() 
{ 
    logger.info("Registering JpaTransactionManager bean."); 

    JpaTransactionManager txManager = new JpaTransactionManager(); 
    EntityManagerFactory emf = entityManagerFactory(); 
    txManager.setEntityManagerFactory(emf); 
    txManager.setDataSource(dataSource()); 
    return txManager; 
} 

} 

回答

0

宾果!!!! 我用@PersistenceContext注释了我的EntityManager(unitName = PERSISTENCE_UNIT_NAME)。之后,它的作品。

下面是修复上述问题的代码。注意下面的@PersistenceContext注解(这是解决问题的方法)。在我进行@Inject注解之前。

@PersistenceContext(unitName = PERSISTENCE_UNIT_NAME) 
protected EntityManager em; 
+0

Hi @Rubens Gomes,你可以看看我的代码,看看我做错了什么吗? https://stackoverflow.com/questions/47624373/how-to-implement-springs-transactional-annotation-using-hibernate-hikaricp-an/ – jNewbie