Hibernate 4 explicit polymorphism (annotation) not working?

前端 未结 2 1738
南笙
南笙 2021-01-18 01:34

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 al

相关标签:
2条回答
  • 2021-01-18 02:19

    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.

    0 讨论(0)
  • 2021-01-18 02:25

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

    0 讨论(0)
提交回复
热议问题