Hibernate を使用してデータベースに接続していますが、データベースからデータをロードしようとすると、セッションでヌル ポインター例外が発生します。どうしてか分かりません?すべてが私にとっては良さそうです:<私はdbから読み取るこのクラスを持っています:
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