java - 带有 Hibernate 的 PostgreSQL 没有持久化我的实体

标签 java hibernate postgresql

我正在使用 Hibernate 和 PostgreSQL 构建一个应用程序。

但是当我调用 persist(entity) 方法时,我的 table 上什么也没有出现。

我的 persistence.xml:

<?xml version="1.0" encoding="UTF-8"?>
<persistence 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"
    version="2.1">
    <persistence-unit name="myPersistenceUnit">
        <provider>org.hibernate.ejb.HibernatePersistence</provider>
        <!-- Annotated entity classes -->
        <class>br.com.programadoremjava.MyEntity</class>
        <properties>
            <property name="hibernate.connection.url" value="jdbc:postgresql://localhost/netshoes" />
            <property name="hibernate.connection.driver_class" value="org.postgresql.Driver" />
            <property name="hibernate.connection.username" value="postgres" />
            <property name="hibernate.connection.password" value="admin" />
            <property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQLDialect" />
            <property name="hibernate.hbm2ddl.auto" value="create" />
        </properties>
    </persistence-unit>
</persistence>

我的持久方法:

public void persist(MyEntity myEntity) {
    Persistence.createEntityManagerFactory("myPersistenceUnit")
    .createEntityManager().persist(myEntity);
}

我的日志输出:

14:05:10,122 INFO  [org.hibernate.Version] (http-localhost/127.0.0.1:80-1) HHH000412: Hibernate Core {4.3.9.Final}
14:05:10,135 INFO  [org.hibernate.cfg.Environment] (http-localhost/127.0.0.1:80-1) HHH000206: hibernate.properties not found
14:05:10,140 INFO  [org.hibernate.cfg.Environment] (http-localhost/127.0.0.1:80-1) HHH000021: Bytecode provider name : javassist
14:05:10,440 INFO  [org.hibernate.annotations.common.Version] (http-localhost/127.0.0.1:80-1) HCANN000001: Hibernate Commons Annotations {4.0.5.Final}
14:05:10,515 WARN  [org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl] (http-localhost/127.0.0.1:80-1) HHH000402: Using Hibernate built-in connection pool (not for production use!)
14:05:10,515 INFO  [org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl] (http-localhost/127.0.0.1:80-1) HHH000401: using driver [org.postgresql.Driver] at URL [jdbc:postgresql://localhost/netshoes]
14:05:10,515 INFO  [org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl] (http-localhost/127.0.0.1:80-1) HHH000046: Connection properties: {user=postgres, password=****}
14:05:10,515 INFO  [org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl] (http-localhost/127.0.0.1:80-1) HHH000006: Autocommit mode: false
14:05:10,515 INFO  [org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl] (http-localhost/127.0.0.1:80-1) HHH000115: Hibernate connection pool size: 20 (min=1)
14:05:11,078 INFO  [org.hibernate.dialect.Dialect] (http-localhost/127.0.0.1:80-1) HHH000400: Using dialect: org.hibernate.dialect.PostgreSQLDialect
14:05:11,104 INFO  [org.hibernate.engine.jdbc.internal.LobCreatorBuilder] (http-localhost/127.0.0.1:80-1) HHH000424: Disabling contextual LOB creation as createClob() method threw error : java.lang.reflect.InvocationTargetException
14:05:11,294 INFO  [org.hibernate.hql.internal.ast.ASTQueryTranslatorFactory] (http-localhost/127.0.0.1:80-1) HHH000397: Using ASTQueryTranslatorFactory
14:05:11,824 INFO  [org.hibernate.tool.hbm2ddl.SchemaExport] (http-localhost/127.0.0.1:80-1) HHH000227: Running hbm2ddl schema export
14:05:11,889 WARN  [org.hibernate.engine.jdbc.spi.SqlExceptionHelper] (http-localhost/127.0.0.1:80-1) SQL Warning Code: 0, SQLState: 00000
14:05:11,891 WARN  [org.hibernate.engine.jdbc.spi.SqlExceptionHelper] (http-localhost/127.0.0.1:80-1) análise de S_3: COMMIT
14:05:11,893 WARN  [org.hibernate.engine.jdbc.spi.SqlExceptionHelper] (http-localhost/127.0.0.1:80-1) SQL Warning Code: 0, SQLState: 00000
14:05:11,894 WARN  [org.hibernate.engine.jdbc.spi.SqlExceptionHelper] (http-localhost/127.0.0.1:80-1) StartTransactionCommand
14:05:11,896 WARN  [org.hibernate.engine.jdbc.spi.SqlExceptionHelper] (http-localhost/127.0.0.1:80-1) SQL Warning Code: 0, SQLState: 00000
14:05:11,897 WARN  [org.hibernate.engine.jdbc.spi.SqlExceptionHelper] (http-localhost/127.0.0.1:80-1) ligação de <unnamed> para S_3
14:05:11,899 WARN  [org.hibernate.engine.jdbc.spi.SqlExceptionHelper] (http-localhost/127.0.0.1:80-1) SQL Warning Code: 0, SQLState: 00000
14:05:11,900 WARN  [org.hibernate.engine.jdbc.spi.SqlExceptionHelper] (http-localhost/127.0.0.1:80-1) executar S_3: COMMIT
14:05:11,902 WARN  [org.hibernate.engine.jdbc.spi.SqlExceptionHelper] (http-localhost/127.0.0.1:80-1) SQL Warning Code: 0, SQLState: 00000
14:05:11,903 WARN  [org.hibernate.engine.jdbc.spi.SqlExceptionHelper] (http-localhost/127.0.0.1:80-1) ProcessUtility
14:05:11,905 WARN  [org.hibernate.engine.jdbc.spi.SqlExceptionHelper] (http-localhost/127.0.0.1:80-1) SQL Warning Code: 0, SQLState: 00000
14:05:11,906 WARN  [org.hibernate.engine.jdbc.spi.SqlExceptionHelper] (http-localhost/127.0.0.1:80-1) CommitTransactionCommand
14:05:11,908 WARN  [org.hibernate.engine.jdbc.spi.SqlExceptionHelper] (http-localhost/127.0.0.1:80-1) SQL Warning Code: 0, SQLState: 00000
14:05:11,909 WARN  [org.hibernate.engine.jdbc.spi.SqlExceptionHelper] (http-localhost/127.0.0.1:80-1) CommitTransaction
14:05:11,910 WARN  [org.hibernate.engine.jdbc.spi.SqlExceptionHelper] (http-localhost/127.0.0.1:80-1) SQL Warning Code: 0, SQLState: 00000
14:05:11,912 WARN  [org.hibernate.engine.jdbc.spi.SqlExceptionHelper] (http-localhost/127.0.0.1:80-1) name: unnamed; blockState:           END; state: INPROGR, xid/subid/cid: 0/1/0, nestlvl: 1, children: 
14:05:11,914 WARN  [org.hibernate.engine.jdbc.spi.SqlExceptionHelper] (http-localhost/127.0.0.1:80-1) SQL Warning Code: 0, SQLState: 00000
14:05:11,915 WARN  [org.hibernate.engine.jdbc.spi.SqlExceptionHelper] (http-localhost/127.0.0.1:80-1) CommitTransaction
14:05:11,917 WARN  [org.hibernate.engine.jdbc.spi.SqlExceptionHelper] (http-localhost/127.0.0.1:80-1) SQL Warning Code: 0, SQLState: 00000
14:05:11,918 WARN  [org.hibernate.engine.jdbc.spi.SqlExceptionHelper] (http-localhost/127.0.0.1:80-1) name: unnamed; blockState:       STARTED; state: INPROGR, xid/subid/cid: 0/1/0, nestlvl: 1, children: 
14:05:12,028 INFO  [org.hibernate.tool.hbm2ddl.SchemaExport] (http-localhost/127.0.0.1:80-1) HHH000230: Schema export complete
14:08:31,775 INFO  [org.jboss.ejb.client] (http-localhost/127.0.0.1:443-1) JBoss EJB Client version 1.0.25.Final-redhat-1

我错过了什么? 为什么实体不持久保存在 postgres 中?

有人可以帮助我吗?

最佳答案

您应该尝试管理您的交易。

EntityManager em = emf.createEntityManager();
EntityTransaction tx = null;
try {
    tx = em.getTransaction();
    tx.begin();

    // do some work
    ...

    tx.commit();
}
catch (RuntimeException e) {
    if ( tx != null && tx.isActive() ) tx.rollback();
    throw e; // or display error message
}
finally {
    em.close();
}

关于java - 带有 Hibernate 的 PostgreSQL 没有持久化我的实体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30083590/

相关文章:

java - 如何读取标记 x 和 y

java - 如何使用 Spring Integration 在 Ftp 和本地文件夹之间同步多个文件夹中的文件?

hibernate - 在 Hibernate 中更新后如何重新加载 Spring Security Principal?

java - Hibernate:在同一个应用程序中使用两个不同的数据库模式

sql - 使用 postgresql 更新基于其他列的列

java - Spring Data JPA 中的动态@Query WHERE 子句

java - 单元测试中@Nonnull 注释的 Intellij IDEA 警告

java - 批量更新从更新 [0] 返回了意外的行数;

java - PostgreSQL Between 子句

php - PostgreSQL 多条件语句