Spring EntityManager不会持久存在于独立应用程序中

时间:2012-09-17 14:01:23

标签: spring hibernate entitymanager

我无法让我的EntityManager在我的独立应用程序中保留我的@Entity对象。没有错误,但HSQL数据库中没有任何内容。 我敢肯定它一定是愚蠢的东西,但我现在正以我的智慧结束。我搜索并搜索并尝试了一切,但我仍然无法让它工作。

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

没有任何错误,它似乎保存了EntityObj,但数据库中没有任何内容。我觉得这是与事务相关的,可能是由于我如何实例化对象,但我不确定。 ENTITYOBJ数据库表是自动创建的,只是没有数据。

0 个答案:

没有答案