首页 文章

Spring Boot JPA Hibernate persistence.xml Not Inserting

提问于
浏览
1

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

我正在使用Tomcat作为WAR可执行文件运行Spring启动(.1.5.3) . 我在Spring Boot上使用persistence.xml(hibernate5-ddl-maven-plugin <在构建期间生成SQL DDL) .

在调试时,我看到JpaTransactionManager创建新事务(使用名称创建新事务...打开新的EntityManager ,,,将JPA事务暴露为JDBC事务),加入其他@Transactional方法(找到线程绑定的EntityManager ...参与现有事务)并提交TX(启动事务提交...在EntityManager上提交JPA事务),并关闭JPA EM(关闭JPA EntityManager) . 这一切都是根据@Transactional规范发生的 . 但是,记录未插入数据库 . 我没有看到任何hibernate事务消息 .

以下是我的一些配置 .

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;
}

}

1 回答

  • 0

    答对了!!!!我用@PersistenceContext(unitName = PERSISTENCE_UNIT_NAME)注释了我的EntityManager . 之后它才有效 .

    这是解决上述问题的代码 . 请注意下面的@PersistenceContext注释(这是修复问题的原因) . 之前我在em上有@Inject注释 .

    @PersistenceContext(unitName = PERSISTENCE_UNIT_NAME)
    protected EntityManager em;
    

相关问题