Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Inheritance JPA and Hibernate Issue

I've a weird problem loading some objects. I'm using JPA 1, hibernate-core version 3.3.0.SP1 and hibernate-entitymanager version 3.4.0.GA

Let's say I've these JPA entities:

@Entity
@Table(name = "SLC_ELE")
@Inheritance(strategy = InheritanceType.JOINED)
@DiscriminatorColumn(discriminatorType = DiscriminatorType.INTEGER, name = ElementoPrograma.C_ID_CTG_ELE)
public class Element {
...
} 

@Entity
@Table(name = "SLC_ELE_ONE")
@Inheritance(strategy = InheritanceType.JOINED)
@DiscriminatorValue(Categories.ID_CTG_ONE)
public class ElementTypeOne extends Element {
    ...
}

@Entity
@Table(name = "SLC_ELE_TWO")
@Inheritance(strategy = InheritanceType.JOINED)
@DiscriminatorValue(Categories.ID_CTG_TWO)
public class ElementTypeTwo extends Element {
    ...
} 

@Entity
@Table(name = ThreeElementExample.TABLENAME)
@AssociationOverrides({
    @AssociationOverride(name = JpaMany3ManyEntity.ASOCIATION_OVERRIDE_ONE,
    joinColumns =
    @JoinColumn(name = Element.C_ID_ELE)),
    @AssociationOverride(name = JpaMany3ManyEntity.ASOCIATION_OVERRIDE_TWO,
    joinColumns =
    @JoinColumn(name = OneEntity.C_ID_TWO)),
    @AssociationOverride(name = JpaMany3ManyEntity.ASOCIATION_OVERRIDE_THREE,
    joinColumns =
    @JoinColumn(name = AnotherEntity.C_ID_THREE))}) 
public class ThreeElementExample extends JpaMany3ManyEntity<Element, OneEntity, AnotherEntity> {
 ...
}

The thing is, I'd like to obtain always the subclasses (meaning the ElementTypeOne, ElementTypeTwo instead the elements) when I load a collection of these entities. The problem is that the many to many relation always get the Element (the father instead the children)

Let's say I've an entity A containing a colection of Elements:

@Fetch(FetchMode.JOIN)
@OneToMany(cascade = CascadeType.ALL, mappedBy = "idEle")
private Collection<Element> elementCollection;

And if I get the collection, everything works fine (I get the subclasses as expected).

The problem comes when I've another entity B with a collection of the JpaMany3ManyEntity (notice that the same entity element is involved)

@OneToMany(cascade = CascadeType.ALL, mappedBy = JpaMany3ManyEntity.ASOCIATION_OVERRIDE_ONE, fetch = FetchType.LAZY)
private Collection<ThreeElementExample> threeElementExampleCollection;

If I loop the threeElementExampleCollection from class B before I try to obtain the elementCollection from class A, when I load the objects from the elementCollection I obtain just the superclass (Element) objects instead the children.

I guess that, for any reason, the many to many relationship obtains always the Element objects (father) and saves them in the hibernate cache, but I need to avoid this behaviour.

Any ideas or workarround? Any kind of help would be really appreciated.

Thanks in advance.

EDIT: the many to many class:

@SuppressWarnings("serial")
@MappedSuperclass
@AssociationOverrides({
@AssociationOverride(name = JpaMany3ManyEntity.ASOCIATION_OVERRIDE_ONE,
joinColumns =
@JoinColumn(name = "changeMeWhenExtends")),
@AssociationOverride(name = JpaMany3ManyEntity.ASOCIATION_OVERRIDE_TWO,
joinColumns =
@JoinColumn(name = "changeMeWhenExtends")),
@AssociationOverride(name = JpaMany3ManyEntity.ASOCIATION_OVERRIDE_THREE,
joinColumns =
@JoinColumn(name = "changeMeWhenExtends"))})
public abstract class JpaMany3ManyEntity<A extends JpaBaseEntity, B extends JpaBaseEntity, C extends JpaBaseEntity> extends JpaBaseEntity {

public static final String ID_ATTNAME = "id";

public static final String ASOCIATION_OVERRIDE_ONE = JpaMany3ManyEntity.ID_ATTNAME + "." + JpaMany3ManyId.ID_ONE_ATTNAME;

public static final String ASOCIATION_OVERRIDE_TWO = JpaMany3ManyEntity.ID_ATTNAME + "." + JpaMany3ManyId.ID_TWO_ATTNAME;

public static final String ASOCIATION_OVERRIDE_THREE = JpaMany3ManyEntity.ID_ATTNAME + "." + JpaMany3ManyId.ID_THREE_ATTNAME; 

 ...
 }
like image 847
elcadro Avatar asked Jun 30 '14 10:06

elcadro


1 Answers

Here's a workarround that works to me: Deproxy the entities.

Even having a parent proxy of the entity (jpa.inheritance.issue.Element_$$_javassist_1) if you deproxy it, you'll obtain the real entities (children).

Let's say you want to loop your (children) elements collection from the entity A and do something with them.

Something like:

public void  loopDeproxyElements(List<Element> yourElementsCollection){
  for(Element p : yourElementsCollection){
      if(p instanceof HibernateProxy){
        Element child =   (Element) ((HibernateProxy) p).getHibernateLazyInitializer()
                    .getImplementation();

        if (child instanceof ElementTypeOne){

         //You can cast your object or do whatever you want, knowing for sure that's a child element)

          ElementTypeOne myRealElement =  (ElementTypeOne) child;
          ...
          } else {
           //It should be ElementTypeTwo (if u never create parent entities)
           ...
      }
    }        
  }
)

It will always get the children elements as I was expecting.

like image 131
elcadro Avatar answered Oct 12 '22 02:10

elcadro