我试图使用Spring,Hibernate,JPA插入数据到我的数据库。但是当我尝试插入数据时,它不会给出任何错误。但数据还没有插入。任何想法?
I am trying to insert data to my db using Spring,Hibernate,JPA .But when I try to insert data it does not give any errors. But data has not been inserted as well. Any ideas ?
我的persistence.xml
MY persistence.xml
<?xml version="1.0" encoding="UTF-8"?> <persistence version="2.0" xmlns="java.sun/xml/ns/persistence" xmlns:xsi="www.w3/2001/XMLSchema-instance" xsi:schemaLocation="java.sun/xml/ns/persistence java.sun/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声明(spring_service.xml)
bean declaration in service project(spring_service.xml)
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="www.springframework/schema/beans" xmlns:context="www.springframework/schema/context" xmlns:mvc="www.springframework/schema/mvc" xmlns:xsi="www.w3/2001/XMLSchema-instance" xmlns:tx="www.springframework/schema/tx" xmlns:p="www.springframework/schema/p" xsi:schemaLocation=" www.springframework/schema/beans www.springframework/schema/beans/spring-beans-3.2.xsd www.springframework/schema/context www.springframework/schema/context/spring-context-3.2.xsd www.springframework/schema/tx www.springframework/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>dao项目中的bean声明(spring_dao.xml)
bean declaration in dao project(spring_dao.xml)
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="www.springframework/schema/beans" xmlns:context="www.springframework/schema/context" xmlns:mvc="www.springframework/schema/mvc" xmlns:xsi="www.w3/2001/XMLSchema-instance" xmlns:oxm="www.springframework/schema/oxm" xmlns:p="www.springframework/schema/p" xsi:schemaLocation=" www.springframework/schema/beans www.springframework/schema/beans/spring-beans-3.2.xsd www.springframework/schema/context www.springframework/schema/context/spring-context-3.2.xsd "> <context:component-scan base-package="com.sanja.test.myDao" /> <context:annotation-config /> </beans>我的实体
@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
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"); } }我的Dao逻辑
@Repository("userDao") public class UserDaoImpl implements UserDao { @PersistenceContext EntityManager entityManager; public User save(User user){ entityManager.persist(user); return user; } }我发布了每一个代码。
I posted each and every code.
推荐答案您正在使用 Propagation.SUPPORTS 。如果事务不存在,操作将不是事务性的(参见 javadoc for Propagation.SUPPORTS )。 $ b
You are using Propagation.SUPPORTS. If the transaction do not exists, the operation will be non transactional (see javadoc for Propagation.SUPPORTS).
支持当前事务,执行非 -
Support a current transaction, execute non-transactionally if none exists.
请尝试使用 Propagation.REQUIRED ( javadoc for Propagation。要求);
Give it a try using Propagation.REQUIRED(javadoc for Propagation.REQUIRED);
支持当前交易,如果不存在,请创建一个新交易。
Support a current transaction, create a new one if none exists.
更多推荐
数据没有插入到数据库
发布评论