未插入数据的数据

时间:2013-07-25 11:13:57

标签: java spring hibernate jpa

我正在尝试使用Spring,Hibernate,JPA将数据插入到我的数据库。但是当我尝试插入数据时,它不会给出任何错误。但是还没有插入数据。有什么想法吗?

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声明(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>
dao项目中的

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;

    }


}

我发布了每个代码。

2 个答案:

答案 0 :(得分:2)

您正在使用Propagation.SUPPORTS。如果事务不存在,则操作将是非事务性的(请参阅javadoc for Propagation.SUPPORTS)。

  

支持当前事务,如果不存在则以非事务方式执行。

尝试使用Propagation.REQUIREDjavadoc for Propagation.REQUIRED);

  

支持当前事务,如果不存在则创建一个新事务。

答案 1 :(得分:0)

可能是这条线吗? @Transactional(propagation = Propagation.SUPPORTS)

我们有时会遇到问题。根据数据库配置,有时它不会执行提交。 它在the doc

中说
  

支持当前事务,如果没有,则以非事务性方式执行   存在

所以也许你需要将它包装在另一个事务性调用中,或者将其更改为另一种类型,例如REQUIRED。