2013-03-25 82 views
0

我正在使用Hibernate連接到我的數據庫,當我想從數據庫加載數據我會話時得到空指針異常。我不知道爲什麼?一切看起來都對我好:< 我有這個類從數據庫中讀取:爲什麼我的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

+0

我已經更新了我的答案來說明如何你可以添加事件經理豆作爲你的春天配置文件的一部分,然後用它來檢索事件管理器實例 – 2013-03-25 16:44:44

回答

1

爲了注入的SessionFactory(多虧了註釋@Autowired),豆EventManager必須由容器實例化(即春節)和不是(如你在你的主要方法中做的那樣:EventManager eventManager = new EventManager();

+0

我是新的春天的框架,你可以告訴他們如何做到這一點? – Vardius 2013-03-25 16:27:22

+0

查看答案Ankit – ben75 2013-03-25 16:28:37

1

您應該從spring上下文中檢索EventManager實例。

您可以在您的文件中將eventManager定義爲一個bean。你可以將其定義爲:

<bean id="eventManager" class="bbstats.domain.EventManager"> 
<property name="sessionFactory" ref="sessionFactory"/> 
</bean> 

比方說,你的Spring配置文件名是springConfig.xml和eventmanager進行定義爲豆,那麼你可以使用

ApplicationContext context = 
      new ClassPathXmlApplicationContext(new String[] {"springConfig.xml"}); 

     EventManager eventManager = (EventManager)context.getBean("eventManager"); 
+0

評論它是很長,所以我編輯了我的答案,對不起 – Vardius 2013-03-25 16:35:48

+0

'線程中的異常「main」org.springframework.beans.factory.BeanCreationException:錯誤創建名爲'org.springframework.beans在類路徑資源[hibernate.cfg.xml]中定義的.factory.config.PropertyPlaceholderConfigurer#0:bean實例化失敗;嵌套異常是java.lang.NoClassDefFoundError:org/springframework/core/convert/support/PropertyTypeDescriptor' – Vardius 2013-03-25 16:48:04

+0

這個錯誤的意思是說,你有不兼容的彈簧依賴。 PropertyTypeDescriptor已從春天移除。你在使用包管理器嗎? – 2013-03-25 16:55:20

0

刪除此方法:

@Autowired 
    public void anyMethodName(SessionFactory sessionFactory) 
    { 
     setSessionFactory(sessionFactory); 
    } 

並將@Autowired註釋添加到setSessionFactory方法。

UPDATE

海這個鏈接(在這裏你可以找到解決方案):

  1. first
  2. second
  3. therd
+0

但我沒有setSessionFactory方法 – Vardius 2013-03-25 20:03:16

+0

和你在'anyMethodName'方法中調用什麼方法? – 2013-03-25 20:23:07

+0

setSessionFactory來自依賴項包含類 – Vardius 2013-03-25 21:41:41

相關問題