2011-06-08 33 views
1

我需要在Hibernate中使用一些遗留类。其中一个类没有默认构造函数,所以我得到一个“org.hibernate.InstantiationException:没有实体的默认构造函数:..”错误。我不需要直接坚持这个课。这里是映射:Hibernate可以使用没有默认c'tor的类作为组件或复合元素吗?

<class name="test.geo.support.Observation" table="observation"> 
     <id access="property" column="obs_msg" name="EncodedObMsg" type="string"/> 
       <property name="ReportTime" column="report_time" type="long" /> 
       <many-to-one name="Station" column="station_id" class="test.geo.Station"/> 
    </class> 
    <class name="test.geo.Station" table="station"> 
     <id access="property" column="station_id" name="UniqueId" type="string" /> 
       <component name="Point" class="test.geo.geometry.PointGeometry"> 
        <property name="latitude" type="double" access="field" column="lat" /> 
        <property name="longitude" type="double" access="field" column="lon" /> 
       </component> 
    </class> 

我需要坚持的“观察”和“站”,并要参考“PointGeometry”类持久化Station.Point。有没有什么办法可以用'PointGeometry'来取消默认构造函数?

回答

1

不,你需要一个没有参数的构造函数。 Hibernate需要一种创建对象的方法。

您可能可以创建此类的子类,并为子类提供no-arg构造函数。

2

您将始终需要一个无参数构造函数。如果我真的不喜欢用我的代码调用任何参数构造函数,那么我将它设为protected

+0

无参数的构造函数,最多可以包专用 – hvgotcodes 2011-06-09 14:35:38

+0

@hvgotcodes'protected' definitelly工作,因为它是所有在我的代码。但我回过头来谈谈“私​​人”。回答编辑。 – 2011-06-09 16:02:14

+0

是受保护的将会起作用,因为它比私人包的限制性更小。注意“包私人”是一个术语。我没有说私人。 – hvgotcodes 2011-06-09 16:32:18

相关问题