Hibernate 4 explicit polymorphism (annotation) not working?

天涯浪子 提交于 2019-12-01 17:07:11

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.!!

If you look at the definition of PolymorphismType.EXPLICIT it says:

EXPLICIT: This entity is retrieved only if explicitly asked.

To hide the subclasses, you will have to annotate the subclasses with EXPLICIT and not the base class.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!