每次我想运行hibernate jpa应用程序时它都会挂起

Posted

技术标签:

【中文标题】每次我想运行hibernate jpa应用程序时它都会挂起【英文标题】:hibernate jpa application suspends everytime I want to run it 【发布时间】:2018-01-03 22:35:12 【问题描述】:

我正在使用带有 JPA 2.1 库的 Hibernate 5.2。每次我想运行我的应用程序时它都不会停止 - 我必须手动停止它,因为它仍在工作。记录已正确保存到数据库中。这些是我的项目文件:


Main.java


/**
 * @param args the command line arguments
 */
public static void main(String[] args) 
            EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory("testjpa");
    EntityManager em = entityManagerFactory.createEntityManager();

    em.getTransaction().begin();
            Kraj country = new Kraj();
            country.setNazwa("ExampleCountry");
            em.persist(country);
    em.getTransaction().commit();
    em.close();


**

peristence.xml

**

    <?xml version="1.0" encoding="UTF-8" ?>
<persistence xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
    version="2.0" xmlns="http://java.sun.com/xml/ns/persistence">
  <persistence-unit name="testjpa">
    <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
    <class>bazafilmow.model.Aktor</class>
    <class>bazafilmow.model.Film</class>
    <class>bazafilmow.model.Gatunek</class>
    <class>bazafilmow.model.Kraj</class>
    <class>bazafilmow.model.Rezyser</class>
    <shared-cache-mode>ENABLE_SELECTIVE</shared-cache-mode>
    <properties>
      <property name="javax.persistence.jdbc.url" value="jdbc:postgresql://localhost:5432/testdb"/>
      <property name="javax.persistence.jdbc.user" value="postgres"/>
      <property name="javax.persistence.jdbc.driver" value="org.postgresql.Driver"/>
      <property name="javax.persistence.jdbc.password" value="****"/>
      <property name="hibernate.flushMode" value="FLUSH_AUTO" />
      <property name="hibernate.hbm2ddl.auto" value="validate" />
    </properties>
  </persistence-unit>
</persistence>

输出


run:
Jan 03, 2018 11:23:06 PM org.hibernate.jpa.internal.util.LogHelper logPersistenceUnitInformation
INFO: HHH000204: Processing PersistenceUnitInfo [
    name: testjpa
    ...]
Jan 03, 2018 11:23:06 PM org.hibernate.Version logVersion
INFO: HHH000412: Hibernate Core 5.2.12.Final
Jan 03, 2018 11:23:06 PM org.hibernate.cfg.Environment <clinit>
INFO: HHH000206: hibernate.properties not found
Jan 03, 2018 11:23:06 PM org.hibernate.annotations.common.reflection.java.JavaReflectionManager <clinit>
INFO: HCANN000001: Hibernate Commons Annotations 5.0.1.Final
Jan 03, 2018 11:23:06 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
WARN: HHH10001002: Using Hibernate built-in connection pool (not for production use!)
Jan 03, 2018 11:23:06 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH10001005: using driver [org.postgresql.Driver] at URL [jdbc:postgresql://localhost:5432/testdb]
Jan 03, 2018 11:23:06 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH10001001: Connection properties: user=postgres, password=****
Jan 03, 2018 11:23:06 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH10001003: Autocommit mode: false
Jan 03, 2018 11:23:06 PM org.hibernate.engine.jdbc.connections.internal.PooledConnections <init>
INFO: HHH000115: Hibernate connection pool size: 20 (min=1)
Jan 03, 2018 11:23:06 PM org.hibernate.dialect.Dialect <init>
INFO: HHH000400: Using dialect: org.hibernate.dialect.PostgreSQL81Dialect
Jan 03, 2018 11:23:07 PM org.hibernate.engine.jdbc.env.internal.LobCreatorBuilderImpl useContextualLobCreation
INFO: HHH000424: Disabling contextual LOB creation as createClob() method threw error : java.lang.reflect.InvocationTargetException
Jan 03, 2018 11:23:07 PM org.hibernate.resource.transaction.backend.jdbc.internal.DdlTransactionIsolatorNonJtaImpl getIsolatedConnection
INFO: HHH10001501: Connection obtained from JdbcConnectionAccess [org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator$ConnectionProviderJdbcConnectionAccess@45cff11c] for (non-JTA) DDL execution was not in auto-commit mode; the Connection 'local transaction' will be committed and the Connection will be set into auto-commit mode.
Jan 03, 2018 11:23:07 PM org.hibernate.hql.internal.QueryTranslatorFactoryInitiator initiateService
INFO: HHH000397: Using ASTQueryTranslatorFactory

并且此时暂停,没有BUILD SUCCESSFULL信息。

【问题讨论】:

这看起来没什么问题,所以问题出在其他地方。可能在实体中。此外,由于您使用的是 Hibernate 5.2,因此您应该将 xml 中的 JPA 版本更改为 2.1,如下所示 &lt;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"&gt; 【参考方案1】:

更改适用于我的版本后,我遇到了同样的问题。

【讨论】:

这应该作为评论发布。

以上是关于每次我想运行hibernate jpa应用程序时它都会挂起的主要内容,如果未能解决你的问题,请参考以下文章

使用 micronaut-data-hibernate-jpa 库连接到 jpa 时出错

在hibernate中导入import.sql不成功

Spring + Hibernate + JPA [关闭]

如何配置 Hibernate 以在 Karaf JPA 示例中工作?

Spring 3.1 + Hibernate 4.1 JPA,Entity manager factory注册两次

如何使用 Hibernate JPA 和 Spring Framework 启用加载时间/运行时编织