ZimSystem ZimSystem - 1 year ago 51
Java Question

Is it possible for Hibernate to use a class with no default c'tor as a component or composite-element?

I need to use some legacy classes in Hibernate. One of the classes doesn't have a default constructor so I get a "org.hibernate.InstantiationException: No default constructor for entity: .." error. I do not need to persist this class directly. Here is the mapping:

<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 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" />

I need to persist the 'Observation' and 'Station', and want to reference the 'PointGeometry' class to persist the Station.Point. Is there any way to pull this off with 'PointGeometry' not having a default constructor?

Answer Source

No, you need a no argument constructor. Hibernate needs a way to create objects.

You might be able to create a subclass of this class, and give the subclass the no-arg constructor.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download