Hibernate的4显性多态性(注释)不工作? [英] Hibernate 4 explicit polymorphism (annotation) not working?

查看:543
本文介绍了Hibernate的4显性多态性(注释)不工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我现在面临与Hibernate的明确多态性问题。我用了多态注释并将其设置为明确的,但get()和集合的映射类我总是得到所有子类。我看到左边的所有子类在Hibernateshow_sql输出加入。
有什么问题?难道我理解的文档错了吗?或者是休眠4中的错误?我还没有看到与Hibernate 4和多态性注解的例子。

SessionFactory.getCurrentSession()函数得到(Node.class,111)。 //返回子!
@实体
@Table(NAME =节点)
@Inheritance(策略= InheritanceType.JOINED)
@Polymorphism(类型= PolymorphismType.EXPLICIT)
公共类节点实现Serializable {
    ...
}@实体
@Table(NAME =人)
公共类Person扩展节点{
}
@实体
@Table(名称=网络)
公共类网络扩展节点{
}...等...子


解决方案

其共同怀念的理解,我也有同样的疑问,一旦..

这是真正发生的事情明确的多态性。


  

    

多态性明确只适用于根实体和prevent
    查询命名(未映射)超返回映射子实体


  
  
  

在你的情况下,如果实体类的节点没有映射和分别为
  具有多态性明确,然后点击节点将不会返回者
  元素


看这个code ..

  @Entity
@Table(NAME =节点)
@Inheritance(策略= InheritanceType.JOINED)
公共类节点实现Serializable {
    ...
}@实体
@Polymorphism(类型= PolymorphismType.EXPLICIT)
@Table(NAME =人)
公共类Person扩展节点{
}
@实体
@Polymorphism(类型= PolymorphismType.EXPLICIT)
@Table(名称=网络)
公共类网络扩展节点{
}

它基本上每个人都在心中存在相反的。!

I am facing problem with hibernate's explicit polymorphism. I used the polymorphism annotation and set it to explicit, but with get() and collections in mapped classes i always get all subclasses. I see all subclasses with left join in the hibernate "show_sql" output. What's the problem? Do I understand the documentation wrong? Or is it a bug in hibernate 4? I haven't seen any example with hibernate 4 and polymorphism annotation.

sessionFactory.getCurrentSession().get(Node.class, 111); // return subclasses!


@Entity
@Table(name="Nodes")
@Inheritance(strategy = InheritanceType.JOINED)
@Polymorphism(type= PolymorphismType.EXPLICIT)
public class Node implements Serializable {
    ...
}



@Entity
@Table(name="Persons")
public class Person extends Node {
}


@Entity
@Table(name="Networks")
public class Network extends Node {
}

...and other subclasses...

解决方案

Its a common miss understanding, I too had the same doubt once..

This is what really Happens in explicit polymorphism .

polymorphism explicit only applies on root entities and prevent queries naming a (unmapped) superclass to return mapped sub entities

In your case, if Entity Class Nodes were not mapped and Persons were having polymorphism explicit, then Nodes would not return Persons elements.

Look at this code..

@Entity
@Table(name="Nodes")
@Inheritance(strategy = InheritanceType.JOINED)
public class Node implements Serializable {
    ...
}



@Entity
@Polymorphism(type= PolymorphismType.EXPLICIT)
@Table(name="Persons")
public class Person extends Node {
}


@Entity
@Polymorphism(type= PolymorphismType.EXPLICIT)
@Table(name="Networks")
public class Network extends Node {
}

Its basically the reverse of what everyone have in there mind.!!

这篇关于Hibernate的4显性多态性(注释)不工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆