English 中文(简体)
.persist() returns no errors, but where is my persisted data?
原标题:

In my DAO implementation I m doing Stripersist.getEntityManager().persist(client);, this doesn t seem to return any errors, but I can t find the data that it persists.

My client object looks like this :

@Entity
public class Client implements Serializable
{

    @Id
    @GeneratedValue
    private Integer id;
    @Column
    private String name;

    //accessors etc here
 ...
 }

I have one persistence unit and is as follows

<persistence-unit name="stripes" transaction-type="RESOURCE_LOCAL">
      <!-- Tell JPA to use Hibernate -->
        <provider>org.hibernate.ejb.HibernatePersistence</provider>
       <!--Hibernate settings -->
        <properties>
         <!--Autodetect entity classes -->
            <property name="hibernate.archive.autodetection" value="class"/>
         <!--Automatically create the SQL schema -->
            <property name="hibernate.hbm2ddl.auto" value="create"/>
       <!--Tell Hibernate to use HSQLDB -->
            <property name="hibernate.dialect"
        value="org.hibernate.dialect.DerbyDialect" />
            <property name="hibernate.connection.driver_class"
        value="org.apache.derby.jdbc.ClientDriver" />
       <!--Configure the JDBC database connection-->
            <property name="hibernate.connection.url" value="jdbc:derby://localhost:1527/salestracker" />
            <property name="hibernate.connection.username" value="admin"/>
            <property name="hibernate.connection.password" value="admin"/>
            <property name="jdbc.batch_size" value="0"/>
       <!--Configure the connection pool--> 
            <property name="hibernate.c3p0.min_size" value="5"/>
            <property name="hibernate.c3p0.max_size" value="20"/>
            <property name="hibernate.c3p0.timeout" value="300"/>
            <property name="hibernate.c3p0.max_statements" value="50"/>
            <property name="hibernate.c3p0.idle_test_period" value="3000"/>
        </properties>
    </persistence-unit>

Like I say, I ve set a breakpoint on the .persist(client);, the client has a name set, and has null for an id (otherwise I d have used .merge()), therefore I m not sure as to why the data is not persisted?

If I look at the table, CLIENT, it has 0 rows.

SELECT * FROM ADMIN.CLIENT

Executed successfully in 0.001 s.
Line 1, column 1

Execution finished after 0.001 s, 0 error(s) occurred.

Is it possible that the entity is being persisted, but the transaction is not being committed?

Thanks

EDIT : Here is some logging info

INFO: 09:24:32,002 DEBUG JpaTransactionManager:285 - Found thread-bound EntityManager [org.hibernate.ejb.EntityManagerImpl@4e65ad52] for JPA transaction
INFO: 09:24:32,002 DEBUG JpaTransactionManager:371 - Creating new transaction with name [com.jameselsey.salestracker.service.ClientService.persistClient]: PROPAGATION_REQUIRED,ISOLATION_DEFAULT
INFO: 09:24:32,002 DEBUG JDBCTransaction:82 - begin
INFO: 09:24:32,003 DEBUG ConnectionManager:444 - opening JDBC connection
INFO: 09:24:32,003 DEBUG JDBCTransaction:87 - current autocommit status: true
INFO: 09:24:32,003 DEBUG JDBCTransaction:90 - disabling autocommit
INFO: break
INFO: 09:24:40,960 DEBUG SessionImpl:247 - opened session at timestamp: 12600914809
INFO: 09:24:40,961 DEBUG JDBCTransaction:82 - begin
INFO: 09:24:40,962 DEBUG ConnectionManager:444 - opening JDBC connection
INFO: 09:24:40,965 DEBUG JDBCTransaction:87 - current autocommit status: true
INFO: 09:24:40,966 DEBUG JDBCTransaction:90 - disabling autocommit
INFO: 09:24:40,993 DEBUG AbstractBatcher:585 - opening JDBC connection
INFO: 09:24:40,993 DEBUG DriverManagerConnectionProvider:132 - opening new JDBC connection
INFO: 09:24:40,996 DEBUG DriverManagerConnectionProvider:138 - created connection to: jdbc:derby://localhost:1527/salestracker, Isolation Level: 2
INFO: 09:24:40,997 DEBUG SQL:111 - select next_hi from hibernate_unique_key for read only with rs
INFO: 09:24:41,001 DEBUG SQL:111 - update hibernate_unique_key set next_hi = ? where next_hi = ?
INFO: 09:24:41,006 DEBUG AbstractBatcher:606 - closing JDBC connection (open PreparedStatements: 0, globally: 0) (open ResultSets: 0, globally: 0)
INFO: 09:24:41,007 DEBUG TableHiLoGenerator:87 - new hi value: 0
INFO: 09:24:41,007 DEBUG AbstractSaveEventListener:135 - generated identifier: 1, using strategy: org.hibernate.id.TableHiLoGenerator
INFO: 09:24:41,036 DEBUG JpaTransactionManager:730 - Initiating transaction commit
INFO: 09:24:41,037 DEBUG JpaTransactionManager:451 - Committing JPA transaction on EntityManager [org.hibernate.ejb.EntityManagerImpl@4e65ad52]
INFO: 09:24:41,037 DEBUG JDBCTransaction:134 - commit
INFO: 09:24:41,038 DEBUG JDBCTransaction:227 - re-enabling autocommit
INFO: 09:24:41,038 DEBUG JDBCTransaction:147 - committed JDBC Connection
INFO: 09:24:41,039 DEBUG ConnectionManager:427 - aggressively releasing JDBC connection
INFO: 09:24:41,039 DEBUG ConnectionManager:464 - releasing JDBC connection [ (open PreparedStatements: 0, globally: 0) (open ResultSets: 0, globally: 0)]
INFO: 09:24:41,047 DEBUG JpaTransactionManager:539 - Not closing pre-bound JPA EntityManager after transaction
INFO: 09:24:41,047 DEBUG ExecutionContext:183 - Transitioning to lifecycle stage ResolutionExecution
INFO: 09:24:41,048 DEBUG HttpCacheInterceptor:183 - Looking for HttpCache on com.jameselsey.salestracker.action.ViewClientAction.save()
INFO: 09:24:41,160 DEBUG ExecutionContext:183 - Transitioning to lifecycle stage RequestComplete
INFO: 09:24:41,161 DEBUG JDBCTransaction:186 - rollback
INFO: 09:24:41,161 DEBUG JDBCTransaction:227 - re-enabling autocommit
INFO: 09:24:41,162 DEBUG JDBCTransaction:197 - rolled back JDBC Connection
INFO: 09:24:41,162 DEBUG ConnectionManager:427 - aggressively releasing JDBC connection
INFO: 09:24:41,162 DEBUG ConnectionManager:464 - releasing JDBC connection [ (open PreparedStatements: 0, globally: 0) (open ResultSets: 0, globally: 0)]
INFO: 09:24:41,163 DEBUG OpenEntityManagerInViewFilter:119 - Closing JPA EntityManager in OpenEntityManagerInViewFilter
INFO: 09:24:41,163 DEBUG EntityManagerFactoryUtils:313 - Closing JPA EntityManager
最佳回答

Freely quoting the Java EE specs, whenever you say em.persist(obj), it doesn t necessarily mean a direct SQL query to the database. It means that the entity named obj will have a persistent identity, thus it s state will be saved and flushed to the persistent store in some time, maybe right after your persist call, maybe after the transaction, it depends on your JPA provider. This could mean that if you call persist to an entity and pause the application with a breakpoint, your persisted data won t be in the database immediately.

If you d like to have a SQL query right after persisting an entity, you could run em.flush().

问题回答

Did you do this, after calling persist()?

Stripersist.getEntityManager().getTransaction().commit();




相关问题
Multiple Hibernate instances using C3P0

I am facing a weird problem and it seems to be c3p0 related. I am starting two instances of an app in the same java vm which interact with each other. After some operations "APPARENT DEADLOCK" ...

Hibernate vs Ibatis caching

We can speed up a hibernate app easyly with 2nd level cache using infinispan or ehcache/terracotta,... but ibatis only have a simple interface to implement for caching. And hibernate knows more ...

Using annotations to implement a static join in hibernate

I m relatively new to hibernate and was wondering if someone could help me out. While I have no issues implementing a normal join on multiple columns in hibernate using the @JoinColumns tag, I m ...

Hibernate query with fetch question

I have a 2 entities in a One-To-Many relationship: OfficeView.java: public class OfficeView implements java.io.Serializable { private Integer officeId; private String addr1; private ...

hibernate interceptors : afterTransactionCompletion

I wrote a Hibernate interceptor : public class MyInterceptor extends EmptyInterceptor { private boolean isCanal=false; public boolean onSave(Object entity, Serializable arg1, Object[] arg2, String[]...

How to prevent JPA from rolling back transaction?

Methods invoked: 1. Struts Action 2. Service class method (annotated by @Transactional) 3. Xfire webservice call Everything including struts (DelegatingActionProxy) and transactions is configured ...

Hibernate/GORM: collection was not processed by flush()

I have an integration test in my Grails application that fails when I try to save an entity of type Member invitingMember.save(flush: true) This raises the following exception org.hibernate....

Hibernate Criteria API equivalent for "elements()"

Is it possible to implement the following query using Criteria API? select order from ORDER as order,ITEM as item where item.itemID like ITM_01 and item in elements(order.items)

热门标签