2

I'm having problems getting my EntityManager to persist my @Entity object in my standalone application. There are no errors, but nothing is persisted into the HSQL database. I'm sure it must be something silly but i'm at my wits end with it now. I've searched and searched and tried everything but i still can't get it to work.

appconfig.xml:

<bean id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalEntityManagerFactoryBean" />
<bean id="productDaoImpl" class="test.ProductDao"/>
<bean
class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" />
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>

<tx:annotation-driven transaction-manager="transactionManager"/>
<context:component-scan base-package="test">
</context:component-scan>

persistence.xml:

<persistence-unit name="test" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>  
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.HSQLDialect"/>
<property name="hibernate.hbm2ddl.auto" value="update"/>
<property name="hibernate.connection.driver_class" value="org.hsqldb.jdbcDriver"/>
<property name="hibernate.connection.username" value=""/>
<property name="hibernate.connection.password" value=""/>
<property name="hibernate.connection.url" value="jdbc:hsqldb:file://c:/db2"/>
</properties>
</persistence-unit>
</persistence>

EntityObj.java:

package test;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
@Entity
public class EntityObj {
@Id
@GeneratedValue
private int id;
private String value;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
}

ProductDao.java:

package test;
import org.springframework.dao.DataAccessException;
public interface ProductDao {
public void save(EntityObj obj) throws DataAccessException;
}

ProductDaoImpl.java:

package test;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;
@Repository
@Transactional
public class ProductDaoImpl implements ProductDao {
@PersistenceContext
private EntityManager entityManager;
public EntityManager getEntityManager() {
return entityManager;
}
public void setEntityManager(EntityManager entityManager) {
this.entityManager = entityManager;
}
@Transactional (readOnly = false)
public void save(EntityObj obj)
{
try
{
entityManager.persist(obj);
entityManager.flush();
}
catch(Exception e)
{
e.printStackTrace();
}
}    
}

Main.java:

package test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
public class Main {
public static void main(String[] args)
{
ApplicationContext springContext = new FileSystemXmlApplicationContext("classpath*:appconfig.xml");
ProductDao dao = (ProductDao) springContext.getBean("productDaoImpl");
EntityObj obj = new EntityObj();
obj.setValue("Blah");
dao.save(obj);
}
}

There are no errors whatsoever, it appears like it has saved the EntityObj but nothing is persisted in the database. I get the feeling it is transaction related and probably due to how i am instantiating the objects but i'm not sure. The ENTITYOBJ database table is created automatically, just no data.

ThinkingStiff
  • 64,767
  • 30
  • 146
  • 239
Sam
  • 21
  • 1
  • possible duplicate of [EntityManager does not write to database](http://stackoverflow.com/questions/8105618/entitymanager-does-not-write-to-database) – axtavt Sep 17 '12 at 14:08
  • thanks, but no, i am not calling createEntityManager. That is being injected. Also, the transaction _should_ be created via the @Transactional annotation rather than having to explicitly create the transaction. – Sam Sep 17 '12 at 14:15
  • It doesn't matter. It's about graceful shutdown of HSQLDB, not about JPA and transactions. – axtavt Sep 17 '12 at 14:18
  • apologies, just seen that bit and you are right. Adding the Thread.sleep(1000) to the end of my main method fixes it. Is that the correct thing to do or is there a better way? Thanks – Sam Sep 17 '12 at 14:21
  • See this post : http://stackoverflow.com/questions/12635205/spring-jpa-hibernate-in-standalone-app/12934470#12934470 – Prasobh.Kollattu Oct 17 '12 at 13:39
  • Hsqldb has a log file in root dir, it has all the sql script that excuted, can you check whether the insert is operated? – OQJF Mar 12 '13 at 07:59

0 Answers0