我正在使用 Hibernate 连接到我的数据库,当我想从数据库加载数据时,我在会话中得到空指针异常。我不知道为什么?一切对我来说都很好:<我有这个类可以从数据库中读取:
package bbstats.domain;
import bbstats.util.HibernateUtil;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import java.util.List;
public class EventManager extends HibernateUtil
{
@Autowired
SessionFactory sessionFactory;
protected Class<DbUserData> clazz;
public List<DbUserData> getAll()
{
return this.getCurrentSession().createCriteria(clazz).list();
}
protected final Session getCurrentSession()
{
return this.sessionFactory.getCurrentSession();
}
public static void main(String args[])
{
EventManager eventManager = new EventManager();
System.out.println(eventManager.getAll());
}
}
package bbstats.util;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
public class HibernateUtil extends HibernateDaoSupport
{
private SessionFactory sessionFactory;
@Autowired
public void anyMethodName(SessionFactory sessionFactory)
{
setSessionFactory(sessionFactory);
}
}
package bbstats.domain;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Table;
@Entity
@Table(name = "Users")
public class DbUserData
{
@Column(name = "id", unique = true, nullable = false)
private Long id;
@Column(name = "nick", nullable = false, length = 26)
private String title;
public DbUserData() {}
public Long getId() {
return id;
}
private void setId(Long id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
}
当我尝试运行它时,我得到了异常:
Exception in thread "main" java.lang.NullPointerException
at bbstats.domain.EventManager.getCurrentSession(EventManager.java:25)
它在这里:
protected final Session getCurrentSession()
{
return this.sessionFactory.getCurrentSession();
}
为什么?我怎样才能解决这个问题?
编辑:我有这个标签的文件是正确的吗?我没有其他带有标签的文件
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:utils="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd">
<!-- Hibernate session factory -->
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
</props>
</property>
<property name="packagesToScan" ref="sessionFactoryDomainToScan"/>
</bean>
<utils:list id="sessionFactoryDomainToScan">
<value>bbstats.domain</value>
</utils:list>
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="properties" ref="clientProperties"/>
<property name="ignoreUnresolvablePlaceholders" value="true"/>
</bean>
<bean id="clientProperties"
class="org.springframework.beans.factory.config.PropertiesFactoryBean">
<property name="locations" value="/database.properties"/>
</bean>
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="${jdbc.driverClassName}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</bean>
</beans>
还是应该为我的 EventManager 类添加一些其他 bean?可能不需要一些豆子吗?
依赖项:
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>3.2.1.RELEASE</version>
<type>jar</type>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>4.1.9.Final</version>
<type>jar</type>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-annotations</artifactId>
<version>3.5.6-Final</version>
<type>jar</type>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>net.sourceforge.jtds</groupId>
<artifactId>jtds</artifactId>
<version>1.3.0</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>3.0.3.RELEASE</version>
</dependency>
现在那个例外:
Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'eventManager' defined in class path resource [hibernate.cfg.xml]: Cannot resolve reference to bean 'sessionFactory' while setting bean property 'sessionFactory'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in class path resource [hibernate.cfg.xml]: Initialization of bean failed; nested exception is java.lang.NoClassDefFoundError: [Lorg/hibernate/engine/FilterDefinition;
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in class path resource [hibernate.cfg.xml]: Initialization of bean failed; nested exception is java.lang.NoClassDefFoundError: org/hibernate/cache/CacheProvider