1

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

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?

Carol Skelly
  • 351,302
  • 90
  • 710
  • 624

3 Answers3

2

You will always need a no argument constructor. If I really don't like no arg constructor to be called by my code, I make it protected.

Alex Gitelman
  • 24,429
  • 7
  • 52
  • 49
1

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.

hvgotcodes
  • 118,147
  • 33
  • 203
  • 236
0

No. That is a JPA requirement. In Hibernate you can implement an Interceptor that populates the object using as desired. See:

Community
  • 1
  • 1
user2108278
  • 391
  • 5
  • 17