我在让我的 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>
持久性.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();
}
}
}
主.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 数据库表是自动创建的,只是没有数据。