我有一个控制器,用于在 Maven 3 下使用 Spring mvc 开发的 webapp。在这个控制器中,@Autowired
PersonDAO dbController
我想使用 JPA 2.0 来管理持久性,所以我重写了我的 applicationContext.xml:我添加了
`<tx:annotation-driven transaction-manager="transactionManager" />
<bean
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"
id="entityManagerFactory">
<property name="dataSource" ref="dataSource" />
<!-- <property name="packagesToScan" value="net.tirasa.test.addressbook.dao" />-->
<property name="persistenceUnitName" value="persistenceUnit" />
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<property name="showSql" value="true" />
<!--<property name="generateDdl" value="true" />-->
<!--<property name="databasePlatform" value="org.hibernate.dialect.H2Dialect" />-->
<!--<property name="database" value="H2" />-->
</bean>
</property>
</bean>
<bean class="org.springframework.orm.jpa.JpaTransactionManager"
id="transactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="org.h2.Driver"/>
<property name="url" value="jdbc:h2:mem:test;DB_CLOSE_DELAY=-1"/>
<property name="username" value="sa"/>
<property name="password" value=""/>
</bean>`
我的 PersonDAOImpl 是:
@Repository
public class PersonDAOJpaImpl implements PersonDAO {
//@Value("#{entityManager}")
@PersistenceContext(type = PersistenceContextType.TRANSACTION)
//@Autowired
protected EntityManager entityManager;
private final static Logger LOG = LoggerFactory.getLogger(PersonDAOJpaImpl.class);
@Transactional
public void save(String id, String name, String email, String telephone) throws DatabaseException {
Person person = null;
if (find(id) == null) {
entityManager.persist(person);
} else {
entityManager.merge(person);
}
}
@Transactional
public Person find(String requestParam_id) throws DatabaseException {
return entityManager.find(Person.class, requestParam_id);
}
@Transactional
public List<Person> list() throws DatabaseException {
Query query = entityManager.createQuery("select p from Persons p");
List<Person> resultList = query.getResultList();
Person p = null;
entityManager.find(Person.class, "Andrea Patricelli");
System.out.println("ECCO UN ELEMENTO DELLA LISTA: ------->" + resultList.iterator().next().getName());
System.out.println("ECCO IL RISULTATO DELLA FIND: -------->" + p.getName());
return resultList;
}
但没有任何效果;我做错了什么?特别是我不知道如何将 My PersonDAO dbController 与 PersonDAOImpl 和 entityManager 连接起来