Hibernate继承问题

时间:2017-01-24 11:21:37

标签: java hibernate inheritance discriminator

使用DiscriminatorColumn时,我在从数据库加载表时遇到问题。只要有实体扩展我的超类(ObjectA),数据加载就没有问题。当新值出现在没有表扩展名的鉴别器列中时出现问题。我试图加载具有不同于注释(ObjectTypes.GENERIC_OBJECT)中指定的鉴别器值的ObjctA。由于我使用的是IngeritanceType.JOINED,我知道应该有一个带有id的子表来连接这些表,但是没有这样的表。我也不能添加它(要求)。

我要做的是加载具有与注释中指定的不同的鉴别器值的ObjectA类。我知道Value只接受一个值。我还尝试创建扩展ObjectA的空类,并将dicriminator值设置为所需的值。没有成功。 Hibernate将其视为实体而无法找到它。

该方案是否有解决方法?

我的实体看起来像这样:

@MappedSuperclass
public abstract class AbstractEntity implements Serializable
{...

@Entity
@Table(name = "objecta")
@Inheritance(strategy = InheritanceType.JOINED)
@DiscriminatorColumn(name = "objectclass", discriminatorType = DiscriminatorType.INTEGER)
@DiscriminatorValue(ObjectTypes.GENERIC_OBJECT)
@NamedQueries({...})
public class ObjectA extends AbstractEntity {
...

@Entity
@Table(name = "objectb")
@DiscriminatorValue(ObjectTypes.OBJECT_B)
@NamedQueries({...})
public class ObjectB extends ObjectA{

public class ObjectTypes{
    public static final String GENERIC_OBJECT = "0";
    public static final String OBJECT_B= "1";
    public static final String OBJECT_C = "2";//no extension table for that object type, 
//it is the same as GENERI_OBJECT (no additional properties)
    public static final String OBJECT_C = "3";
...}

(编辑)问题:

我无法通过执行本机查询来加载ObjectA对象(但是当ObjectA作为子对象扩展加载时我可以加载它):

@NamedQueries({ @NamedQuery(name = ObjectA.FIND_BY_ID, query = "SELECT t FROM ObjectA t WHERE t.id = :id"),
        @NamedQuery(name = ObjectA.FIND_ALL, query = "SELECT t FROM ObjectA t"),
        @NamedQuery(name = ObjectA.TOTAL_RESULT, query = "SELECT count(t) from ObjectA t"),
        @NamedQuery(name = ObjectA.FIND_BY_OBJECT_TYPE, query = "SELECT t FROM ObjectA t WHERE t.objecttype = :objectType") })
public class ObjectA extends AbstractEntity {

    private static final long serialVersionUID = 8831394270398606926L;
    public static final String FIND_BY_ID = "ObjectA.find";
    public static final String FIND_ALL = "ObjectA.findAll";
...

//loading part
    public List<ObjectA> findByObjectType(ObjectTypes objectType) {
         return em.createNamedQuery(ObjectA.FIND_BY_OBJECT_TYPE, ObjectA.class).setParameter("objectType", objectType).getResultList();
    }

堆栈追踪:

Caused by: javax.persistence.PersistenceException: org.hibernate.type.SerializationException: could not deserialize
    at org.hibernate.jpa.spi.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1692)
    at org.hibernate.jpa.spi.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1602)
    at org.hibernate.jpa.internal.QueryImpl.getResultList(QueryImpl.java:492)
    at com.mypackage.repository.ObjectARepository.findAll(ObjectARepository.java:38)
    at com.mypackage.database.DatabaseManager.init(DatabaseManager.java:77)
    ... 240 more
Caused by: org.hibernate.type.SerializationException: could not deserialize
    at org.hibernate.internal.util.SerializationHelper.doDeserialize(SerializationHelper.java:243)
    at org.hibernate.internal.util.SerializationHelper.deserialize(SerializationHelper.java:287)
    at org.hibernate.type.descriptor.java.SerializableTypeDescriptor.fromBytes(SerializableTypeDescriptor.java:138)
    at org.hibernate.type.descriptor.java.SerializableTypeDescriptor.wrap(SerializableTypeDescriptor.java:113)
    at org.hibernate.type.descriptor.java.SerializableTypeDescriptor.wrap(SerializableTypeDescriptor.java:27)
    at org.hibernate.type.descriptor.sql.VarbinaryTypeDescriptor$2.doExtract(VarbinaryTypeDescriptor.java:60)
    at org.hibernate.type.descriptor.sql.BasicExtractor.extract(BasicExtractor.java:47)
    at org.hibernate.type.descriptor.converter.AttributeConverterSqlTypeDescriptorAdapter$2.extract(AttributeConverterSqlTypeDescriptorAdapter.java:121)
    at org.hibernate.type.AbstractStandardBasicType.nullSafeGet(AbstractStandardBasicType.java:238)
    at org.hibernate.type.AbstractStandardBasicType.nullSafeGet(AbstractStandardBasicType.java:234)
    at org.hibernate.type.AbstractStandardBasicType.nullSafeGet(AbstractStandardBasicType.java:224)
    at org.hibernate.type.AbstractStandardBasicType.hydrate(AbstractStandardBasicType.java:300)
    at org.hibernate.persister.entity.AbstractEntityPersister.hydrate(AbstractEntityPersister.java:2738)
    at org.hibernate.loader.Loader.loadFromResultSet(Loader.java:1729)
    at org.hibernate.loader.Loader.instanceNotYetLoaded(Loader.java:1655)
    at org.hibernate.loader.Loader.getRow(Loader.java:1544)
    at org.hibernate.loader.Loader.getRowFromResultSet(Loader.java:727)
    at org.hibernate.loader.Loader.processResultSet(Loader.java:972)
    at org.hibernate.loader.Loader.doQuery(Loader.java:930)
    at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:336)
    at org.hibernate.loader.Loader.doList(Loader.java:2617)
    at org.hibernate.loader.Loader.doList(Loader.java:2600)
    at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2429)
    at org.hibernate.loader.Loader.list(Loader.java:2424)
    at org.hibernate.loader.hql.QueryLoader.list(QueryLoader.java:501)
    at org.hibernate.hql.internal.ast.QueryTranslatorImpl.list(QueryTranslatorImpl.java:371)
    at org.hibernate.engine.query.spi.HQLQueryPlan.performList(HQLQueryPlan.java:216)
    at org.hibernate.internal.SessionImpl.list(SessionImpl.java:1326)
    at org.hibernate.internal.QueryImpl.list(QueryImpl.java:87)
    at org.hibernate.jpa.internal.QueryImpl.list(QueryImpl.java:606)
    at org.hibernate.jpa.internal.QueryImpl.getResultList(QueryImpl.java:483)
    ... 242 more
Caused by: java.io.StreamCorruptedException: invalid stream header: 7B227363
    at java.io.ObjectInputStream.readStreamHeader(ObjectInputStream.java:806)
    at java.io.ObjectInputStream.<init>(ObjectInputStream.java:299)
    at org.hibernate.internal.util.SerializationHelper$CustomObjectInputStream.<init>(SerializationHelper.java:309)
    at org.hibernate.internal.util.SerializationHelper$CustomObjectInputStream.<init>(SerializationHelper.java:299)
    at org.hibernate.internal.util.SerializationHelper.doDeserialize(SerializationHelper.java:218)
    ... 272 more

1 个答案:

答案 0 :(得分:0)

使用canActive时,不需要InheritanceType.JOINED(这适用于DiscriminatorColumn继承)。

对于SINGLE_TABLE,您的子类中需要以下内容:

InheritanceType.JOINED

您的子类的表将需要一个列@PrimaryKeyJoinColumn(name = "parent_id", referencedColumnName = "id") ,该列既是表的parent_id,也是列primary key的{​​{1}}(主键) )在与超类对应的表中。