3

Not sure how to even phrase this because I am not certain what is null.

I have an hbm.xml file configured like this: `

<hibernate-mapping package="com.cottage.entity">
    <class name="Reservation" table="reservation" lazy="false">
        <id name="entityId" type="java.lang.Integer" column="reservationId"
            unsaved-value="0">
            <generator class="native">
                <param name="sequence">reservation_id_sequence</param>
            </generator>
        </id>
        <discriminator column="reservationId" insert="false" />

        ...

    </class>
</hibernate-mapping>

`

This hbm.xml is properly added to the hibernate.cfg.xml.

I am using a BaseDAOImpl <T extends Entity> extends GenericDAOImpl<T, Long> implements BaseDAO<T>. This Generic DAO is a class from Hibernate-Generic-DAO-framework

More code below:

public abstract class BaseDAOImpl <T extends Entity> extends GenericDAOImpl<T, Long> implements BaseDAO<T> {

@Autowired
@Override
public void setSessionFactory(SessionFactory sessionFactory) {
    super.setSessionFactory(sessionFactory);
}

So all my DAOs extend this base class.

I am using spring for session management. Sample appContext file below:

    <beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:util="http://www.springframework.org/schema/util"
    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-2.5.xsd
           http://www.springframework.org/schema/util
           http://www.springframework.org/schema/util/spring-util-2.5.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-2.5.xsd ">

    <context:annotation-config />
    <context:component-scan base-package="com.cottage" />
    <bean id="dataSource"
        class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="${connection.driver_class}" />
        <property name="url" value="${connection.url}" />
        <property name="username" value="${connection.username}" />
        <property name="password" value="${connection.password}" />
    </bean>

    <bean id="sessionFactory" depends-on="liquibase"
        class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="configLocation">
            <value>
                classpath:hibernate.cfg.xml
        </value>
        </property>
        <property name="hibernateProperties">
            <util:property-path path="appSettings.resolvedProps" />
        </property>
    </bean>

    <tx:annotation-driven transaction-manager="transactionManager" />

    <bean id="transactionManager"
        class="org.springframework.orm.hibernate3.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory" />
    </bean>

    <bean name="openSessionInViewInterceptor"
        class="org.springframework.orm.hibernate3.support.OpenSessionInViewInterceptor">
        <property name="sessionFactory" ref="sessionFactory" />
    </bean>

    ...
</beans>

In the class trying to exercise the DAO, i call it like so:

@Test
@Transactional
public void getReservationShouldRetrieveAReservationById() {

    final ReservationDAO mockReservationDAO = context.mock(ReservationDAO.class);
    context.checking(new Expectations() {
        {
            oneOf(mockReservationDAO).getReservation(0);
            will(returnValue(reservation1));
        }
    });

    assertNotNull(reservationDAO.getSession());
    Reservation returnedReservation = reservationDAO.getReservation(0);

    ...
}

The DAO implementation for the above code is:

@Repository("reservationDAO")
public class HibernateReservationDAOImpl extends BaseDAOImpl<Entity> implements ReservationDAO {

public Reservation getReservation(int reservationId) {
    return (Reservation) searchUniqueByPropertyEqual("entityId", reservationId);
}

}

When it hits that point, it generates the following exception:

java.lang.NullPointerException
at org.hibernate.impl.SessionFactoryImpl.getClassMetadata(SessionFactoryImpl.java:694)
at com.trg.search.hibernate.HibernateMetadataUtil.getUnproxiedClass(HibernateMetadataUtil.java:126)
at com.trg.search.hibernate.HibernateMetadataUtil.get(HibernateMetadataUtil.java:91)
at com.trg.search.hibernate.HibernateMetadataUtil.get(HibernateMetadataUtil.java:103)
at com.trg.search.BaseSearchProcessor.prepareValue(BaseSearchProcessor.java:707)
at com.trg.search.BaseSearchProcessor.filterToQL(BaseSearchProcessor.java:453)
at com.trg.search.BaseSearchProcessor.generateWhereClause(BaseSearchProcessor.java:413)
at com.trg.search.BaseSearchProcessor.generateQL(BaseSearchProcessor.java:111)
at com.trg.search.hibernate.HibernateSearchProcessor.searchUnique(HibernateSearchProcessor.java:196)
at com.trg.dao.hibernate.HibernateBaseDAO._searchUnique(HibernateBaseDAO.java:600)
at com.trg.dao.hibernate.GenericDAOImpl.searchUnique(GenericDAOImpl.java:125)
at com.cottage.dao.hibernate.impl.BaseDAOImpl.searchUniqueByPropertyEqual(BaseDAOImpl.java:36)
at com.cottage.dao.hibernate.impl.HibernateReservationDAOImpl.getReservation(HibernateReservationDAOImpl.java:22)
at com.cottage.dao.hibernate.impl.HibernateReservationDAOTest.getReservationShouldRetrieveAReservationById(HibernateReservationDAOTest.java:63)

I am using hibernate with dbunit and spring tests. Trying to set up a test environment for my code.

Any ideas on where the configuration is messed or where I missed something imperative?

Thanks

  • @skaffman. I am using hibernate 3.2.5.ga. FYI, i am using hibernate-generic-dao 0.5.1. Hope this helps –  Feb 25 '11 at 18:09
  • I just ran into this as well, while searching a collection of String within a parent object. I'm disappointed not to see a solution. – Nerdfest Jul 04 '12 at 19:32
  • I am sorry I couldn't help. I actually had to just delete the whole config and start over again. Sorry –  Jul 05 '12 at 09:41

3 Answers3

1

I think the wrong part is

public class HibernateReservationDAOImpl extends BaseDAOImpl<Entity> implements ReservationDAO

You don't post code of ReservationDAO, but i think it something like

public interface ReservationDAO extends BaseDAO<Entity> {
}

in your case it should be

public class HibernateReservationDAOImpl extends BaseDAOImpl<Reservation> implements ReservationDAO

and

public interface ReservationDAO extends BaseDAO<Reservation> 

Just run into similar issue yesterday, i used generic dao class similar to yours in my test, and have to do implementation without generic. So i think for hibernate-generic-dao is important to know with what class it is working.

user1516873
  • 5,060
  • 2
  • 37
  • 56
  • This seems to be the correct answer. This happened over a year ago and I do not have access to it now but your answer is in sync with how I did it then (and how I would have done it again if I had not read your answer). –  Jul 12 '12 at 07:54
0

Adding this as another answer, as I ran into an exactly same error stacktrace. But the issue in my case was different, my Entity class was not in the package specified under property 'packagesToScan' in context file. Make sure your Entity class in packages specified under below property.

<property name="dataSource" ref="intDataSource" />
     <property name="packagesToScan">
     <array>
          <value>com.mycompany.bus.server.dao</value>
          <value>com.mycompany.bus.client.dao</value>
     </array>
 </property>  
Nirmal Mangal
  • 812
  • 1
  • 12
  • 26
0

at org.hibernate.internal.SessionFactoryImpl.locate EntityPersister(SessionFactoryImpl.java:783)

may occur when your type is null in:

private Class<T> type;

entityManager.find(type, entityId);

or

session.createCriteria(type);

Because you aren't specifying type in DAOImpl. Constructor in DAOImpl class can solve the problem:

public ExampleClassDAOImpl() {
    super(ExampleClass.class);
}
Justinas Jakavonis
  • 8,220
  • 10
  • 69
  • 114