Hibernate中的自然标识符是什么? [英] What is a natural identifier in Hibernate?

查看:105
本文介绍了Hibernate中的自然标识符是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在阅读Hibernate文档时,我总是看到对自然标识符概念的引用。

这是否意味着实体所具有的id是由于它所保存的数据的性质?

例如,用户的名字+密码+年龄+有些东西被用作复合标识符?

解决方案

在Hibernate中,自然键通常用于查找。在大多数情况下,您将拥有自动生成的代理ID。但是这个id对于查找来说是无用的,因为你总是会查询字段,比如姓名,社会安全号码或者真实世界中的任何其他字段。



当使用Hibernate的缓存功能,这种差异非常重要:如果缓存由您的主键(代理id)编入索引,查找时不会有任何性能提升。这就是为什么您可以定义一组您要查询数据库的字段 - 自然ID。然后Hibernate可以通过自然键索引数据并提高查询性能。



看到这个出色的 blog post ,以获得更详细的解释或 RedHat页面的示例Hibernate映射文件。


While reading through the Hibernate documentation, I keep seeing references to the concept of a natural identifier.

Does this just mean the id an entity has due to the nature of the data it holds?

E.g. A user's name + password + age + something are used as a compound identitifier?

解决方案

In Hibernate, natrual keys are often used for lookups. You will have an auto-generated surrogate id in most cases. But this id is rather useless for lookups, as you'll always query by fields like name, social security number or anything else from the real world.

When using Hibernate's caching features, this difference is very important: If the cache is indexed by your primary key (surrogate id), there won't be any performance gain on lookups. That's why you can define a set of fields that you are going to query the database with - the natural id. Hibernate can then index the data by your natural key and improve the lookup performance.

See this excellent blog post for a more detailed explanation or this RedHat page for an example Hibernate mapping file.

这篇关于Hibernate中的自然标识符是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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