数据没有插入到数据库

编程入门 行业动态 更新时间:2024-10-25 08:23:00
本文介绍了数据没有插入到数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我试图使用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.

更多推荐

数据没有插入到数据库

本文发布于:2023-11-26 07:12:41,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1633136.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数据库   数据

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!