gpt4 book ai didi

java - 为什么我的 Hibernate session 始终为空?

转载 作者:行者123 更新时间:2023-11-29 08:34:18 25 4
gpt4 key购买 nike

我正在使用 Hibernate 连接到我的数据库,当我想从数据库加载数据时,我在 session 中遇到空指针异常。我不知道为什么?一切对我来说看起来都很好:<我有这个类可以从数据库读取:

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 吗?难道有些 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

最佳答案

为了注入(inject) SessionFactory(感谢注释 @Autowired),bean EventManager 必须由容器(即 Spring)实例化,并且 自己完成(就像您在 main 方法中所做的那样:EventManager eventManager = new EventManager();)

关于java - 为什么我的 Hibernate session 始终为空?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15619725/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com