gpt4 book ai didi

java - Spring 3 和 JPA 使用 JTATransactionManager,但事务管理器无法提交

转载 作者:行者123 更新时间:2023-12-01 05:10:06 26 4
gpt4 key购买 nike

我正在使用 Spring 并与 hibernate 4 集成作为持久化提供程序,这里我尝试的是进行增删改查操作关于学生实体,

我的所有选择操作都工作正常,但添加和更新操作不起作用,

我的 persistence.xml,

<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
version="2.0">
<persistence-unit name="forSpring" transaction-type="JTA">
<class>com.entity.Student</class>
</persistence-unit>
</persistence>

我的 Spring-bean.xml,

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util-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/jee
http://www.springframework.org/schema/jee/spring-jee-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">

<context:annotation-config />
<context:component-scan base-package="com.*" />

<jee:jndi-lookup id="datasourceId" jndi-name="jdbc/myPrac" resource-ref="true" />
<bean id="txManager" class="org.springframework.transaction.jta.JtaTransactionManager" />

<!-- Spring integration with JPA -->
<bean id="vendorAdaptor-inj" class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<property name="database" value="DERBY" />
<property name="showSql" value="true" />
<property name="generateDdl" value="true" />
</bean>

<bean id="entityMgrFactory-inj" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="datasourceId" />
<property name="persistenceUnitName" value="forSpring" />
<property name="jpaVendorAdapter" ref="vendorAdaptor-inj" />
</bean>

</beans>

这是我的 StudentDAO,

package com.dao;

import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.TypedQuery;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.entity.Student;


@Repository("studentSpringDao-inj")
@Transactional(value="txManager",readOnly=true)
public class StudentSpringDAO {


@PersistenceContext(unitName="forSpring")
private EntityManager em;

public EntityManager getEm() {
return em;
}

public void setEm(EntityManager em) {
this.em = em;
}

public List<Student> getStudentList() throws Exception{
System.out.println(" \n\n\n LOADING... \n\n\n ");
String query = "select s from Student s order by s.studentId desc";
TypedQuery<Student> studentQuery = em.createQuery(query,Student.class);
return studentQuery.getResultList();
}

public Integer latestStudent() throws Exception{
String query = "select max(s.studentId) from Student s";
TypedQuery<Integer> studentQuery = em.createQuery(query,Integer.class);
return studentQuery.getResultList().get(0);
}

@Transactional(value="txManager",readOnly=false,propagation=Propagation.REQUIRES_NEW)
public Student saveStudent(Student student) throws Exception{
return em.merge(student);
}
}

仅供引用,我使用 jndi 查找名称“jdbc/myPrac”来查找数据源,

你能帮我解决这个问题吗?我是否缺少 spring-bean.xml 中的任何配置

最佳答案

@Transactional(value="txManager",readOnly=true)

您认为 readOnly=true 意味着什么?您是否尝试过 readOnly=false

关于java - Spring 3 和 JPA 使用 JTATransactionManager,但事务管理器无法提交,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12140168/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com