我試圖將數據插入到使用Spring,Hibernate的,JPA。但是當我嘗試將數據插入它不給任何錯誤,我的分貝。但數據還沒有插入。有任何想法嗎 ?數據未插入到數據庫
我的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聲明在服務項目(spring_service.xml)在道項目(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>
<?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聲明我實體
@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;
}
}
我張貼的每一個代碼。
在哪一類,你寫的數據庫插入查詢? –
@IrenPatel我更新的代碼now.At底部在我的崗位 –
@SanjayaLiyanage在您的測試類添加'@TransactionConfiguration(defaultRollback = FALSE)',看看你是否能夠堅持記錄數據庫。 – user75ponic