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

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

问题描述

我面临着 hibernate 显式多态性的问题.我使用了多态注释并将其设置为显式,但是使用 get() 和映射类中的集合,我总是得到所有子类.我在休眠show_sql"输出中看到所有带有左连接的子类.有什么问题?我对文档的理解有误吗?或者它是hibernate 4中的一个错误?我还没有看到任何带有 hibernate 4 和多态注释的示例.

sessionFactory.getCurrentSession().get(Node.class, 111);//返回子类!@实体@Table(name="节点")@Inheritance(strategy = InheritanceType.JOINED)@Polymorphism(type=PolymorphismType.EXPLICIT)公共类节点实现可序列化{...}@实体@Table(name="人")公共类人扩展节点{}@实体@Table(name="网络")公共类网络扩展节点{}...和其他子类...

解决方案

这是一个常见的误解,我也有过同样的疑问..

这就是显式多态中真正发生的事情.

<块引用><块引用>

多态显式仅适用于根实体并防止查询命名(未映射的)超类以返回映射的子实体

在您的情况下,如果实体类节点未映射并且人员具有显式的多态性,则 Nodes 不会返回 Persons元素.

看看这段代码..

@Entity@Table(name="节点")@Inheritance(strategy = InheritanceType.JOINED)公共类节点实现可序列化{...}@实体@Polymorphism(type=PolymorphismType.EXPLICIT)@Table(name="人")公共类人扩展节点{}@实体@Polymorphism(type=PolymorphismType.EXPLICIT)@Table(name="网络")公共类网络扩展节点{}

它基本上与每个人的想法相反.!!

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天全站免登陆