Note that there are some explanatory texts on larger screens.

plurals
  1. POSpring Hibernate-sessionfactory is null while trying to inject it in DAO
    text
    copied!<p>The session factory I have defined is null in DAO. Here goes my code:</p> <pre><code>@Repository public class LinkDetailsDAO { private SessionFactory sessionFactory; @Autowired public void setSessionFactory(SessionFactory sessionFactory) { this.sessionFactory = sessionFactory; } Session session = sessionFactory.getCurrentSession(); </code></pre> <p>throws an null pointer when i am trying to create a session object.</p> <p>My applicationContext:</p> <pre><code> &lt;!-- Load Hibernate related configuration --&gt; &lt;import resource="hibernate-context.xml"/&gt; &lt;context:annotation-config/&gt; &lt;context:component-scan base-package="com.Neuverd.*"/&gt; </code></pre> <p>My hibernate-context:</p> <pre><code> &lt;context:property-placeholder location="/WEB-INF/config/testapp.properties" /&gt; &lt;!-- Enable annotation style of managing transactions --&gt; &lt;tx:annotation-driven transaction-manager="transactionManager" /&gt; &lt;!-- Declare the Hibernate SessionFactory for retrieving Hibernate sessions --&gt; &lt;bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean" p:dataSource-ref="dataSource" p:configLocation="/WEB-INF/config/hibernate.cfg.xml" p:packagesToScan="com.Neuverd"/&gt; &lt;!-- Declare a datasource that has pooling capabilities--&gt; &lt;bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close" p:driverClassName="${app.jdbc.driverClassName}" p:url="${app.jdbc.url}" p:username="${app.jdbc.username}" p:password="${app.jdbc.password}" /&gt; </code></pre> <p> </p> <p>and my hibernate config file</p> <pre><code>&lt;hibernate-configuration&gt; &lt;session-factory&gt; &lt;!-- We're using MySQL database so the dialect needs to MySQL as well--&gt; &lt;property name="hibernate.dialect"&gt;org.hibernate.dialect.MySQL5InnoDBDialect&lt;/property&gt; &lt;!-- Enable this to see the SQL statements in the logs--&gt; &lt;property name="show_sql"&gt;true&lt;/property&gt; &lt;property name="hbm2ddl.auto"&gt;create&lt;/property&gt; &lt;/session-factory&gt; &lt;/hibernate-configuration&gt; </code></pre> <p>I have tried using the @Resource annotation too. but no luck. I am using Spring 3.1 and Hibernate 4.1.</p> <p>Application throws a beancreationexception for LinkDetailsDAO during start up, caused due to the above mentioned nullpointer.</p> <p>After the sessionfactory bean and transactionManager bean is created, when the container tries to create the LinkDetailsDAO bean, it fails. I don't understand why a null sessionfactory bean is created!!. Tried a sessionfactory as mentioned in the <a href="http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/orm.html#orm-session-factory-setup" rel="nofollow">spring</a> doc. Not working.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload