3

甚至不知道如何表达这一点,因为我不确定什么是空的。

我有一个像这样配置的 hbm.xml 文件:`

<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>

`

此 hbm.xml 已正确添加到 hibernate.cfg.xml。

我正在使用一个BaseDAOImpl <T extends Entity> extends GenericDAOImpl<T, Long> implements BaseDAO<T>. 这个 Generic DAO 是 Hibernate-Generic-DAO-framework 的一个类

更多代码如下:

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

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

所以我所有的 DAO 都扩展了这个基类。

我正在使用 spring 进行会话管理。下面的示例 appContext 文件:

    <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>

在尝试练习 DAO 的课堂上,我这样称呼它:

@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);

    ...
}

上述代码的DAO实现是:

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

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

}

当它达到那个点时,它会生成以下异常:

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)

我正在使用带有 dbunit 和 spring 测试的hibernate。尝试为我的代码设置测试环境。

关于配置混乱的地方或我错过了什么必要的地方有什么想法吗?

谢谢

4

3 回答 3

1

我认为错误的部分是

public class HibernateReservationDAOImpl extends BaseDAOImpl<Entity> implements ReservationDAO

您没有发布 ReservationDAO 的代码,但我认为它类似于

public interface ReservationDAO extends BaseDAO<Entity> {
}

在你的情况下应该是

public class HibernateReservationDAOImpl extends BaseDAOImpl<Reservation> implements ReservationDAO

public interface ReservationDAO extends BaseDAO<Reservation> 

昨天刚遇到类似的问题,我在测试中使用了与您类似的通用 dao 类,并且必须在没有泛型的情况下进行实现。所以我认为对于 hibernate-generic-dao 来说,了解它在哪个类上工作很重要。

于 2012-07-11T07:26:38.590 回答
0

将此添加为另一个答案,因为我遇到了完全相同的错误堆栈跟踪。但是我的问题是不同的,我的实体类不在上下文文件中属性“packagesToScan”下指定的包中。确保您在以下属性下指定的包中的实体类。

<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>  
于 2015-01-11T18:41:50.253 回答
0

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

当您处于以下情况时可能会type发生null

private Class<T> type;

entityManager.find(type, entityId);

或者

session.createCriteria(type);

因为您没有在 DAOImpl 中指定类型。DAOImpl 类中的构造函数可以解决问题:

public ExampleClassDAOImpl() {
    super(ExampleClass.class);
}
于 2017-02-07T16:19:46.673 回答