我试图了解Spring-hibernate环境中HibernateTemplate的工作原理。由于我是HibernateTemplate的新手,我觉得很难理解错误。在我的项目中,我使用了 spring-framework-4.0.3 和 hibernate-4.3.5 中提供的所有jar文件。该项目中使用的一些额外的罐子是:
*cglib-nodep-2.1.3.jar
*commons-logging.jar
*ojdbc14.jar
*tomcat-dbcp-7.0.30.jar
使用了以下java / xml文件:
StudentDaoInterface.java
package dao;
import java.util.List;
import model.Student;
public interface StudentDaoInterface {
public int save(Student st);
public boolean update(Student st);
public boolean delete(Student st);
public Student findbyPK(int pk);
public List<Student> findAllUsingHQL();
public List<Student> findAllUsingCriteria();
}
StudentDaoImplHT.java
package dao;
import java.util.List;
import org.hibernate.criterion.DetachedCriteria;
import org.springframework.orm.hibernate4.HibernateTemplate;
import model.Student;
public class StudentDaoImplHT implements StudentDaoInterface {
private HibernateTemplate ht;
public void setHt(HibernateTemplate ht) {
this.ht = ht;
}
@Override
public int save(Student st) {
int i=(Integer)ht.save(st);
return i;
}
@Override
public boolean update(Student st) {
ht.update(st);
return true;
}
@Override
public boolean delete(Student st) {
ht.delete(st);
return true;
}
@Override
public Student findbyPK(int pk) {
Student st = (Student)ht.get(Student.class,pk);
return st;
}
@Override
public List<Student> findAllUsingHQL() {
List list = ht.find("from student");
return list;
}
@Override
public List<Student> findAllUsingCriteria() {
DetachedCriteria dc = DetachedCriteria.forClass(Student.class);
List list = ht.findByCriteria(dc);
return list;
}
}
Student.java
package model;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name="student",schema="system")
public class Student {
@Id
private int id;
private String name;
private String email;
private String address;
public Student(int id, String name, String email, String address) {
super();
this.id = id;
this.name = name;
this.email = email;
this.address = address;
}
public void setId(int id) {
this.id = id;
}
public void setName(String name) {
this.name = name;
}
public void setEmail(String email) {
this.email = email;
}
public void setAddress(String address) {
this.address = address;
}
}
的test.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.0.xsd">
<bean id="bds" class="org.apache.tomcat.dbcp.dbcp.BasicDataSource">
<property name="driverClassName" value="oracle.jdbc.OracleDriver"/>
<property name="url" value="jdbc:oracle:thin:@localhost:1521:xe"/>
<property name="username" value="system"/>
<property name="password" value="manager"/>
<property name="maxActive" value="15"/>
<property name="maxIdle" value="5"/>
<property name="maxWait" value="5000"/>
</bean>
<bean id="sf" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="bds" />
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.OracleDialect</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
<property name="annotatedClasses">
<list>
<value>model.Student</value>
</list>
</property>
</bean>
<bean id="ht" class="org.springframework.orm.hibernate4.HibernateTemplate">
<property name="sessionFactory" ref="sf" />
</bean>
<bean id="dao" class="dao.StudentDaoImplHT">
<property name="ht" ref="ht" />
</bean>
</beans>
SaveClient.java
package test;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import dao.StudentDaoInterface;
import model.Student;
public class SaveClient {
public static void main(String[] args){
ConfigurableApplicationContext cap = new ClassPathXmlApplicationContext("resources/test.xml");
StudentDaoInterface dao = (StudentDaoInterface)cap.getBean("dao");
Student st = new Student(222,"bbb","bbb@gmail.com","hyd");
dao.save(st);
System.out.println("success");
cap.close();
}
}
运行代码 SaveClient.java 后,我面临以下错误:
Dec 17, 2017 9:27:52 AM org.springframework.context.support.AbstractApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@3f8f9dd6: startup date [Sun Dec 17 09:27:52 IST 2017]; root of context hierarchy
Dec 17, 2017 9:27:52 AM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [resources/test.xml]
Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sf' defined in class path resource [resources/test.xml]: Initialization of bean failed; nested exception is java.lang.NoClassDefFoundError: [Lorg/hibernate/engine/FilterDefinition;
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:547)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:475)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:304)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:228)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:300)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:195)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:684)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:760)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:482)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:83)
at test.SaveClient.main(SaveClient.java:13)
Caused by: java.lang.NoClassDefFoundError: [Lorg/hibernate/engine/FilterDefinition;
at java.lang.Class.getDeclaredMethods0(Native Method)
at java.lang.Class.privateGetDeclaredMethods(Unknown Source)
at java.lang.Class.privateGetPublicMethods(Unknown Source)
at java.lang.Class.privateGetPublicMethods(Unknown Source)
at java.lang.Class.getMethods(Unknown Source)
at org.springframework.beans.ExtendedBeanInfoFactory.supports(ExtendedBeanInfoFactory.java:54)
at org.springframework.beans.ExtendedBeanInfoFactory.getBeanInfo(ExtendedBeanInfoFactory.java:46)
at org.springframework.beans.CachedIntrospectionResults.<init>(CachedIntrospectionResults.java:275)
at org.springframework.beans.CachedIntrospectionResults.forClass(CachedIntrospectionResults.java:188)
at org.springframework.beans.BeanWrapperImpl.getCachedIntrospectionResults(BeanWrapperImpl.java:327)
at org.springframework.beans.BeanWrapperImpl.getPropertyDescriptorInternal(BeanWrapperImpl.java:359)
at org.springframework.beans.BeanWrapperImpl.isWritableProperty(BeanWrapperImpl.java:438)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1458)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1197)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:537)
... 11 more
Caused by: java.lang.ClassNotFoundException: org.hibernate.engine.FilterDefinition
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)
... 26 more
答案 0 :(得分:1)
您应该使用org.springframework.orm.hibernate4.LocalSessionFactoryBean
代替org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean
。但在doc中,它表示如下,所以要小心你的Spring版本或从4.3.5减少你的Hibernate。如文件所示,为4.2或4.1。
LocalSessionFactoryBean的这个变体需要Hibernate 4.0或更高版本。请注意,此版本与(经过重构的)Hibernate 4.3不兼容;请升级到 Spring 4.0系列以获得完整的Hibernate 4.3支持。我们建议将此版本与最新的Hibernate 4.1.x或4.2.x 版本一起使用。