继承JPA和Hibernate问题

时间:2014-06-30 10:52:22

标签: java hibernate inheritance jpa

我在加载某些对象时遇到了一个奇怪的问题。我使用的是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;

如果我在尝试从类A获取elementCollection之前从类B循环threeElementExampleCollection,那么当我从elementCollection加载对象时 我只获得超类(Element)对象而不是子对象。

我想,无论出于何种原因,多对多关系总是获得Element对象(父对象)并将它们保存在hibernate缓存中,但我需要避免这种行为。

任何想法或工作环境?任何形式的帮助都会非常感激。

提前致谢。

编辑:多对多的课程:

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

 ...
 }

2 个答案:

答案 0 :(得分:3)

这是一个对我有用的工作场所:Deproxy the entities。

即使拥有该实体的父代理(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)
           ...
      }
    }        
  }
)

它总会像我期待的那样得到儿童元素。

答案 1 :(得分:0)

尝试使用hibernate.default_batch_fetch_size属性进行试验。默认情况下,它设置为1.这将仅从您的集合中加载第一个实体。将它增加到集合的大小可能会有所帮助。