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

时间:2015-05-06 17:24:16

标签: java hibernate postgresql

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

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

我的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>

我的persistMethod:

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?

有人可以帮助我吗?

1 个答案:

答案 0 :(得分:1)

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

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