Hibernate: Implicit & Explicit Polymorphism

As I was going through the various inheritance strategies in Hibernate, I came across the ‘class’ element’s attribute, polymorphism=”implicit|explicit”.

From the Hibernate’s reference manual, this is what I found as definitions for implicit and explicit polymorphism.

Implicit Polymorphism:

Implicit polymorphism means that instances of the class will be returned by a query that names any superclass or implemented interface or the class and that instances of any subclass of the class will be returned by a query that names the class itself. For most purposes the default, polymorphism=”implicit”, is appropriate.

In simple terms, for getting a better idea about Implicit polymorphism, here is a scenario. Consider a program that manipulates lists of several different types of elements. One approach is to define a separate composite class hierarchy for each kind of list. But this approach requires replicating essentially the same code in the definition of each class. To avoid this code replication, we can define a single composite class hierarchy for lists of type Object. Since all Java object types are subtypes of Object, such a list type can be used in place of any specific list type. However, when we extract an element from such a list, we will generally have to cast it to the specific type required by the context in which it is used.

Explicit Polymorphism:

Explicit polymorphism means that class instances will be returned only by queries that explicitly name that class and that queries that name the class will return only instances of subclasses mapped inside this <class> declaration as a <subclass> or <joined-subclass>.

Explicit polymorphism is useful when two different classes are mapped to the same table (this allows a “lightweight” class that contains a subset of the table columns).

Further beyond all the findings and explanations, I was still in need of a clear living example for understand the concept, especially about explicit polymorphism and its usage. Eventually I landed up in a much better place than expected which gave me a right picture of how polymorphism strategies work in Hibernate.

Light weight Class: http://www.hibernate.org/41.html

I hope the article in the above link have given you a better understanding too.

posted @   遥望星空  阅读(692)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 25岁的心里话
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现
历史上的今天:
2013-07-26 windows样式(style)参考
2011-07-26 C# Attribute 实现简单的 AOP 处理的例子(转)
2011-07-26 使用 SharpZipLib 进行压缩的辅助类,简化压缩字节数组和字符串的操作(一)(转
点击右上角即可分享
微信分享提示