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.

@Entity
@Table(name="package")
public class CatalogPackage {

    protected String mId;

    public String getId() {return mId;}
}

public class CatalogPackageImpl extends CatalogPackage {

   public void setId(String id) {
      mId=id;
   }
}

I've no idea why things are set up like this. It's supposed to provide an interface-like setup, so clients can't modify fields.

We want code to refer to CatalogPackage objects. But when trying to persist an array of CatalogPackages (which are in fact CatalogPackageImpls underneath), hibernate says this:

java.lang.IllegalArgumentException: Unknown entity: com.mycompany.catalog.internal.CatalogPackageImpl

How do I suggest to hibernate that it use the superclass when persisting the objects?

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

mdarwin
  • 1,684
  • 7
  • 28
  • 72

1 Answers1

0

This would work, there is really no other way than to make CatalogPackageImpl an entity one way or the other:

@MappedSuperclass
public class CatalogPackage {
    protected String mId;
    public String getId() {return mId;}
}

@Entity
@Table(name="package")
public class CatalogPackageImpl extends CatalogPackage {
   public void setId(String id) {
      mId=id;
   }
}
Angular University
  • 42,341
  • 15
  • 74
  • 81