2016-10-03 15 views
0

私は春と休止の統合を使用しており、MYSQLをバックエンドとして単純なCRUDアプリケーションを作成しようとしています。データがhibernateTemplateクラスを使用してデータベースに挿入されていません

Here is my Employee.java 


package com.springhibernate; 


public class Employee { 
    private int id; 
    private String name; 
    private float salary; 

    public Employee() { 
     // TODO Auto-generated constructor stub 
    } 

    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.springhibernate; 

import java.util.ArrayList; 
import java.util.List; 

import org.springframework.orm.hibernate4.HibernateTemplate; 

public class EmployeeDao { 
    HibernateTemplate template; 

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

    // method to save employee 
    public void saveEmployee(Employee e) { 
     System.out.println(e.getId()); 
     System.out.println(e.getName()); 
     System.out.println(e.getSalary()); 
     template.save(e); 
     System.out.println("After template.save method"); 
    } 

    // method to update employee 
    public void updateEmployee(Employee e) { 
     template.update(e); 
    } 

    // method to delete employee 
    public void deleteEmployee(Employee e) { 
     template.delete(e); 
    } 

    // method to return one employee of given id 
    public Employee getById(int id) { 
     Employee e = (Employee) template.get(Employee.class, id); 
     return e; 
    } 

    // method to return all employees 
    public List<Employee> getEmployees() { 
     List<Employee> list = new ArrayList<Employee>(); 
     list = template.loadAll(Employee.class); 
     template.setCheckWriteOperations(false); 
     return list; 
    } 

} 

applicationContext.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-3.0.xsd"> 

    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"> 
     <property name="driverClassName" value="com.mysql.jdbc.Driver" /> 
     <property name="url" value="jdbc:mysql://localhost:3306/nupur" /> 
     <property name="username" value="root" /> 
     <property name="password" value="nupur" /> 
    </bean> 

    <bean id="mysessionFactory" 
     class="org.springframework.orm.hibernate4.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.MySQLDialect</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.hibernate4.HibernateTemplate"> 
     <property name="sessionFactory" ref="mysessionFactory"></property> 
     <property name="checkWriteOperations" value="false"></property> 
    </bean> 

    <bean id="d" class="com.springhibernate.EmployeeDao"> 
     <property name="template" ref="template"></property> 
    </bean> 
</beans> 

InsertTest.java 

package com.springhibernate; 

import org.springframework.beans.factory.BeanFactory; 
import org.springframework.beans.factory.xml.XmlBeanFactory; 
import org.springframework.core.io.ClassPathResource; 
import org.springframework.core.io.Resource; 

@SuppressWarnings("deprecation") 
public class InsertTest { 

    public static void main(String[] args) { 

     Resource r = new ClassPathResource("applicationContext.xml"); 
     BeanFactory factory = new XmlBeanFactory(r); 

     EmployeeDao dao = (EmployeeDao) factory.getBean("d"); 

     Employee e = new Employee(); 
     e.setId(164); 
     e.setName("varun"); 
     e.setSalary(50000); 

     System.out.println("Before calling dao method"); 
     dao.saveEmployee(e); 
     System.out.println("After calling dao method"); 
    } 

} 

Employee.hbm.xml 

<?xml version="1.0"?> 
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" 
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> 
<!-- Generated Oct 3, 2016 1:19:10 PM by Hibernate Tools 3.4.0.CR1 --> 
<hibernate-mapping> 
    <class name="com.springhibernate.Employee" table="EMPLOYEE"> 
     <id name="id" type="int"> 
      <column name="ID" /> 
      <generator class="assigned" /> 
     </id> 
     <property name="name" type="java.lang.String"> 
      <column name="NAME" /> 
     </property> 
     <property name="salary" type="float"> 
      <column name="SALARY" /> 
     </property> 
    </class> 
</hibernate-mapping> 

コンソールにエラーは表示されませんが、データはデータベースに挿入されません。どこが間違っているのか教えてください。

私はSOUT文のいくつかは、コード が実行されると、次のように出力がどこにあるかまでチェックするために印刷していた

:template.saveメソッドの後、DAOメソッド164 VARUN

50000.0を呼び出す前に

daoメソッドを呼び出した後

+0

あなたのコンソールに文字列「After template.saveメソッド」が表示されますか? –

+0

まず、BeanFactoryではなく、 'ApplicationContext'を使います。 2番目にトランザクションマネージャを追加し、あなたのdaoに '@ Transactional'を入れ、あなたの設定に' 'を追加してください。トランクアクションがなければ、何も維持されません。 –

答えて

1

以下のようにEmployeeDao.javaクラスに宣言トランザクション管理を追加します。春に

@Transactional 
public void saveEmployee(Employee e) { 
    System.out.println(e.getId()); 
    System.out.println(e.getName()); 
    System.out.println(e.getSalary()); 
    template.save(e); 
    System.out.println("After template.save method"); 
} 

、あなたは開口部、コミット、クローズフラッシングを避けることを可能にする宣言的トランザクション管理を、使用する必要があります。それはすべて自動的にSpringによって行われます。

+0

@Transactionalを追加しましたが、設定ファイルにを追加してもデータに – Nupur

+0

が挿入されていませんか? –

+0

<?xml version = "1.0" encoding = "UTF-8"?> Nupur

0

私は、Eclipseに確認しないでまだ持っていますが、私はあなたのためにいくつかのアイデアを持っている:

  1. モデルクラスを使用すると、DaoImplクラスの@Autowiredを使用する場合は特に、完全にコンストラクタを持つ必要があります。ここではEmployee()とEmployee(ID、名前、給与)です。
  2. DaoImplクラスの@Repository、Controllerの@Controller、トランザクションを使用する場合はDaoImplの@Transactional、サービスクラスの@Serviceを追加します。
    ここで登録しましたので、@ Repoを必要としません。
+0

私はすべてのものを追加しましたが、それも動作していません。私のコードで問題を教えてくださいできますか? – Nupur

+0

すみません。私は今あなたのコードで問題を見つけることができません。しかし、Hibernateのリファレンスとして、あなたはそれを使うことができます:https://github.com/thamluong/test –

関連する問題