2017-10-23 17 views
0

私はSpringとHibernateを統合しようとしています。この機能を使用してデータをデータベースに保存したいと思います。私のファイルは次のように添付されています。Spring-Hibernate統合フレームワーク

Employee.java

package springhibernate; 

public class Employee { 

    private int id; 
    private String name; 
    private float salary; 

    public Employee() { 
     super(); 
    } 
    public Employee(int id, String name, float salary) { 
     super(); 
     this.id = id; 
     this.name = name; 
     this.salary = 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; 
    } 


} 

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.javatpoint.Employee" table="emp558"> 
      <id name="id"> 
      <generator class="assigned"></generator> 
      </id> 

      <property name="name"></property> 
      <property name="salary"></property> 
</class> 
</hibernate-mapping> 

EmployeeDao.java

package springhibernate; 

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

import org.springframework.orm.hibernate3.HibernateTemplate; 

public class EmployeeDao { 

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

    //method to save employees 
    public void saveEmployee(Employee e) 
    { 
     template.save(e); 
    } 

    //method to update employees 

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

    //method to delete employees 

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

    //method to return 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); 
     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="oracle.jdbc.driver.OracleDriver"/> 
     <property name="url" value="jdbc:oracle:thin:@localhost:1521/XE"></property> 
     <property name="username" value="system"></property> 
     <property name="password" value="manager"></property> 
    </bean> 

    <bean id="mysessionFactory" class="org.springframework.orm.hibernate3.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.Oracle9Dialect</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.hibernate3.HibernateTemplate"> 
    <property name="sessionFactory" ref="mysessionFactory"></property> 
    </bean> 

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


    </beans> 

InsertTest.java

package 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) { 
     // TODO Auto-generated method stub 
     Resource r = new ClassPathResource("applicationContext.xml"); 
     BeanFactory factory = new XmlBeanFactory(r); 
     EmployeeDao dao = (EmployeeDao)factory.getBean("d"); 

     Employee e = new Employee(); 
     e.setId(114); 
     e.setName("varun"); 
     e.setSalary(5000); 

     dao.saveEmployee(e); 
    } 

} 

次のように私は例外を取得しています上記のコードを実行中。私は必要なものがあればすべてのjarファイルをインクルード/追加しました。私はまた、stackoverflowに関するいくつかの関連する質問を見たが、適切なヒントを得ていない。

エラー

Oct 23, 2017 3:12:45 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions 
INFO: Loading XML bean definitions from class path resource [applicationContext.xml] 
Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'd' defined in class path resource [applicationContext.xml]: Cannot resolve reference to bean 'template' while setting bean property 'template'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'template' defined in class path resource [applicationContext.xml]: Instantiation of bean failed; nested exception is java.lang.NoClassDefFoundError: org/hibernate/JDBCException 
    at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:359) 
    at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveValueIfNecessary(BeanDefinitionValueResolver.java:108) 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1469) 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1214) 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:537) 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:476) 
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:303) 
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) 
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:299) 
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194) 
    at springhibernate.InsertTest.main(InsertTest.java:15) 
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'template' defined in class path resource [applicationContext.xml]: Instantiation of bean failed; nested exception is java.lang.NoClassDefFoundError: org/hibernate/JDBCException 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:1093) 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1038) 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:504) 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:476) 
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:303) 
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) 
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:299) 
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194) 
    at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:351) 
    ... 10 more 
Caused by: java.lang.NoClassDefFoundError: org/hibernate/JDBCException 
    at java.lang.Class.getDeclaredConstructors0(Native Method) 
    at java.lang.Class.privateGetDeclaredConstructors(Unknown Source) 
    at java.lang.Class.getConstructor0(Unknown Source) 
    at java.lang.Class.getDeclaredConstructor(Unknown Source) 
    at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:80) 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:1086) 
    ... 18 more 
Caused by: java.lang.ClassNotFoundException: org.hibernate.JDBCException 
    at java.net.URLClassLoader$1.run(Unknown Source) 
    at java.net.URLClassLoader$1.run(Unknown Source) 
    at java.security.AccessController.doPrivileged(Native Method) 
    at java.net.URLClassLoader.findClass(Unknown Source) 
    at java.lang.ClassLoader.loadClass(Unknown Source) 
    at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source) 
    at java.lang.ClassLoader.loadClass(Unknown Source) 
    ... 24 more** 

は、誰もがこのエラー/例外から抜け出すに私を助けることができます。

ありがとうございます。

+0

'org.hibernate.JDBCException'クラスを使用すると、使用しているかに応じてバージョンに' hibernate.jar'または '休止状態-core.jar'に存在すべきであり、 。あなたの質問を更新し、あなたの依存関係をバージョンと共に投稿してください。また、それらの依存関係がターゲットビルド(おそらく戦争)に含まれていることを確認してください。また、競合に依存しないことを確認してください。同じバージョンのjarファイル – pleft

答えて

0

このソリューションがちょっとした感覚で動作するかどうかはわかりません。 は交換してください:

Resource r = new ClassPathResource("applicationContext.xml"); 
BeanFactory factory = new XmlBeanFactory(r); 
EmployeeDao dao = (EmployeeDao)factory.getBean("d"); 

で:

ApplicationContext r = new ClassPathXMLContext("applicationContext.xml); 
Employee dao = r.getBean("d"); 
関連する問題