1

我正在使用 Spring 并与 hibernate 4 集成作为持久性提供程序,这里我正在尝试对学生实体进行 crud 操作,

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

我的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 中的任何配置

4

2 回答 2

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

你认为是什么readOnly=true意思,你试过了readOnly=false吗?

于 2012-08-27T10:38:05.387 回答
0

替换类org.springframework.transaction.jta.JtaTransactionManagerorg.springframework.orm.jpa.JpaTransactionManager添加tx:annotation-driven

于 2013-10-30T02:59:20.193 回答