Back Forum Reply New

EntityManager can Read But cant Persist objects in DB

greeting all.
im new to Using JPA with spring.
i have a problem when i want to persisting an object.
its my importantn snippet cod of my files:

web.xmlCode:   lt;listenergt;       lt;listener-classgt;org..web.context.ContextLoaderListenerlt;/listener-classgt;   lt;/listenergt;   lt;context-paramgt;       lt;param-namegt;contextConfigLocationlt;/param-namegt;       lt;param-valuegt;/WEB-INF/appplicationContext.xmllt;/param-valuegt;   lt;/context-paramgt;   lt;servletgt;       lt;servlet-namegt;springDispatcherlt;/servlet-namegt;       lt;servlet-classgt;org..web.servlet.DispatcherServletlt;/servlet-classgt;       lt;load-on-startupgt;1lt;/load-on-startupgt;   lt;/servletgt;   lt;servlet-mappinggt;       lt;servlet-namegt;springDispatcherlt;/servlet-namegt;       lt;ucl-patterngt;*.htmllt;/ucl-patterngt;   lt;/servlet-mappinggt;
applicationContext.xmlCode:

lt;?xml version=quot;1.0quot; encoding=quot;UTF-8quot;?gt;
lt;beans xmlns=quot;schema/beansquot;      xmlns:xsi=quot;2001/XMLSchema-instancequot;      xmlns:context=quot;schema/contextquot;      xsi:schemaLocation=quot;schema/beans      schema/beans/spring-beans.xsd      schema/context      schema/context/spring-context.xsdquot;gt;   lt;context:annotation-config/gt;
   lt;bean id=quot;userAuthenticationquot; class=quot;com.tosan.statisticapplication.service.user.UserAuthenticationquot;/gt;
   lt;bean id=quot;registrationquot; class=quot;com.tosan.statisticapplication.controller.UserRegistrationquot;gt;       lt;property name=quot;userDaoquot; ref=quot;userDaoquot;/gt;   lt;/beangt;
   lt;bean id=quot;userDaoquot; class=quot;com.tosan.statisticapplication.service.user.UserDaoquot;/gt;
   lt;bean id=quot;entityManagerFactoryquot; class=quot;org..orm.jpa.LocalContainerEntityManagerFactoryBeanquot;gt;       lt;property name=quot;persistenceXmlLocationquot; value=quot;classpath:persistence.xmlquot;/gt;   lt;/beangt;   lt;bean id=quot;transactionManagerquot; class=quot;org..orm.jpa.JpaTransactionManagerquot;gt;       lt;property name=quot;entityManagerFactoryquot; ref=quot;entityManagerFactoryquot;/gt;   lt;/beangt;

lt;/beansgt;
persistence.xmlCode:

lt;?xml version=quot;1.0quot; encoding=quot;UTF-8quot;?gt;
lt;persistence xmlns=quot;xml/ns/persistencequot; version=quot;1.0quot;gt;
   lt;persistence-unit name=quot;statisticquot; transaction-type=quot;RESOURCE_LOCALquot;gt;       lt;providergt;org.hibernate.ejb.HibernatePersistencelt;/providergt;       lt;classgt;com.tosan.statisticapplication.beans.users.UserInformationlt;/classgt;       lt;classgt;com.tosan.statisticapplication.beans.users.Testlt;/classgt;       lt;propertiesgt;lt;property name=quot;hibernate.dialectquot; value=quot;org.hibernate.dialect.MySQL5Dialectquot;/gt;lt;property name=quot;hibernate.show_sqlquot; value=quot;truequot;/gt;lt;property name=quot;hibernate.connection.uclquot; value=quot;jdbc:mysql--localhost:3306/statisticquot;/gt;lt;property name=quot;hibernate.connection.passwordquot; value=quot;123456quot;/gt;lt;property name=quot;hibernate.connection.driver_classquot; value=quot;com.mysql.jdbc.Driverquot;/gt;lt;property name=quot;hibernate.connection.usernamequot; value=quot;user1quot;/gt;       lt;/propertiesgt;   lt;/persistence-unitgt;

lt;/persistencegt;

UserDaoCode:

import com.tosan.statisticapplication.beans.users.Test;
import com.tosan.statisticapplication.beans.users.UserInformation;
import org..transaction.annotation.Transactional;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

public class UserDao {
   @PersistenceContext()   private EntityManager entityManager;
   @Transactional   public void registerUser(UserInformation userInformation) {       Test test = new Test();       test.setName(quot;user12quot;);       entityManager.persist(test);   }

}
my beans created correctly with spring and when i put break point at registerUser() method and checking i can
read from database but i cant save in it.
i know that my problem is transaction and read many post and forums that lead me to use lt;tx:annotation-driven/gt; in
my applicationContext.xml file.but when i use it my beans not created with spring and it makes Exception.
please help.

Well as you stated you need transactions, you don't have them hence no insert, update and delete. Put tx:annotation-driven in there and fix the error. I suggest a read of the reference guide, especially the transaction chapter.
¥
Back Forum Reply New