我正在尝试使用Spring、Hibernate、JPA将数据插入我的数据库。但是当我尝试插入数据时,它不会给出任何错误。但是数据也没有被插入。有什么想法吗?
我的持久. 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声明(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声明(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>
我的实体
@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;
}
}
我的测试类
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");
}
}
我的道逻辑
@Repository("userDao")
public class UserDaoImpl implements UserDao {
@PersistenceContext
EntityManager entityManager;
public User save(User user){
entityManager.persist(user);
return user;
}
}
我发布了每一个代码。
您正在使用传播. SUPPORTS
。如果事务不存在,则操作将是非事务性的(参见javadoc的传播.SUPPORTS)。
支持当前事务,如果不存在,则以非事务方式执行。
尝试使用传播. REQUIRED
(javadoc for传播.REQUIRED);
支持当前事务,如果不存在,则创建一个新事务。
会是这条线吗?@Transactional(传播=传播.支持)
我们有时会遇到问题。根据DB配置,有时它不会提交。它在文档中说
支持当前事务,不存在则非事务执行
因此,您可能需要将其包装在另一个事务调用中,或者将其更改为另一种类型,例如REQUIRED。