Search code examples
hibernatepolymorphismhibernate-mapping

Hibernate table per class hierarchy with explicit polymorphism throws can not get reflection value


I'm getting IllegalArgumentException while saving an dynamic entity,

I have two classes as SuperClassImpl and SubClassImpl, which are mapped using table per class hierarchy. Also, I used the explicit polymorphism to use same POJO for two different tables.

<class name="SuperclassImpl" polymorphism="explicit" table="super_class" mutable="true" discriminator-value="1000">
<id name='id' type='long'>
<discriminator column="data" type="int"/>
<version name='version'/>
<property name="entry"/>
<subclass name="SupclassImpl" discriminator-value="1">
    <property name="subId" column="sub_id" type="long"/>
</subclass>

<class name="SuperclassImpl" entity-name="SuperclassImplHist"  table="super_class_hist" mutable="true" discriminator-value="1000">
<composite-id>
   <key-property name="id" type="long"/>
   <key-property name="fromDate" type="imm_timestamp"/>
</composite-id>
<discriminator column="data" type="int"/>
<property name="entry"/>
<subclass name="SupclassImpl" entity-name="SupclassImplHist" discriminator-value="1">
    <property name="subId" column="sub_id" type="long"/>
</subclass>

public class SuperClassImpl implements Serializable {
private Long id;
private int version;
private String entry;

public Long getId() {
    return id;
}

public void setId(Long id) {
    this.id = id;
}

public String getEntry() {
    return entry;
}

public void setEntry(String entry) {
    this.entry = entry;
}

}

public class SubClassImpl extends SuperClassImpl {

private Date fromDate;
private Long subId;

public Date getFromDate() {
    return fromDate;
}

public void setFromDate(Date fromDate) {
    this.fromDate = fromDate;
}

public Long getSubId() {
    return id;
}

public void setSubId(Long subId) {
    this.subId = subId;
}

}

/*this below code works and saves the entity*/
SuperClassImpl sci = new SuperClassImpl();
pbe.setId(109305L);
pbe.setEntry("started");
pbe.setFromDate(new Date());
HibernateUtil.currentSession().save("SuperClassImplHist", sci);


/*not working and throws exception 
Caused by: java.lang.IllegalArgumentException: Can not set   java.lang.Long field      SubClassImpl.id to java.lang.Long*/
SubClassImpl sci = new SubClassImpl();
pbe.setId(109321L);
pbe.setSubId(2123L);
pbe.setEntry("started");
pbe.setFromDate(new Date());
HibernateUtil.currentSession().save("SubClassImplHist", sci);

Do you see any issues with the code or mapping? While saving the SubClassImplHist I get this exception

"java.lang.IllegalArgumentException: Can not set   java.lang.Long field SubClassImpl.id to java.lang.Long"

Many thanks for your answers.


Solution

  • After half a day of code debug, found that it's indeed a bug in the hibernate version I'm using which is in hibernate version 3.0.

    The issue was in the code file org.hibernate.tuple.entity.EntityMetamodel in the method

    public String findEntityNameByEntityClass(Class inheritenceClass) {
        return ( String ) entityNameByInheritenceClassNameMap.get( inheritenceClass.getName() );
    }
    

    The entityNameByInheritenceClassNameMap is holding the key with Class type. For my problem I have no fix with the current hibernate version.