-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: TransactionRequiredException Executing an update/delete quer
PostPosted: Sat Sep 13, 2014 5:09 am 
Newbie

Joined: Sat Aug 23, 2014 12:59 am
Posts: 8
Hi I am using hibernate JPA with spring and mongodb and i am running my application on Glassfish-4.0.

My service class is

Code:
@Component
public class Test  {

        @PersistenceContext
             EntityManager em;
             EntityManagerFactory emf;

         @Transactional
             public String persist(Details details){
                 details.getUsername();
                 details.getPassword();
                 Query query = em.createNativeQuery("db.details.find(username= "+details.getUsername()+"& password= "+details.getPassword());
                 em.getTransaction().begin();
                 em.persist(details);
                 em.getTransaction().commit();
                 em.flush();
                 em.clear();
                 em.close();
                 query.executeUpdate();
                 System.out.println("Sucessful!");
                return "persist";       
         }
    }


And my spring-context.xml

Code:
<?xml version="1.0" encoding="UTF-8"?>
<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:mvc="http://www.springframework.org/schema/mvc"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-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/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">

    <context:component-scan base-package="com.javapapers.spring.mvc" />
    <context:annotation-config/>
    <mvc:annotation-driven />
    <tx:annotation-driven transaction-manager="txManager" />
    <bean
            class="org.springframework.web.servlet.view.InternalResourceViewResolver">
            <property name="prefix" value="/WEB-INF/view/" />
            <property name="suffix" value=".jsp" />
    </bean>
    <bean id="entityManagerFactory"
            class="org.springframework.orm.jpa.LocalEntityManagerFactoryBean">
            <property name="persistenceUnitName" value="ogmTest"/>
     </bean>
    <bean id="txManager" class="org.springframework.transaction.jta.JtaTransactionManager">
    </bean>
</beans>


I have applied some changes in the code but their is no effect. Can anyone please help me to sort out this. Thanks in advance.


Top
 Profile  
 
 Post subject: Re: TransactionRequiredException Executing an update/delete quer
PostPosted: Sat Sep 13, 2014 5:47 am 
Newbie

Joined: Sat Aug 23, 2014 12:59 am
Posts: 8
mongodb wrote:
Hi I am using hibernate JPA with spring and mongodb and i am running my application on Glassfish-4.0.

My service class is

Code:
@Component
public class Test  {

        @PersistenceContext
             EntityManager em;
             EntityManagerFactory emf;

         @Transactional
             public String persist(Details details){
                 details.getUsername();
                 details.getPassword();
                 Query query = em.createNativeQuery("db.details.find(username= "+details.getUsername()+"& password= "+details.getPassword());
                 em.getTransaction().begin();
                 em.persist(details);
                 em.getTransaction().commit();
                 em.flush();
                 em.clear();
                 em.close();
                 query.executeUpdate();
                 System.out.println("Sucessful!");
                return "persist";       
         }
    }


And my spring-context.xml

Code:
<?xml version="1.0" encoding="UTF-8"?>
<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:mvc="http://www.springframework.org/schema/mvc"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-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/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">

    <context:component-scan base-package="com.javapapers.spring.mvc" />
    <context:annotation-config/>
    <mvc:annotation-driven />
    <tx:annotation-driven transaction-manager="txManager" />
    <bean
            class="org.springframework.web.servlet.view.InternalResourceViewResolver">
            <property name="prefix" value="/WEB-INF/view/" />
            <property name="suffix" value=".jsp" />
    </bean>
    <bean id="entityManagerFactory"
            class="org.springframework.orm.jpa.LocalEntityManagerFactoryBean">
            <property name="persistenceUnitName" value="ogmTest"/>
     </bean>
    <bean id="txManager" class="org.springframework.transaction.jta.JtaTransactionManager">
    </bean>
</beans>


I have applied some changes in the code but their is no effect. Can anyone please help me to sort out this. Thanks in advance.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.