java - Data not inserted to db -
i trying insert data db using spring,hibernate,jpa .but when try insert data not give errors. data has not been inserted well. ideas ?
my persistence.xml
<?xml version="1.0" encoding="utf-8"?> <persistence version="2.0" 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"> <persistence-unit name="guestbookpu" transaction-type="resource_local"> <provider>org.hibernate.ejb.hibernatepersistence</provider> <class>com.sanja.test.mydao.entity.user</class> <properties> <property name="hibernate.connection.driver_class" value="com.mysql.jdbc.driver"/> <property name="hibernate.connection.password" value="welcome"/> <property name="hibernate.connection.url" value="jdbc:mysql://localhost:3306/sanjaya"/> <property name="hibernate.connection.username" value="root"/> </properties> </persistence-unit> </persistence>
bean declaration in service project(spring_service.xml)
<?xml version="1.0" encoding="utf-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:p="http://www.springframework.org/schema/p" xsi:schemalocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd "> <import resource="classpath:spring_dao.xml"/> <bean id="app" class="com.sanja.test.myservice.app"/> <bean id="entitymanagerfactory" class="org.springframework.orm.jpa.localcontainerentitymanagerfactorybean" p:persistenceunitname="guestbookpu"/> </beans>
bean declaration in dao project(spring_dao.xml)
<?xml version="1.0" encoding="utf-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" xmlns:oxm="http://www.springframework.org/schema/oxm" xmlns:p="http://www.springframework.org/schema/p" xsi:schemalocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd "> <context:component-scan base-package="com.sanja.test.mydao" /> <context:annotation-config /> </beans>
my entity
@entity public class user { @id @generatedvalue(strategy=generationtype.auto) long id; string name; public long getid() { return id; } public void setid(long id) { this.id = id; } public string getname() { return name; } public void setname(string name) { this.name = name; } }
my test class
public class app { @autowired userdao userdao; public static void main( string[] args ) { applicationcontext context = new classpathxmlapplicationcontext("spring_service.xml"); app app=(app)context.getbean("app"); app.adduser(); } @transactional(propagation = propagation.supports) public void adduser(){ user user=new user(); user.setname("sanja ela"); userdao.save(user); system.out.println("elane"); } }
my dao logic
@repository("userdao") public class userdaoimpl implements userdao { @persistencecontext entitymanager entitymanager; public user save(user user){ entitymanager.persist(user); return user; } }
i posted each , every code.
you using propagation.supports
. if transaction not exists, operation non transactional (see javadoc propagation.supports).
support current transaction, execute non-transactionally if none exists.
give try using propagation.required
(javadoc propagation.required);
support current transaction, create new 1 if none exists.
Comments
Post a Comment