当前位置: 首页 > 面试题库 >

Spring 3.1 Hibernate 4继承的异常[无法转换为org.hibernate.mapping.RootClass]

谢海阳
2023-03-14
问题内容

嗨,我刚刚开始将Spring和Hibernate4和maven一起使用。基本上,我的类层次结构是HUmanMicroTask从MicroTask扩展而来。将来可能会从MicroTask扩展其他几个类。我试图为每个具体的类创建一个表,这是使用spring3和hibernate
4来启动和运行的最简单方法。但是,当我运行代码时。我不断收到以下异常

13:11:52,260 ERROR TestContextManager:324 - Caught exception while allowing TestExecutionListener [org.springframework.test.context.support.DependencyInjectionTestExecutionListener@6ef137d] to prepare test instance [HumanMicroTaskBaseHibernateTest@52c05d3b]
java.lang.IllegalStateException: Failed to load ApplicationContext
    at org.springframework.test.context.TestContext.getApplicationContext(TestContext.java:157)
    at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.injectDependencies(DependencyInjectionTestExecutionListener.java:109)
    at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.prepareTestInstance(DependencyInjectionTestExecutionListener.java:75)
    at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:321)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTest(SpringJUnit4ClassRunner.java:211)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner$1.runReflectiveCall(SpringJUnit4ClassRunner.java:288)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.methodBlock(SpringJUnit4ClassRunner.java:290)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:231)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:47)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222)
    at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
    at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:71)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:300)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:174)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in class path resource [database-config.xml]: Invocation of init method failed; nested exception is java.lang.ClassCastException: org.hibernate.mapping.UnionSubclass cannot be cast to org.hibernate.mapping.RootClass
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1455)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:519)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:294)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:225)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:291)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:567)
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:913)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:464)
    at org.springframework.test.context.support.AbstractGenericContextLoader.loadContext(AbstractGenericContextLoader.java:103)
    at org.springframework.test.context.support.AbstractGenericContextLoader.loadContext(AbstractGenericContextLoader.java:1)
    at org.springframework.test.context.support.DelegatingSmartContextLoader.loadContext(DelegatingSmartContextLoader.java:228)
    at org.springframework.test.context.TestContext.loadApplicationContext(TestContext.java:124)
    at org.springframework.test.context.TestContext.getApplicationContext(TestContext.java:148)
    ... 24 more
Caused by: java.lang.ClassCastException: org.hibernate.mapping.UnionSubclass cannot be cast to org.hibernate.mapping.RootClass
    at org.hibernate.cfg.annotations.PropertyBinder.bind(PropertyBinder.java:212)
    at org.hibernate.cfg.annotations.PropertyBinder.makePropertyValueAndBind(PropertyBinder.java:203)
    at org.hibernate.cfg.AnnotationBinder.processElementAnnotations(AnnotationBinder.java:2013)
    at org.hibernate.cfg.AnnotationBinder.processIdPropertiesIfNotAlready(AnnotationBinder.java:768)
    at org.hibernate.cfg.AnnotationBinder.bindClass(AnnotationBinder.java:687)
    at org.hibernate.cfg.Configuration$MetadataSourceQueue.processAnnotatedClassesQueue(Configuration.java:3431)
    at org.hibernate.cfg.Configuration$MetadataSourceQueue.processMetadata(Configuration.java:3385)
    at org.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:1337)
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1727)
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1778)
    at org.springframework.orm.hibernate4.LocalSessionFactoryBuilder.buildSessionFactory(LocalSessionFactoryBuilder.java:184)
    at org.springframework.orm.hibernate4.LocalSessionFactoryBean.afterPropertiesSet(LocalSessionFactoryBean.java:314)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1514)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1452)
    ... 38 more

尽管我看过很多论坛,但我不确定要在哪里犯错误。我的MicroTask类如下所示:

@Entity
@Table(name = "MICROTASK")
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
public class MicroTask {
    @Id
    @GeneratedValue(generator = "system-uuid")
    @GenericGenerator(name = "system-uuid", strategy = "uuid")
    @Column(name = "MICROTASKID")
    private String microTaskId;
    @Column(name = "CREATIONDATE")
    private Date creationDate;
    @Column(name = "DESCRIPTION")
    private String description;

    public Date getCreationDate() {
        return creationDate;
    }
//More Getters and setters

我的HumanMicroTask类如下所示:

    @Entity
    @Table(name = "HUMANMICROTASK")
    @AttributeOverrides({
        @AttributeOverride(name="microTaskId", column=@Column(name="MICROTASKID")),
        @AttributeOverride(name="creationDate", column=@Column(name="CREATIONDATE")),
        @AttributeOverride(name="description", column=@Column(name="DESCRIPTION"))
    })
    public class HumanMicroTask extends MicroTask {


        @Column(name = "TITLE")
        private String title;
        @Column(name = "CHANNEL")
        private String channel;
        @Id
        @Column(name = "HMTID")
        private String humanMicroTaskid;

        public String getId() {
            return humanMicroTaskid;
        }
//More Getters and setters

我的config.html" target="_blank">xml如下所示:

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
    xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
        ">

    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
        destroy-method="close">
        <property name="driverClass">
            <value>${jdbc.driver.className}</value>
        </property>
        <property name="jdbcUrl">
            <value>${jdbc.url}</value>
        </property>
        <property name="user">
            <value>${jdbc.username}</value>
        </property>
        <property name="password">
            <value>${jdbc.password}</value>
        </property>

    </bean>

    <bean id="sessionFactory"
        class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
        <property name="dataSource">
            <ref bean="dataSource"/>
        </property>
        <property name="packagesToScan" value="com.hp.hpl.crowdcloud" />
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">${jdbc.hibernate.dialect}</prop>
                <prop key="hibernate.hbm2ddl.auto">create</prop>
                <!-- uncomment this for first time run -->
                <prop key="hibernate.hbm2ddl.auto">create</prop>
                <prop key="hibernate.show_sql">true</prop>
            </props>
        </property>
    </bean>
    <bean id="transactionManager"
        class="org.springframework.orm.hibernate4.HibernateTransactionManager">
        <property name="sessionFactory">
            <ref bean="sessionFactory" />
        </property>
    </bean>
    <tx:annotation-driven />

</beans>

我的Maven配置

    <maven.test.failure.ignore>true</maven.test.failure.ignore>
    <org.springframework.version>3.1.0.RELEASE</org.springframework.version>
    <hibernate.version>4.1.1.Final</hibernate.version>
    <sl4j.version>1.5.6</sl4j.version>

请帮我。我不确定在哪里犯错。


问题答案:

这是由于两个类中的Id列。从HumanMicroTask中删除ID。



 类似资料:
  • 问题内容: 我正在一个项目中,该项目涉及连接到远程服务器,等待响应,然后根据该响应执行操作。我们捕获了两个不同的异常,并且根据捕获的异常而表现不同。例如: 为了测试这一点,我们编写了如下测试 如果我直接运行该函数,一切都会按预期进行。我什至通过在函数的子句中进行测试。但是当我运行单元测试时,我得到 我试图更改要修补的异常,但得到的错误大致相同。 我已经读过,所以我认为它在某处一定是一个不好的选择,

  • 可以从共用基类派生各种异常类。如果 catch 捕获基类类型异常对象的指针或引用,则也可以捕获该基类所派生的异常对象的指针或引用。这样允许相关错误的多态处理。 调试与调试提示 13.2 利用异常继承使异常处理器可以用相当简单的符号捕获相关错误。虽然可以捕获每个派生类异常对象的指针或引用,但更简练的方法是捕获基类异常对象的指针或引用,另外.如果程序员忘记测试一个或几个派生类指针或引用,则捕获每个派生

  • 问题内容: 您好,我正在使用developenig Java Web应用程序,并且在尝试使用hibernate模式获取数据时遇到了下一个异常 这里stacktrace 这是我的代码 这是我的对象 这是映射 我正在使用Oracle Weblogic 12c服务器 问题答案: 通过将我的pom.xml中的依赖项更改为较旧的版本4.3.4进行了修复。最终错误已在4.3.4。最终版本更改为4.2.7。 不

  • Deal和DealDocument类是相同的。Deal document类如下所示: 你觉得问题可能是什么?它似乎是随机出现的,好像它工作正常,但在时间内系统退化 *更新*:这是我如何配置模型映射器的:ModelMapper.GetConfiguration().SetAmbiguity忽略(false).SetFieldMatchingEnabled(true).SetMatchingStrat

  • 我正在做的一个项目涉及到连接到远程服务器,等待响应,然后根据响应执行操作。我们捕获了几个不同的异常,并且根据捕获的异常的不同而有不同的行为。例如: 为了测试这一点,我们编写了一个如下所示的测试

  • 问题内容: 假设我们有这个问题 扩展和覆盖,但鉴于实现现在会引发异常。由于覆盖方法不能引发新的异常,因此不会编译。 最好的解决方案是什么? 将所需的异常传播到。对我来说,这是反对封装,继承和一般OOP的(可能会引发永远不会发生的异常)。 使用呢?此解决方案不会将传播到,但是Oracle文档和其他来源指出,当“客户端代码无法执行任何操作”时,应使用异常类。并非这种情况,此异常将对恢复blablabl