简体   繁体   中英

handling superclasses and subclasses with Hibernate JPA

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM