Java – inherit JPA and Hibernate issues

I have a strange problem loading some objects I use JPA 1, hibernate core version 3.3 0.sp1 and hibernate entitymanager version 3.4 0.GA

Suppose I have 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, when I load a collection of these entities, I want to always get subclasses (meaning elementtypeone, elementtypetwo, not elements) The problem is that many relationships always get elements (fathers, not children)

Suppose I have a set of entity a containing one element:

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

If I collect, everything is fine (I get subclasses as expected)

The problem arises when I have another collection of entity B and jpamany3manyentity (note 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 three eementexamplecollections from class B, and then I try to get elementcollection from Class A, when I load objects from elementcollection, I just get superclass objects, not children

I guess, for any reason, many to many relationships always get element objects (parents) and save them in the hibernate cache, but I need to avoid this

Any ideas or work environment? Any kind of help would be appreciated

Thank you in advance

Edit: many to many lessons:

@SuppressWarnings("serial")
@MappedSuperclass
@AssociationOverrides({
@AssociationOverride(name = JpaMany3ManyEntity.ASOCIATION_OVERRIDE_ONE,joinColumns =
@JoinColumn(name = "changeMeWhenExtends")),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; 

 ...
 }

Solution

This is a working environment for me: separation from entity

Even if you have the parent agent of the entity (JPA. Inheritance. Issue. Element $$$ujavassist_1), if you remove it, you will get the real entity (child)

Suppose you want to loop your collection of (child) elements from entity a and do something with them

It's 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 always gets the child's element as I expected

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>