开发者

NullPointerException at org.hibernate.impl.SessionFactoryImpl.getClassMetadata

Not sure how to even phrase t开发者_运维知识库his 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


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.


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>  


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);
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜