"getHibernateTemplate()" returns null

A

Amit Jain

Hi
I'm starting to learn hibernate. I found a problem in my code. I have
made the database table in MYSQL with name "userinfo" and I also made
a mapping file. But I am getting "null" while calling function
"getHibernateTemplate()":

This is the code to show the list:
============ MemberDAOImpl.java Start =========
package com.mycomp.crtracker.dao;
public class MemberDAOImpl extends HibernateDaoSupport implements
DataAccessObject{
public ValueObject findByPK(String primaryKey) throws
DataAccessException {
***
**
List myList = getHibernateTemplate().find("from MemberVO
membervo where membervo.memberid=75571");
***
**
}


}
============ MemberDAOImpl.java END =========

============ hibernate.cfg.xml Start ===========
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate
Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/
hibernate-
configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property
name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</
property>
<property
name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</
property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:
3306/crtracker</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">root</property>
<property name="hibernate.show_sql">true</property>
<property name="hibernate.current_session_context_class">thread</
property>
<mapping resource="com/mycomp/crtracker/vo/h_mapping/
userinfo.hbm.xml"/>
</session-factory>
</hibernate-configuration>
============ hibernate.cfg.xml END ===========

============ userinfo.hbm.xml Start===========
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping
DTD
3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated Feb 3, 2009 5:28:33 PM by Hibernate Tools 3.2.1.GA -->
<hibernate-mapping>
<class name="com.mycomp.crtracker.vo.MemberVO" table="userinfo"
catalog="crtracker">
<id name="userId" type="string">
<column name="USERID" length="15" />
<generator class="assigned" />
</id>
<property name="firstName" type="string">
<column name="FIRSTNAME" length="15" />
</property>
<property name="lastName" type="string">
<column name="LASTNAME" length="15" />
</property>
<property name="email" type="string">
<column name="EMAIL" length="15" />
</property>
<property name="password" type="string">
<column name="PASSWORD" length="15" />
</property>
<property name="memberId" type="java.lang.Integer">
<column name="MEMBERID" unique="true" />
</property>
</class>
</hibernate-mapping>

============ userinfo.hbm.xml END===========

============ hibernate.reveng.xml Start===========
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-reverse-engineering PUBLIC "-//Hibernate/
Hibernate
Reverse Engineering DTD 3.0//EN" "http://hibernate.sourceforge.net/
hibernate-reverse-engineering-3.0.dtd">
<hibernate-reverse-engineering>
<schema-selection match-catalog="crtracker"/>
<table-filter match-name="userinfo"/>
</hibernate-reverse-engineering>
============ hibernate.reveng.xml END===========

============ MemberVO.java Start =========
package com.mycomp.crtracker.vo;
public class MemberVO extends ValueObject implements Serializable{
private String memberId;
private String firstName;
private String lastName;
private String userId;
private String password;
private String email;
/*setters and getters for all above listed*/
}
============ MemberVO.java End =========
 
D

Daniel Pitts

Amit said:
Hi
I'm starting to learn hibernate. I found a problem in my code. I have
made the database table in MYSQL with name "userinfo" and I also made
a mapping file. But I am getting "null" while calling function
"getHibernateTemplate()":

This is the code to show the list:
You failed to include the one XML file that matters. The Spring
Application Context XML.

HibernateDaoSupport is a Spring class, (not hibernate). The intended
use is in a Spring managed container, where you inject the data source
via configuration. If you're not doing that, then you are definitely
going to get a null. If you are doing that, show us and then maybe we
can help.

Either way, you're likely to get even better responses by going to the
Spring Framework forums.
 
A

Amit Jain

Thanks for reply!!

-------------------------- applicationContext.xml START
---------------------------------------
<?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:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">

<bean id="propertyConfigurer"

class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"
p:location="/WEB-INF/jdbc.properties" />

<bean id="dataSource"

class="org.springframework.jdbc.datasource.DriverManagerDataSource"
p:driverClassName="${jdbc.driverClassName}"
p:url="${jdbc.url}"
p:username="${jdbc.username}"
p:password="${jdbc.password}" />

<!-- ADD PERSISTENCE SUPPORT HERE (jpa, hibernate, etc) -->

<!-- Hibernate SessionFactory -->
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource"><ref local="dataSource"/></property>
<property name="mappingResources">
<list>
<value>com/mycomp/crtracker/vo/h_mapping/Userinfo.hbm.xml</value>
<value>com/mycomp/crtracker/vo/h_mapping/
Crinfo.hbm.xml</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">${hibernate.dialect}</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">create</prop>
</props>
</property>
</bean>

<!-- Transaction manager for a single Hibernate SessionFactory
(alternative to JTA) -->
<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory"><ref local="sessionFactory"/></
property>
</bean>

<!-- DAO object: Hibernate implementation -->
<bean id="memberDAO" class="com.mycomp.crtracker.dao.MemberDAOImpl">
<property name="sessionFactory"><ref local="sessionFactory"/></
property>
</bean>

<bean name="/listCR"
class="com.mycomp.crtracker.controller.CRInfoListAction">
<property name="apssService">
<ref bean="apssService"/>
</property>
</bean>
<!-- ***** CRT SERVICE Start *****-->
<bean id="apssService"
class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
said:
</property>
<property name="target">
<ref bean="apssTarget"/>
</property>
<property name="transactionAttributes">
<props>
<prop key="find*">PROPAGATION_REQUIRED,readOnly</prop>
</props>
</property>
</bean>
<!-- ***** CRT SERVICE End *****-->

<!-- CRTTarget primary business object implementation START-->
<bean id="apssTarget"
class="com.mycomp.crtracker.services.APSSServiceImpl">
<property name="apssDAO"><ref local="apssDAO"/></property>
</bean>
<!-- CRTTarget primary business object implementation END-->

<!-- DAO object: Hibernate implementation START-->
<bean id="apssDAO" class="com.mycomp.crtracker.dao.APSSDAOImpl">
<property name="sessionFactory"><ref local="sessionFactory"/></
property>
</bean>
<!-- DAO object: Hibernate implementation END-->

</beans>

-------------------------- applicationContext.xml END
---------------------------------------
 
D

Daniel Pitts

Amit said:
Thanks for reply!!

-------------------------- applicationContext.xml START
[snip]

It all looks Okay to me.

Are you getting an exceptions or other error messages? How do you know
that getHibernateTemplate() is returning null?
 
A

Amit Jain

if(getHibernateTemplate()!=null){
List myList = getHibernateTemplate().find("from MemberVO membervo
where membervo.memberid=75571");
}else{
System.out.println("getHibernateTemplate() returns null value");
}
 
A

Amit Jain

Hi,

I am not sure but I think that there is issue in
RequestProcessorHelper.java because of that getHibernateTemplate()
returns null.

I am using Struts, Spring and Hibernate in my project. I customized
the RequestProcessor, Whenever request comes controller
"CRTrackerRequestProcessor" is called instead of "RequestProcessor".

Please check struts config as mentioned below.
------------ struts-config.xml -------------
<struts-config>
...
..

<controller
processorClass="com.mycomp.crtracker.controller.CRTrackerRequestProcessor"/
..
...
</struts-config>

---------------
com.mycomp.crtracker.controller.CRTrackerRequestProcessor
----------------
public class CRTrackerRequestProcessor extends RequestProcessor {
private RequestProcessorHelper helper = null;
public CRTrackerRequestProcessor(){
super();
helper = new RequestProcessorHelper();
}

public void process(HttpServletRequest request,
HttpServletResponse response) throws IOException, ServletException{
if(helper.checkMember(request, response)){
super.process(request, response);
}
}
}

In "RequestProcessorHelper" I created a new bean using "new" operator
for MemberDAOImpl. When doing this, MemberDAOImpl bean isn't managed
by spring.
--------------- com.mycomp.crtracker.controller.RequestProcessorHelper
----------------
public class RequestProcessorHelper {
public boolean checkMember(HttpServletRequest request,
HttpServletResponse response) throws IOException, ServletException{
if(memberVO == null){
try{
...
MemberDAOImpl memberDAO = new MemberDAOImpl();
...
}catch(Exception ex){}
}
}

If this is the cause, I don't know how to fix this problem. Could
anyone help me.

Thanks in advance,

Amit Jain
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,968
Messages
2,570,150
Members
46,697
Latest member
AugustNabo

Latest Threads

Top