我的代码执行成功但数据没有存储在spring和hibernate的数据库中

时间:2016-12-25 11:53:56

标签: spring hibernate

当我要执行这个程序时。 我的程序已成功执行但它不会将数据存储在数据库中。 这个程序有什么问题。

这是我的Employee.java类

package com.spring.demo;    
public class Employee {

private int id;
private String name;
private float salary;
public int getId() {
    return id;
}
public void setId(int id) {
    this.id = id;
}
public String getName() {
    return name;
}
public void setName(String name) {
    this.name = name;
}
public float getSalary() {
    return salary;
}
public void setSalary(float salary) {
    this.salary = salary;           
}
}

这是我的EmployeeDao.java类

package com.spring.demo;
import org.springframework.orm.hibernate5.HibernateTemplate;

public class EmployeeDao {
HibernateTemplate template;

public void setTemplate(HibernateTemplate template) {
    this.template = template;
}

public void saveEmployee(Employee e) {
    Integer i = (Integer) template.save(e);
    if (i > 0) {
        System.out.println("Success");
    } else {
        System.out.println("Not Success");
    }
}

public void updateEmployee(Employee e) {
    template.update(e);
}

public void deleteEmployee(Employee e) {
    template.delete(e);
}
}

这是我的Test.java类

package com.spring.demo;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Test {
public static void main(String[] args) {

    ClassPathXmlApplicationContext bean = new                                       
 ClassPathXmlApplicationContext("spring.xml");

    EmployeeDao emp = (EmployeeDao) bean.getBean("obj");

    Employee e = new Employee();
    e.setId(2);
    e.setName("Amit Goyal");
    e.setSalary(40000);

    emp.saveEmployee(e);
    // emp.updateEmployee(e);

    bean.close();
}
}

这是我的Employee.hbm.xml文件

<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-mapping PUBLIC
      "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
      "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping>
<class name="com.spring.demo.Employee" table="amit1234">
    <id name="id">
        <generator class="assigned"></generator>
    </id>
    <property name="name"></property>
    <property name="salary"></property>
</class>

</hibernate-mapping>

最后这是我的spring.xml文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans  
           http://www.springframework.org/schema/beans/spring-beans- 
 4.2.xsd">


<bean id="dataSource"
    class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName" 
value="oracle.jdbc.driver.OracleDriver"/>
    <property name="url" value="jdbc:oracle:thin:@localhost:1521:xe" />
    <property name="username" value="system" />
    <property name="password" value="tiger" />
</bean>

<bean id="mysessionFactory"
    class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
    <property name="dataSource" ref="dataSource"></property>

    <property name="mappingResources">
        <list>
            <value>Employee.hbm.xml</value>
        </list>
    </property>

    <property name="hibernateProperties">
        <props>
            <prop key="hibernate.dialect">
org.hibernate.dialect.Oracle10gDialect
</prop>
            <prop key="hibernate.hbm2ddl.auto">update</prop>
            <prop key="hibernate.show_sql">true</prop>
        </props>
    </property>
</bean>

<bean id="template" 
class="org.springframework.orm.hibernate5.HibernateTemplate">
    <property name="sessionFactory" ref="mysessionFactory"></property>
    <property name="checkWriteOperations" value="false"></property>
</bean>

<bean id="obj" class="com.spring.demo.EmployeeDao">
    <property name="template" ref="template"></property>
</bean>
</beans>

1 个答案:

答案 0 :(得分:0)

在此配置中,您缺少事务管理配置。对服务方法使用@transactional注释。 在您的情况下,数据不会在数据库中提交,它只是保存数据而不是在数据库中提交数据。