I'm using Hibernate 4.3.6 final for my standalone web application on tomcat 8. DB2 is my database
I'm using Hibernate as the JPA-2.1 provider for my web application. All things are fine except for that the entity is not getting saved in the database. Below is my persistence.xml given
Code:
<?xml version="1.0" encoding="UTF-8"?>
<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">
<persistence-unit name="myProjectJPA">
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
<class>com.domain.project.war.jpaentities.Entity1</class>
...
<class>com.domain.project.war.jpaentities.Entity10</class>
<properties>
<property name="javax.persistence.jdbc.driver" value="com.ibm.db2.jcc.DB2Driver" />
<property name="javax.persistence.jdbc.url" value="jdbc:db2://IP_ADDR:50000/DB_NAME:currentSchema=SCHEMA_NAME;" />
<property name="javax.persistence.jdbc.user" value="dbuser" />
<property name="javax.persistence.jdbc.password" value="*******" />
<property name="hibernate.dialect" value="org.hibernate.dialect.DB2Dialect" />
<property name="hibernate.show_sql" value="true" />
<property name="hibernate.format_sql" value="false" />
</properties>
</persistence-unit>
</persistence>
I'm creating a new entity as given by below java DAO code. Entity1 represents my JPA Entity/POJO which just has the fields with its getter/setter and overidden equals and hsahcode methods (....like any other JPA entity)
Code:
public Entity1VO createEntity1(Entity1VO e1VO) {
logger.info("Trying to create entity1 with ID : " + e1VO.getId());
Entity1 e1 = new Entity1();
EntityManagerFactory emf = Persistence.createEntityManagerFactory("myProjectJPA");
EntityManager em = emf.createEntityManager();
EntityTransaction tx = null;
try {
tx = em.getTransaction();
tx.begin();
e1.setEntity1Id(e1VO.getId());
//this is of type CLOB in database
e1.setJsonRepresentation(e1VO.getConfig());
e1.setName(e1VO.getName());
e1.setCreateDate(new Timestamp(new Date().getTime()));
e1.setUpdateDate(null);
e1.setUpdateUser(null);
em.persist(e1);
//em.flush();
tx.commit();
} catch (RuntimeException e) {
// TODO Auto-generated method stub
e.printStackTrace();
//throw e;
if (null != tx) {
tx.rollback();
}
} finally {
em.close();
emf.close();
}
return e1VO;
}
Finally I even enabled my logs in debug mode and printing out SQL. Every thing seems normal. NO exception is thrown but it is not getting saved. Log snippets below
Code:
[2014-09-02 14:10:17,505] [DEBUG] [org.hibernate.jpa.internal.schemagen.JpaSchemaGenerator] No actions specified; doing nothing
[2014-09-02 14:10:17,708] [DEBUG] [org.hibernate.jpa.internal.EntityManagerFactoryRegistry] Initializing EntityManagerFactoryRegistry : org.hibernate.jpa.internal.EntityManagerFactoryRegistry@6799b770
[2014-09-02 14:10:17,708] [DEBUG] [org.hibernate.jpa.internal.EntityManagerFactoryRegistry] Registering EntityManagerFactory: myProjectJPA
[2014-09-02 14:10:17,924] [DEBUG] [org.hibernate.engine.transaction.spi.AbstractTransactionImpl] begin
[2014-09-02 14:10:17,924] [DEBUG] [org.hibernate.engine.jdbc.internal.LogicalConnectionImpl] Obtaining JDBC connection
[2014-09-02 14:10:17,924] [DEBUG] [org.hibernate.engine.jdbc.internal.LogicalConnectionImpl] Obtained JDBC connection
[2014-09-02 14:10:17,924] [DEBUG] [org.hibernate.engine.transaction.internal.jdbc.JdbcTransaction] initial autocommit status: false
[2014-09-02 14:10:17,946] [DEBUG] [org.hibernate.event.internal.AbstractSaveEventListener] Generated identifier: 74e94a88-148363aef40-e+ud9n/q0/V5wQi+J8wYhA==, using strategy: org.hibernate.id.Assigned
[2014-09-02 14:10:17,993] [DEBUG] [org.hibernate.event.internal.AbstractFlushingEventListener] Processing flush-time cascades
[2014-09-02 14:10:17,997] [DEBUG] [org.hibernate.event.internal.AbstractFlushingEventListener] Dirty checking collections
[2014-09-02 14:10:18,005] [DEBUG] [org.hibernate.event.internal.AbstractFlushingEventListener] Flushed: 1 insertions, 0 updates, 0 deletions to 1 objects
[2014-09-02 14:10:18,005] [DEBUG] [org.hibernate.event.internal.AbstractFlushingEventListener] Flushed: 0 (re)creations, 0 updates, 0 removals to 0 collections
[2014-09-02 14:10:18,008] [DEBUG] [org.hibernate.internal.util.EntityPrinter] Listing entities:
[2014-09-02 14:10:18,008] [DEBUG] [org.hibernate.internal.util.EntityPrinter] com.domain.project.war.jpaentities.Entity1{newDate=null, jsonRepresentation={ json : { MYAPPDATA }}, updUser=null, name=abcd002, updDate=null, entity1Id=74e94a88-148363aef40-e+ud9n/q0/V5wQi+J8wYhA==, newUser=null}
[2014-09-02 14:10:18,055] [DEBUG] [org.hibernate.SQL] insert into ENTITY_1_TABLE (JSON_REPRESENTATION, NAME, CREATE_DATE, CREATE_USER, UPD_DATE, UPD_USER, ENTITY_1_ID) values (?, ?, ?, ?, ?, ?, ?)
[2014-09-02 14:10:18,064] [DEBUG] [org.hibernate.internal.SessionFactoryImpl] HHH000031: Closing
[2014-09-02 14:10:18,064] [DEBUG] [org.hibernate.service.internal.AbstractServiceRegistryImpl] Implicitly destroying ServiceRegistry on de-registration of all child ServiceRegistries
[2014-09-02 14:10:18,065] [INFO ] [org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl] HHH000030: Cleaning up connection pool [jdbc:db2://IP_ADDR:50000/DB_NAME:currentSchema=SCHEMA_NAME;]
[2014-09-02 14:10:18,066] [DEBUG] [org.hibernate.boot.registry.internal.BootstrapServiceRegistryImpl] Implicitly destroying Boot-strap registry on de-registration of all child ServiceRegistries
[2014-09-02 14:10:18,066] [DEBUG] [org.hibernate.jpa.internal.EntityManagerFactoryRegistry] Remove: name=myProjectJPA
I assume that log entries after insert which indicates closing is w.r.t em.close()
So I'm not able to figure out why my data is NOT getting saved in DB? Have I done any thing wrong in config?
Thanks