EntityManager.merge()未更新数据库

时间:2013-07-01 13:52:27

标签: jpa eclipselink

我正在尝试使用user_info使用新密码更新EntityManager.merge()表,但它没有得到提交。以下是我的代码:

应用-cofig.xml:

<!-- Application Message Bundle -->
    <bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
        <property name="basename" value="/WEB-INF/messages" />
        <property name="cacheSeconds" value="300" />        
    </bean>

    <bean id="loginValidator" class="com.sbi.llms.validator.LoginValidator"/>

    <bean id="loginProcessor" class="com.sbi.llms.processor.LoginProcessor">
        <property name="userDao" ref="userDao"/>
    </bean> 


    <!-- Resolves view names to protected .jsp resources within the /WEB-INF/views directory -->
    <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/jsp/"/>
        <property name="suffix" value=".jsp"/>
    </bean>

    <bean id="forgetProcessor" class="com.test.ForgetPasswordProcessor">
        <property name="forgetDao" ref="forgetDao"/>
    </bean>     

<bean name="/popup_forgot_password.html" class="com.test.ForgetPasswordController">
    <property name="processor" ref="forgetProcessor"/>  
  <property name="commandClass" value="com.test.ForgetPasswordDTO"/>
        <property name="commandName" value="btn_reset"/>
        <property name="formView" value="popup_forgot_password"/>
        <property name="successView" value="popup_forgot_password"/>
                <property name="validator">
            <bean class="com.test.LoginValidator"/>
        </property>

        </bean>

    <bean id="myDataSource"
      class="org.springframework.jdbc.datasource.DriverManagerDataSource">
          <property name="driverClassName" value="oracle.jdbc.OracleDriver"/>
         <property name="url" value="jdbc:oracle:thin:@10.0.27.105:1521/LLMSDB1"/>
         <property name="username" value="llms"/>
         <property name="password" value="llms12"/>
    </bean>
         <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager" p:entityManagerFactory-ref="entityManagerFactory" />
<bean id="entityManager" class="org.springframework.orm.jpa.support.SharedEntityManagerBean" p:entityManagerFactory-ref="entityManagerFactory" />


<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">

    <property name="dataSource" ref="myDataSource" />
    <property name="persistenceUnitName" value="cccPersistenceUnit" />
    <property name="jpaDialect" ref="jpaDialect" />
    <property name="jpaVendorAdapter">
        <bean class="org.springframework.orm.jpa.vendor.EclipseLinkJpaVendorAdapter">
         <property name="databasePlatform" value="org.eclipse.persistence.platform.database.OraclePlatform"/>  
         <property name="showSql" value="true"/>  
        </bean>
    </property>
    <property name="loadTimeWeaver">
        <bean class="org.springframework.instrument.classloading.InstrumentationLoadTimeWeaver" />
    </property>
</bean>


<bean id="baseJPADao" class="com.sbi.llms.dao.jpa.BaseJPADAO">    
<property name="entityManager" ref="entityManager"/>
</bean>



<bean id="userDao" class="com.sbi.llms.dao.UserDAO">
    <property name="entityManager" ref="entityManager"/>
</bean>
<bean id="forgetDao" class="com.test.ForgetPasswordDAO">
    <property name="entityManager" ref="entityManager"/>
</bean>


<bean id="jpaDialect" class="org.springframework.orm.jpa.vendor.EclipseLinkJpaDialect" />

persistance.xml

<?xml version="1.0" encoding="UTF-8" ?>
    <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" xmlns="http://java.sun.com/xml/ns/persistence">
        <persistence-unit name="cccPersistenceUnit" transaction-type="RESOURCE_LOCAL"> <!-- "RESOURCE_LOCAL" -->
        <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
        <mapping-file>META-INF/LoginModel.xml</mapping-file>
        <mapping-file>META-INF/eclipselink-orm.xml</mapping-file>
            <class>com.sbi.llms.model.LoginModel</class>
            <class>com.test.ForgetPasswordModel</class>
            <properties>

                <!-- Configure cache size. -->
                <property name="eclipselink.cache.size.default" value="1000" />

                <!-- Configure simple SQL logging for demonstration. -->
                <property name="eclipselink.logging.level" value="FINE" />
                <property name="eclipselink.logging.thread" value="false" />
                <property name="eclipselink.logging.session" value="false" />
                <property name="eclipselink.logging.exceptions" value="false" />
                <property name="eclipselink.logging.timestamp" value="false" />
            </properties>
        </persistence-unit>
    </persistence>

这是我的DAO

 public class ForgetPasswordDAO {

        private Vector loginResult = null;
        private int resultTrue = 1;
        private int resultFalse = 0;



        protected EntityManager entityManager;
        JpaEntityManager jpaEntityManager;

        public ForgetPasswordDAO()  {
        }

        public EntityManager getEntityManager() {
            return entityManager;
        }

        public void setEntityManager(EntityManager entityManager) {
            this.entityManager = entityManager;
        }


        public Integer fetchUser(ForgetPasswordModel model) throws Exception {
            try {           
                ForgetPasswordDTO DTO ;
                if(model == null) {
                    throw new Exception("102");
                }

                Integer result = 0;
                String userName = model.getUser_id();
                String dob = model.getDob();

                System.out.println("UserDTO " + model.getUser_id() + " "
                        + model.getDob());

                ForgetPasswordModel forgetModel = null;

                            System.out.println(entityManager.isOpen()+">>");

                            forgetModel = entityManager.find(ForgetPasswordModel.class, model.getUser_id());
                            entityManager.close();
                            System.out.println("UserDAO " + forgetModel.getUser_id() + "  DOB  "
                                    + forgetModel.getDob()+"  EMAIL_ID  "+forgetModel.getEmail_id()+"  PASSWORD  "+forgetModel.getPasswd());


                            if(model.getDob().equals(forgetModel.getDob())) {
                                System.out.println("USER VALID , CAN PROCEED WITH PASSWORD RESET");
                                String passwd = GenerateRandomPassword.generateRandomPassword();
                                System.out.println("generated password is" +passwd);
                                entityManager.getTransaction().begin();
                                forgetModel.setPasswd(passwd);
                                entityManager.merge(forgetModel);
                                entityManager.getTransaction().commit();


                                System.out .println("updated password is  "+forgetModel.getPasswd());
                                String email=forgetModel.getEmail_id();
                                ForgetPasswordSendMail.SendMail( email, passwd);
                                result=1;
                            }
                            else
                            {
                                System.out.println("USER InVALID , Please Provide Valid Data"); 
                            }

                            return result;
                        } catch (Exception e) {
                            throw new Exception("103", e);
                        }
                    }

                }

当我运行上面的代码时,我收到以下错误:

java.lang.IllegalStateException: Not allowed to create transaction on shared EntityManager - use Spring transactions or EJB CMT instead
            org.springframework.orm.jpa.SharedEntityManagerCreator$SharedEntityManagerInvocationHandler.invoke(SharedEntityManagerCreator.java:198)
            com.sun.proxy.$Proxy6.getTransaction(Unknown Source)
            com.test.ForgetPasswordDAO.fetchUser(ForgetPasswordDAO.java:81)
            com.test.ForgetPasswordProcessor.execute(ForgetPasswordProcessor.java:52)
            com.test.ForgetPasswordController.onSubmit(ForgetPasswordController.java:56)
            org.springframework.web.servlet.mvc.SimpleFormController.processFormSubmission(SimpleFormController.java:272)
            org.springframework.web.servlet.mvc.AbstractFormController.handleRequestInternal(AbstractFormController.java:268)
            org.springframework.web.servlet.mvc.AbstractController.handleRequest(AbstractController.java:153)
            org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter.handle(SimpleControllerHandlerAdapter.java:48)
            org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:790)


    org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:719)
                org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:644)
                org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:560)
                javax.servlet.http.HttpServlet.service(HttpServlet.java:641)
                javax.servlet.http.HttpServlet.service(HttpServlet.java:722)

1 个答案:

答案 0 :(得分:2)

由于您使用的是Spring,因此需要使用Spring事务,而不是JPA事务。

您需要在Spring中声明事务,或者直接访问EntityManager,而不是通过Spring。