甚至不知道如何表达这一点,因为我不确定什么是空的。
我有一个像这样配置的 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。尝试为我的代码设置测试环境。
关于配置混乱的地方或我错过了什么必要的地方有什么想法吗?
谢谢