org.hibernate.impl.SessionFactoryImpl.getClassMetadata 处的 NullPointerException
不知道如何表达这一点,因为我不确定什么是 null。
我有一个 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
更多代码如下:
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测试。尝试为我的代码设置测试环境。
关于配置哪里混乱或者我错过了什么必要的地方有什么想法吗?
谢谢
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我认为错误的部分是
您没有发布 ReservationDAO 的代码,但我认为它
应该像您的情况
一样并且
昨天遇到类似的问题,我在测试中使用了与您类似的通用 dao 类,并且必须不使用泛型进行实现。所以我认为对于 hibernate-generic-dao 来说,了解它在哪个类上工作很重要。
I think the wrong part is
You don't post code of ReservationDAO, but i think it something like
in your case it should be
and
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.
添加这个作为另一个答案,因为我遇到了完全相同的错误堆栈跟踪。但我的情况不同,我的实体类不在上下文文件中属性“packagesToScan”下指定的包中。确保您的实体类位于以下属性下指定的包中。
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.
当您的
type
为null
时,可能会发生:或
因为您没有在 DAOImpl 中指定类型。 DAOImpl 类中的构造函数可以解决该问题:
may occur when your
type
isnull
in:or
Because you aren't specifying type in DAOImpl. Constructor in DAOImpl class can solve the problem: