java – 继承JPA和Hibernate问题

前端之家收集整理的这篇文章主要介绍了java – 继承JPA和Hibernate问题前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
加载一些对象时我有一个奇怪的问题.我使用JPA 1,hibernate-core版本3.3.0.SP1和hibernate-entitymanager版本3.4.0.GA

假设我有这些JPA实体:

@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> {
 ...
}

事情是,当我加载这些实体的集合时,我想永远获得子类(意思是ElementTypeOne,ElementTypeTwo而不是元素).问题是,许多关系总是得到元素(父亲而不是孩子)

假设我有一个实体A包含一个元素的集合:

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

如果我收集,一切都正常(我按预期得到子类).

当我有另一个实体B与JpaMany3ManyEntity的集合(注意涉及相同的实体元素)时,问题出现了

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

如果我从类B中循环了三个EementExampleCollection,然后我尝试从类A获取elementCollection,当我从elementCollection加载对象
我只是获取超类(Element)对象,而不是孩子.

我猜,由于任何原因,多对多的关系总是获取Element对象(父),并将它们保存在hibernate缓存中,但是我需要避免这种情况.

任何想法或工作环境?任何形式的帮助将非常感激.

提前致谢.

编辑:多对多课:

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

 ...
 }

解决方法

这是一个工作环境,适用于我:脱离实体.

即使拥有实体的父代理(jpa.inheritance.issue.Element _ $$_ javassist_1),如果你去掉它,你将获得真正的实体(子).

假设你想要从实体A循环你的(子)元素集合,然后与它们做一些事情.

就像是:

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)
           ...
      }
    }        
  }
)

它总是会像我所期待的那样得到孩子的元素.

猜你在找的Java相关文章