java – 继承JPA和Hibernate问题

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

假设我有这些JPA实体:

  1. @Entity
  2. @Table(name = "SLC_ELE")
  3. @Inheritance(strategy = InheritanceType.JOINED)
  4. @DiscriminatorColumn(discriminatorType = DiscriminatorType.INTEGER,name = ElementoPrograma.C_ID_CTG_ELE)
  5. public class Element {
  6. ...
  7. }
  8.  
  9. @Entity
  10. @Table(name = "SLC_ELE_ONE")
  11. @Inheritance(strategy = InheritanceType.JOINED)
  12. @DiscriminatorValue(Categories.ID_CTG_ONE)
  13. public class ElementTypeOne extends Element {
  14. ...
  15. }
  16.  
  17. @Entity
  18. @Table(name = "SLC_ELE_TWO")
  19. @Inheritance(strategy = InheritanceType.JOINED)
  20. @DiscriminatorValue(Categories.ID_CTG_TWO)
  21. public class ElementTypeTwo extends Element {
  22. ...
  23. }
  24.  
  25. @Entity
  26. @Table(name = ThreeElementExample.TABLENAME)
  27. @AssociationOverrides({
  28. @AssociationOverride(name = JpaMany3ManyEntity.ASOCIATION_OVERRIDE_ONE,joinColumns =
  29. @JoinColumn(name = Element.C_ID_ELE)),@AssociationOverride(name = JpaMany3ManyEntity.ASOCIATION_OVERRIDE_TWO,joinColumns =
  30. @JoinColumn(name = OneEntity.C_ID_TWO)),@AssociationOverride(name = JpaMany3ManyEntity.ASOCIATION_OVERRIDE_THREE,joinColumns =
  31. @JoinColumn(name = AnotherEntity.C_ID_THREE))})
  32. public class ThreeElementExample extends JpaMany3ManyEntity<Element,OneEntity,AnotherEntity> {
  33. ...
  34. }

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

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

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

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

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

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

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

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

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

提前致谢.

编辑:多对多课:

  1. @SuppressWarnings("serial")
  2. @MappedSuperclass
  3. @AssociationOverrides({
  4. @AssociationOverride(name = JpaMany3ManyEntity.ASOCIATION_OVERRIDE_ONE,joinColumns =
  5. @JoinColumn(name = "changeMeWhenExtends")),joinColumns =
  6. @JoinColumn(name = "changeMeWhenExtends"))})
  7. public abstract class JpaMany3ManyEntity<A extends JpaBaseEntity,B extends JpaBaseEntity,C extends JpaBaseEntity> extends JpaBaseEntity {
  8.  
  9. public static final String ID_ATTNAME = "id";
  10.  
  11. public static final String ASOCIATION_OVERRIDE_ONE = JpaMany3ManyEntity.ID_ATTNAME + "." + JpaMany3ManyId.ID_ONE_ATTNAME;
  12.  
  13. public static final String ASOCIATION_OVERRIDE_TWO = JpaMany3ManyEntity.ID_ATTNAME + "." + JpaMany3ManyId.ID_TWO_ATTNAME;
  14.  
  15. public static final String ASOCIATION_OVERRIDE_THREE = JpaMany3ManyEntity.ID_ATTNAME + "." + JpaMany3ManyId.ID_THREE_ATTNAME;
  16.  
  17. ...
  18. }

解决方法

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

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

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

就像是:

  1. public void loopDeproxyElements(List<Element> yourElementsCollection){
  2. for(Element p : yourElementsCollection){
  3. if(p instanceof HibernateProxy){
  4. Element child = (Element) ((HibernateProxy) p).getHibernateLazyInitializer()
  5. .getImplementation();
  6.  
  7. if (child instanceof ElementTypeOne){
  8.  
  9. //You can cast your object or do whatever you want,knowing for sure that's a child element)
  10.  
  11. ElementTypeOne myRealElement = (ElementTypeOne) child;
  12. ...
  13. } else {
  14. //It should be ElementTypeTwo (if u never create parent entities)
  15. ...
  16. }
  17. }
  18. }
  19. )

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

猜你在找的Java相关文章