2013-09-27 17 views
0

我在這方面看到很多問題,並且嘗試了許多不同解決方案的許多排列,但都沒有工作。在我的DAO中,對於一個普通的java類,hibernate sessionFactory總是爲空

我有一個道需要一個hibernate sessionfactory做交易。在SpringMVC上下文中,我已經看到它的工作,但包含在java類中的dao是null。有一個在catalina.out中沒有錯誤:

我完全applicationContext.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:tx="http://www.springframework.org/schema/tx" 
    xmlns:context="http://www.springframework.org/schema/context" 

    xsi:schemaLocation= 
    "http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans.xsd 
    http://www.springframework.org/schema/tx 
    http://www.springframework.org/schema/tx/spring-tx.xsd 
    http://www.springframework.org/schema/context 
    http://www.springframework.org/schema/context/spring-context.xsd"> 

<!-- Scan classpath for annotations (eg: @Service, @Repository etc)--> 

<context:annotation-config/> 

<context:component-scan base-package="com.shazam.di.*" /> 

<!-- JNDI Data Source. this works I can get to it independent of spring--> 
<bean id="myDataSource" class="org.springframework.jndi.JndiObjectFactoryBean" 
scope="singleton"> 
    <property name="jndiName" value="jdbc/dostudentdb"/> 
    <property name="resourceRef" value="true"/> 
</bean> 

<tx:annotation-driven/> 

<!-- Hibernate Session Factory --> 
<bean id="mySessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"> 
    <property name="dataSource" ref="myDataSource"/> 
    <property name="packagesToScan"> 
    <array> 
     <value>com.shazam.di.spring.coursemgmt.dao</value> 
    </array> 
    </property> 
    <property name="hibernateProperties"> 
     <props> 
      <prop key="hibernate.show_sql">false</prop> 
      <!--<prop key="hibernate.hbm2ddl.auto">update</prop>--> 
      <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop> 
     </props>   
    </property> 
</bean> 

<!-- Hibernate Transaction Manager --> 
<bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager"> 
    <property name="sessionFactory" ref="mySessionFactory"/> 
</bean> 

<!--I've alternated between contructor, properties for getters & setters --> 
<!--here, and nothing (just letting it get autowired into the private--> 
<!--SessionFactory instance, no effing cigar!!--> 
<bean id="studentDAO" class="org.shazam.di.spring.coursemgmt.dao.StudentDAO"> 
    <!--<constructor-arg type="SessionFactory" value="mySessionFactory"/>--> 
    <property name="insertUserProfile" ref="insertUserProfile"/> 
</bean> 
</beans> 

類爲其DAO而不是SessionFactory可以發現:

@Component 
public class CheckClassAccess 
{ 
    @Autowired 
    private static StudentDAO studentDAO;... 

的DAO的開始(試只autwiring吸氣劑& setter和構造函數):

@Repository 
@SuppressWarnings({"unchecked", "rawtypes"}) 
public class StudentDAO { 

    @Autowired 
    private SessionFactory sessionFactory; 
etc... 

的WEB XML彈簧線:

<context-param> 
    <param-name>contextConfigLocation</param-name> 
    <param-value>classpath*:WEB-INF/applicationContext.xml</param-value> 
</context-param>  
and then a little later... 
<listener> 
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 
</listener> 

其他唯一的告誡這是我試圖得到這個叫OpenCms的一個開源的Java CMS內工作。但不確定這是相關的,因爲我接線的文件是香草java支持類,而不是控制器或任何東西(並非真的想用Spring MVC做它)。

事實上,所有這些工作在一個單獨的小應用程序的Spring MVC servlet上下文中,但我似乎無法獲得這些相同的對象/註釋來註冊applicationContext。

+0

'StudentDAO'包含什麼包? –

+0

您如何評估CheckClassAccess對象,並且是這些類/這些類是否包含在com.shazam.di的子包中,並且還嘗試在包掃描中刪除。* –

+0

StudentDAO位於com.shazam.di.spring.coursemgmt.dao –

回答

0

我無法弄清楚發生了什麼事情,但我拉出我的春節,實施和使用Hibernate直接合作解決我的問題。

重新工作使用Hibernate不僅是直截了當:

  • 我在hibernate.cfg.xml中添加
  • 我在DAO的方法去除一些Spring管理的事務註釋和補充該事務管理手動返回
  • 我添加了一個靜態的最終SessionFactory單例,如https://stackoverflow.com/a/15702946/1411545
  • 我刪除了Spring庫和任何雜散的Spring註解。

整個過渡可能需要一個小時才能正常運行,並且可以讓我爲這個項目做我所需要的一切。我認爲我在這裏使用Spring做出了一個糟糕的初始選擇,不管做了什麼或不做什麼。

謝謝大家的許多有用的意見和答案。

0

如果您使用註釋命名事宜。因此,改變你的StudentDAO如下:

@Autowired 
@Qualifier("mySessionFactory") 
private SessionFactory sessionFactory; 

this更多的解釋。

或者春建議@Resource註釋:

@Resource("mySessionFactory") 
private SessionFactory sessionFactory; 
+0

偉大的信息,但不是問題。我只有一個SessionFactory,所以按類型應該沒問題。公平地說,我提出的最初問題原來不是問題。我在Spring MVC的web.xml中仍然引用了一箇舊的調度器servlet,我已經將一些東西從applicationContext.xml中移除了,並且它在抱怨。原來,這是一個紅鯡魚,但在我刪除了servlet.xml引用後,applicationContext.xml無法默默注入任何東西,現在我在上面的評論中嘗試瞭解決方案。 –

相關問題