Home Database Mysql Tutorial Hibernate各种主键生成策略与配置详解

Hibernate各种主键生成策略与配置详解

Jun 07, 2016 pm 05:07 PM
hibernate

1、assigned 主键由外部程序负责生成,在 save() 之前必须指定一个。Hibernate不负责维护主键生成。与Hibernate和底层数据库都无

1assigned

主键由外部程序负责生成,在 save() 之前必须指定一个。Hibernate不负责维护主键生成。与Hibernate和底层数据库都无关,可以跨数据库。在存储对象前,必须要使用主键的setter方法给主键赋值,至于这个值怎么生成,完全由自己决定,这种方法应该尽量避免。

ud”是自定义的策略名,人为起的名字,后面均用“ud”表示。

特点:可以跨数据库,人为控制主键生成,应尽量避免。

2increment

Hibernate从数据库中取出主键的最大值(每个session只取1次),以该值为基础,每次增量为1,在内存中生成主键,不依赖于底层的数据库,因此可以跨数据库。

Hibernate调用org.hibernate.id.IncrementGenerator类里面的generate()方法,使用select max(idColumnName) from tableName语句获取主键最大值。该方法被声明成了synchronized,所以在一个独立的Java虚拟机内部是没有问题的,然而,在多个JVM同时并发访问数据库select max时就可能取出相同的值,再insert就会发生Dumplicate entry的错误。所以只能有一个Hibernate应用进程访问数据库,否则就可能产生主键冲突,所以不适合多进程并发更新数据库,适合单一进程访问数据库,不能用于群集环境。

官方文档:只有在没有其他进程往同一张表中插入数据时才能使用,在集群下不要使用。

特点:跨数据库,不适合多进程并发更新数据库,适合单一进程访问数据库,不能用于群集环境。

3hilo

hilo(高低位方式high low)是hibernate中最常用的一种生成方式,需要一张额外的表保存hi的值。保存hi值的表至少有一条记录(只与第一条记录有关),否则会出现错误。可以跨数据库。

hibernate_hilo

next_hi

100

hibernate_hilo 指定保存hi值的表名

next_hi 指定保存hi值的列名

100 指定低位的最大值

也可以省略tablecolumn配置,其默认的表为hibernate_unique_key,列为next_hi

100

hilo生成器生成主键的过程(以hibernate_unique_key表,next_hi列为例):

1. 获得hi值:读取并记录数据库的hibernate_unique_key表中next_hi字段的值,数据库中此字段值加1保存。

2. 获得lo值:从0max_lo循环取值,差值为1,当值为max_lo值时,重新获取hi值,然后lo值继续从0max_lo循环。

3. 根据公式 hi * (max_lo + 1) + lo计算生成主键值。

注意:当hi值是0的时候,那么第一个值不是0*(max_lo+1)+0=0,而是lo跳过01开始,直接是123……

max_lo配置多大合适呢?

这要根据具体情况而定,如果系统一般不重启,而且需要用此表建立大量的主键,可以吧max_lo配置大一点,这样可以减少读取数据表的次数,提高效率;反之,如果服务器经常重启,可以吧max_lo配置小一点,可以避免每次重启主键之间的间隔太大,造成主键值主键不连贯。

特点:跨数据库,hilo算法生成的标志只能在一个数据库中保证唯一。

4seqhilo

hilo类似,通过hi/lo算法实现的主键生成机制,只是将hilo中的数据表换成了序列sequence,需要数据库中先创建sequence,适用于支持sequence的数据库,如Oracle

hibernate_seq

100

特点:与hilo类似,只能在支持序列的数据库中使用。

5sequence

采用数据库提供的sequence机制生成主键,需要数据库支持sequence。如oralceDBSAP DBPostgerSQLMcKoi中的sequenceMySQL这种不支持sequence的数据库则不行(可以使用identity)。

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to integrate Hibernate in SpringBoot project How to integrate Hibernate in SpringBoot project May 18, 2023 am 09:49 AM

Integrating Hibernate in SpringBoot Project Preface Hibernate is a popular ORM (Object Relational Mapping) framework that can map Java objects to database tables to facilitate persistence operations. In the SpringBoot project, integrating Hibernate can help us perform database operations more easily. This article will introduce how to integrate Hibernate in the SpringBoot project and provide corresponding examples. 1.Introduce dependenciesIntroduce the following dependencies in the pom.xml file: org.springframework.bootspring-boot-starter-data-jpam

Java Errors: Hibernate Errors, How to Handle and Avoid Java Errors: Hibernate Errors, How to Handle and Avoid Jun 25, 2023 am 09:09 AM

Java is an object-oriented programming language that is widely used in the field of software development. Hibernate is a popular Java persistence framework that provides a simple and efficient way to manage the persistence of Java objects. However, Hibernate errors are often encountered during the development process, and these errors may cause the program to terminate abnormally or become unstable. How to handle and avoid Hibernate errors has become a skill that Java developers must master. This article will introduce some common Hib

What are the differences between hibernate and mybatis What are the differences between hibernate and mybatis Jan 03, 2024 pm 03:35 PM

The differences between hibernate and mybatis: 1. Implementation method; 2. Performance; 3. Comparison of object management; 4. Caching mechanism. Detailed introduction: 1. Implementation method, Hibernate is a complete object/relational mapping solution that maps objects to database tables, while MyBatis requires developers to manually write SQL statements and ResultMap; 2. Performance, Hibernate is possible in terms of development speed Faster than MyBatis because Hibernate simplifies the DAO layer and so on.

What is the mapping method of one-to-many and many-to-many relationships in Java Hibernate What is the mapping method of one-to-many and many-to-many relationships in Java Hibernate May 27, 2023 pm 05:06 PM

Hibernate's one-to-many and many-to-many Hibernate is an excellent ORM framework that simplifies data access between Java applications and relational databases. In Hibernate, we can use one-to-many and many-to-many relationships to handle complex data models. Hibernate's one-to-many In Hibernate, a one-to-many relationship means that one entity class corresponds to multiple other entity classes. For example, an order can correspond to multiple order items (OrderItem), and a user (User) can correspond to multiple orders (Order). To implement a one-to-many relationship in Hibernate, you need to define a collection attribute in the entity class to store

Introduction to Hibernate framework in Java language Introduction to Hibernate framework in Java language Jun 10, 2023 am 11:35 AM

Hibernate is an open source ORM framework that binds the data mapping between relational databases and Java programs to each other, making it easier for developers to access data in the database. Using the Hibernate framework can greatly reduce the work of writing SQL statements and improve the development efficiency and reusability of applications. Let's introduce the Hibernate framework from the following aspects. 1. Advantages of the Hibernate framework: object-relational mapping, hiding database access details, making development

How to perform bulk insert update operations in Hibernate? How to perform bulk insert update operations in Hibernate? Aug 27, 2023 pm 11:17 PM

In this article, we will see how to perform bulk insert/update in Hibernate. Whenever we execute a sql statement, we do it by making a network call to the database. Now, if we have to insert 10 entries into the database table, then we have to make 10 network calls. Instead, we can optimize network calls by using batch processing. Batch processing allows us to execute a set of SQL statements in a single network call. To understand and implement this, let us define our entity − @EntitypublicclassParent{@Id@GeneratedValue(strategy=GenerationType.AUTO)

How does Hibernate second level cache work? How does Hibernate second level cache work? Sep 14, 2023 pm 07:45 PM

Caching helps reduce database network calls when executing queries. Level 1 cache and session linking. It is implemented implicitly. The first level cache exists until the session object exists. Once the session object is terminated/closed, there will be no cached objects. Second level cache works for multiple session objects. It is linked with the session factory. Second level cache objects are available to all sessions using a single session factory. These cache objects will be terminated when a specific session factory is closed. To implement the second level cache we need to add the following dependencies to use the second level cache. <!--https://mvnrepository.com/artifact/net.sf.ehcache/ehcache--><de

In-depth understanding of the Java framework technology stack: explore common Java frameworks such as Spring MVC, Hibernate, MyBatis, etc. In-depth understanding of the Java framework technology stack: explore common Java frameworks such as Spring MVC, Hibernate, MyBatis, etc. Dec 26, 2023 pm 12:50 PM

Java framework technology stack: Introducing commonly used Java frameworks, such as SpringMVC, Hibernate, MyBatis, etc. With the continuous development of Java, more and more frameworks have been developed to simplify the development process. Among them, SpringMVC, Hibernate, MyBatis, etc. are one of the most commonly used frameworks in Java development. This article will introduce the basic concepts and usage of these frameworks to help readers better understand and apply these frameworks. First, let’s introduce Sp

See all articles