Are*_*iak 5 spring jpa transactional persist applicationcontext
我知道我的问题是一个普遍的问题,但是我在这里检查了很多问题,检查了Spring文档,但我真的不知道我在做什么错。我的问题:我有一个使用JPA的Spring WebFlow项目(实现:OpenJPA + MySQL数据库)。我使用Spring ORM将EntityManager(通过@PersistenceContext注释)注入到我的简单RegisterDAO中。我已经配置了GlassFishs(正在使用的)连接池以使用MySQL,并且一切正常-我可以使用我的数据库,但是当我保留某些东西时-没有任何反应(数据未保留到数据库)。我知道问题出在我使用的事务上下文中。我阅读了Spring Transaction Management的文档,并按照本文档中的配置步骤进行操作。这是我的applicationContext.xml:
<?xml version="1.0" encoding="windows-1250"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:jee="http://www.springframework.org/schema/jee"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.0.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-2.0.xsd">
<jee:jndi-lookup id="entityManagerFactory" jndi-name="myPersistenceUnit"/>
<bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor"/>
<bean id="registerDaoImpl" class="umk.dumont.db.dao.RegisterDAO" />
<bean id="registerModel" class="umk.dumont.models.RegisterFormModel">
<property name="registerDAO" ref="registerDaoImpl" />
</bean>
<tx:advice id="txAdvice">
<tx:attributes>
<tx:method name="*" />
</tx:attributes>
</tx:advice>
<aop:config>
<aop:pointcut id="registerModelOperation" expression="execution(* umk.dumont.models.RegisterFormModel.*(..))"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="registerModelOperation"/>
</aop:config>
<bean id="transactionManager" class="org.springframework.transaction.jta.JtaTransactionManager" />
</beans>
Run Code Online (Sandbox Code Playgroud)
如您所见,我正在将RegisterDAO注入我的RegisterFormModel中,其中包含我的业务逻辑,用于验证注册表单数据并最终将用户添加到数据库中。验证工作正常,当我尝试添加新用户时出现问题。这是代码:
package umk.dumont.models;
...
public class RegisterFormModel implements Serializable {
private String login;
private String password;
private String email;
@Autowired
private RegisterDAO registerDAO = null;
...
public boolean addUser()
{
MyUser user = new MyUser();
user.setLogin(login);
user.setPassword(password);
user.setEmail(email);
return registerDAO.insertUserIntoDB(user) == 0 ? true : false;
}
...
}
Run Code Online (Sandbox Code Playgroud)
注册DAO:
public class RegisterDAO implements RegisterDAOInterface, Serializable {
private EntityManager em;
@PersistenceContext
public void setEm(EntityManager em)
{
this.em = em;
}
...
public int insertUserIntoDB(MyUser user)
{
int result = -4;
try {
em.persist(user);
result = 0;
}
catch(Exception ex)
{
ex.printStackTrace();
result = -4;
}
finally {
return result;
}
}
...
}
Run Code Online (Sandbox Code Playgroud)
我也尝试使用@Transactional批注。我像这样配置spring applicationContext.xml:
<?xml version="1.0" encoding="windows-1250"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:jee="http://www.springframework.org/schema/jee"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.0.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-2.0.xsd">
<jee:jndi-lookup id="entityManagerFactory" jndi-name="myPersistenceUnit"/>
<bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor"/>
<bean id="registerDaoImpl" class="umk.dumont.db.dao.RegisterDAO" />
<bean id="registerModel" class="umk.dumont.models.RegisterFormModel">
<property name="registerDAO" ref="registerDaoImpl" />
</bean>
<tx:annotation-driven />
<bean id="transactionManager" class="org.springframework.transaction.jta.JtaTransactionManager" />
</beans>
Run Code Online (Sandbox Code Playgroud)
并使用@Transactional注释对我的addUser()方法进行注释,如下所示:
package umk.dumont.models;
...
public class RegisterFormModel implements Serializable {
private String login;
private String password;
private String email;
@Autowired
private RegisterDAO registerDAO = null;
...
@Transactional
public boolean addUser()
{
MyUser user = new MyUser();
user.setLogin(login);
user.setPassword(password);
user.setEmail(email);
return registerDAO.insertUserIntoDB(user) == 0 ? true : false;
}
...
}
Run Code Online (Sandbox Code Playgroud)
甚至通过以下注释对整个类进行注释:
package umk.dumont.models;
...
@Transactional
public class RegisterFormModel implements Serializable {
private String login;
private String password;
private String email;
@Autowired
private RegisterDAO registerDAO = null;
...
public boolean addUser()
{
MyUser user = new MyUser();
user.setLogin(login);
user.setPassword(password);
user.setEmail(email);
return registerDAO.insertUserIntoDB(user) == 0 ? true : false;
}
...
}
Run Code Online (Sandbox Code Playgroud)
但在两种情况下,问题都是相同的-数据未存储在数据库中。我的AOP代理是否有任何问题,因为我是新手(就像整个Spring一样:))?
编辑:在我的persistence.xml中,我正在使用transaction-type="JTA“,所以我认为我应该<bean id="transactionManager" class="org.springframework.transaction.jta.JtaTransactionManager" />在我的applicationContext.xml中使用-是吗?
也许这会有所帮助:
在 JNDI 情况下,在此后处理器的“persistenceUnits”映射中指定相应的 JNDI 名称,通常与 Java EE 部署描述符中的匹配 persistence-unit-ref 条目一起。默认情况下,这些名称被视为资源引用(根据 Java EE 资源引用约定),位于“java:comp/env/”命名空间下。例如:
<bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor">
<property name="persistenceUnits">
<map>
<entry key="unit1" value="persistence/unit1"/>
<entry key="unit2" value="persistence/unit2"/>
</map>
</property>
</bean>
Run Code Online (Sandbox Code Playgroud)
从:
| 归档时间: |
|
| 查看次数: |
12255 次 |
| 最近记录: |