Hibernate 错误地在子类而不是超类中寻找属性的设置器?

Posted

技术标签:

【中文标题】Hibernate 错误地在子类而不是超类中寻找属性的设置器?【英文标题】:Hibernate is erroneously looking for setter of property in subclass not superclass? 【发布时间】:2012-04-05 04:46:58 【问题描述】:

我有两个类,Event 类和 HazardSubmission 类,它扩展了 Event 类。

Event有一个属性dateCreated,有getDateCreated()setDateCreated(String)

当我尝试时:

HazardSubmission hs = new HazardSubmission();
hs.setDateCreated("A");

我假设它会调用 Event 类中的 setter,但它正在寻找 HazardSubmission 类中的 setter!

这是错误,非常感谢一些帮助! :)

SEVERE: Servlet.service() for servlet DispatcherServlet threw exception
org.hibernate.PropertyNotFoundException: Could not find a setter for property dateCreated in class nz.co.company.healthsafety.stripes.model.HazardSubmission
    at org.hibernate.property.BasicPropertyAccessor.createSetter(BasicPropertyAccessor.java:240)
    at org.hibernate.property.BasicPropertyAccessor.getSetter(BasicPropertyAccessor.java:233)
    at org.hibernate.mapping.Property.getSetter(Property.java:299)
    at org.hibernate.tuple.entity.PojoEntityTuplizer.buildPropertySetter(PojoEntityTuplizer.java:307)
    at org.hibernate.tuple.entity.AbstractEntityTuplizer.<init>(AbstractEntityTuplizer.java:142)
    at org.hibernate.tuple.entity.PojoEntityTuplizer.<init>(PojoEntityTuplizer.java:78)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
    at org.hibernate.tuple.entity.EntityTuplizerFactory.constructTuplizer(EntityTuplizerFactory.java:107)
    at org.hibernate.tuple.entity.EntityTuplizerFactory.constructDefaultTuplizer(EntityTuplizerFactory.java:135)
    at org.hibernate.tuple.entity.EntityEntityModeToTuplizerMapping.<init>(EntityEntityModeToTuplizerMapping.java:80)
    at org.hibernate.tuple.entity.EntityMetamodel.<init>(EntityMetamodel.java:323)
    at org.hibernate.persister.entity.AbstractEntityPersister.<init>(AbstractEntityPersister.java:456)
    at org.hibernate.persister.entity.JoinedSubclassEntityPersister.<init>(JoinedSubclassEntityPersister.java:113)
    at org.hibernate.persister.PersisterFactory.createClassPersister(PersisterFactory.java:87)
    at org.hibernate.impl.SessionFactoryImpl.<init>(SessionFactoryImpl.java:267)
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1341)
    at nz.co.company.healthsafety.stripes.action.BaseController.<clinit>(BaseController.java:36)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
    at java.lang.Class.newInstance0(Class.java:355)
    at java.lang.Class.newInstance(Class.java:308)
    at net.sourceforge.stripes.controller.AnnotatedClassActionResolver.makeNewActionBean(AnnotatedClassActionResolver.java:432)
    at net.sourceforge.stripes.controller.AnnotatedClassActionResolver.getActionBean(AnnotatedClassActionResolver.java:360)
    at net.sourceforge.stripes.controller.NameBasedActionResolver.getActionBean(NameBasedActionResolver.java:264)
    at net.sourceforge.stripes.controller.AnnotatedClassActionResolver.getActionBean(AnnotatedClassActionResolver.java:293)
    at net.sourceforge.stripes.controller.DispatcherHelper$1.intercept(DispatcherHelper.java:106)
    at net.sourceforge.stripes.controller.ExecutionContext.proceed(ExecutionContext.java:158)
    at net.sourceforge.stripes.controller.BeforeAfterMethodInterceptor.intercept(BeforeAfterMethodInterceptor.java:113)
    at net.sourceforge.stripes.controller.ExecutionContext.proceed(ExecutionContext.java:155)
    at net.sourceforge.stripes.controller.ExecutionContext.wrap(ExecutionContext.java:74)
    at net.sourceforge.stripes.controller.DispatcherHelper.resolveActionBean(DispatcherHelper.java:102)
    at net.sourceforge.stripes.controller.DispatcherServlet.resolveActionBean(DispatcherServlet.java:238)
    at net.sourceforge.stripes.controller.DispatcherServlet.service(DispatcherServlet.java:141)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at net.sourceforge.stripes.controller.StripesFilter.doFilter(StripesFilter.java:247)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:230)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:104)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:261)
    at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:844)
    at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:581)
    at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447)
    at java.lang.Thread.run(Thread.java:619)
5/04/2012 4:34:42 PM net.sourceforge.stripes.util.Log warn

我的映射 xml 的简化:

<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
    <class name="nz.co.company.healthsafety.stripes.model.Event" table="events">
        <id name="id"  type="java.lang.Integer" column="id">
            <generator class="native" />
        </id>
        <property name="dateCreated" column="dateCreated" type="timestamp" not-null="true" length="200" />

        <joined-subclass name="nz.co.company.healthsafety.stripes.model.HazardSubmission" table="hazard_submissions">
            <key column="id" />
            <property name="reporterFirstName" column="reporterFirstName" type="java.lang.String" not-null="true" length="200" />
            <property name="reporterLastName" column="reporterLastName" type="java.lang.String" not-null="true" length="200" />

        </joined-subclass>

    </class>
</hibernate-mapping>

【问题讨论】:

能否附上两个bean类的源码? 为什么要手动调用Event类的setter?..HazardSubmission的作用是什么? 【参考方案1】:

根据您的 XML,创建日期应该是时间戳,但您似乎已将该属性声明为字符串。

您能否更改其中一种类型以使其匹配?

【讨论】:

以上是关于Hibernate 错误地在子类而不是超类中寻找属性的设置器?的主要内容,如果未能解决你的问题,请参考以下文章

8- 类

java中静态变量与静态方法的继承(转)

我们可以声明一个抽象方法来返回抽象超类中的子类对象而不需要新的实例化吗?

Java中的间接子类无法访问的超类中的受保护成员

Swift 错误 - 方法不覆盖其超类中的任何方法

在子类中启动超类对象