0

I am trying to persist objects in a database using hibernate JPA.

The objects already have a type hierarchy, and I'm trying to make it work with hibernate.

A CatalogPackage object has all the important properties and all the getters. A CatalogPackageImpl (extends CatalogPackage) object has no properties, but most of the setters.

Both classes are non-abstract.

We want code to refer to CatalogPackage objects. But when initializing hibernate, it complains about the setters missing from the CatalogPackage class.

How do I suggest to hibernate that it use the subclass when building the objects?

I don't want to move all the setters to the superclass, and I don't want to use CatalogPackageImpl as the entity.

mdarwin
  • 1,684
  • 7
  • 28
  • 72
  • 1
    did you try using `@Access(AccessType.Field)` on superclass? However I don't think that the design you're describing is appropriate - why do you insist on separating getters from setters? It is very likely to cause problems with Hibernate down the road... – Blekit Apr 14 '14 at 13:13
  • Please update your question by posting your entities – VH-NZZ Apr 14 '14 at 13:16
  • @Blekit you are right, it did cause problems down the road...http://stackoverflow.com/questions/23116287/ – mdarwin Apr 16 '14 at 17:32

1 Answers1

0

Even though I can't see the problem with defining the setter methods in your CatalogPackage since they can be marked private to avoid using them from external world. Since you didn't paste your entities configuration and that you say Hibernate is complaining about the setter methods I can conclude you are using your getters to describe your entity mapping, right?

In such a case Hibernate will always still complaining because it assumes anything mapped to the database should be done in both directions, if it can read from the datastorage so it should be allowed to write in there.

SO you have either of below solutions:

  • Add the setters modifiers.
  • Move the mapping from getter methods to fields and set the acces type to field on top of your entity:

    @Access(AccessType.Field)
    class CatalogPackage {
      ...
    }
    
tmarwen
  • 15,750
  • 5
  • 43
  • 62
  • Thanks! AccessType.FIELD is useful and I've now been playing around with that to achieve what I'm trying to do – mdarwin Apr 16 '14 at 17:20