简体   繁体   English

休眠中的 GenerationType.AUTO 与 GenerationType.IDENTITY

[英]GenerationType.AUTO vs GenerationType.IDENTITY in hibernate

Currently, we are using MySQL as a database and we use目前,我们使用 MySQL 作为数据库,我们使用

@Generated Value(strategy = GenerationType.IDENTITY)

It's working perfectly in certain situations we need to migrate our database to Oracle at that time it's not working properly.它在某些情况下运行良好,我们需要将数据库迁移到 Oracle,但此时它无法正常运行。 If anyone knows what's the actual difference is present behind this and how it's working?如果有人知道这背后的实际区别是什么以及它是如何工作的?

How could it "work properly" (you don't define basic info like what you mean by that) with Oracle ?它如何与 Oracle 一起“正常工作”(您没有像您的意思那样定义基本信息)? I don't see the relevance of AUTO to your question - that simply lets an implementation choose what it wants to use.我没有看到AUTO与您的问题的相关性 - 这只是让实现选择它想要使用的内容。

" IDENTITY " (as per JPA javadocs and spec - what you should be referring to) means autoincrement . IDENTITY ”(根据 JPA javadocs 和规范-您应该指的是)表示autoincrement There is no such concept in Oracle, yet there is in MySQL, SQLServer and a few others.在 Oracle 中没有这样的概念,但在 MySQL、SQLServer 和其他一些中存在。 I would expect any decent JPA implementation to flag an error when even trying such a thing.我希望任何体面的 JPA 实现在尝试这样的事情时都会标记错误。

Oracle would allow " SEQUENCE ", or " TABLE " strategies to be used however但是,Oracle 将允许使用“ SEQUENCE ”或“ TABLE ”策略

Quoting Java Persistence/Identity and Sequencing :引用Java 持久性/身份和排序

Identity sequencing uses special IDENTITY columns in the database to allow the database to automatically assign an id to the object when its row is inserted.身份排序使用数据库中的特殊 IDENTITY 列来允许数据库在插入行时自动为对象分配一个 ID。 Identity columns are supported in many databases, such as MySQL, DB2, SQL Server, Sybase and Postgres .许多数据库都支持标识列,例如MySQL、DB2、SQL Server、Sybase 和 Postgres Oracle does not support IDENTITY columns but they can be simulated through using sequence objects and triggers. Oracle 不支持 IDENTITY 列,但可以通过使用序列对象和触发器来模拟它们。

so I prefer to use SEQUENCE instead所以我更喜欢使用SEQUENCE代替

Sequence objects use special database objects to generate ids.序列对象使用特殊的数据库对象来生成 id。 Sequence objects are only supported in some databases, such as Oracle, DB2, and Postgres.仅在某些数据库中支持序列对象,例如 Oracle、DB2 和 Postgres。 Usually, a SEQUENCE object has a name, an INCREMENT, and other database object settings.通常,SEQUENCE 对象具有名称、INCREMENT 和其他数据库对象设置。 Each time the .NEXTVAL is selected the sequence is incremented by the INCREMENT.每次选择 .NEXTVAL 时,序列都会增加 INCREMENT。

Example :例子 :

@Entity
public class Employee {
    @Id
    @GeneratedValue(strategy=GenerationType.SEQUENCE, generator="EMP_SEQ")
    @SequenceGenerator(name="EMP_SEQ", sequenceName="EMP_SEQ", allocationSize=100)
    private long id;
    ...
}

如果有人正在寻找答案 - GenerationType.IDENTITY vs GenerationType.SEQUENCE vs GenerationType.AUTO那么这是一个很好的博客链接

I would recommend reading this article for fast and thorough understanding of 3 most common generation types namely AUTO , IDENTITY and SEQUENCE我建议阅读这篇文章,以快速彻底地了解 3 种最常见的生成类型,即AUTOIDENTITYSEQUENCE

COMMON GENERATION TYPES DIFFERENCE 常见的世代类型差异

Im using JPA and Oracle 11g, the solution that worked for me is the following我使用 JPA 和 Oracle 11g,对我有用的解决方案如下

package com.example.springsocial.model;

import java.util.Date;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;
import javax.persistence.UniqueConstraint;

@Entity
@Table(name = "rol", uniqueConstraints = {
        @UniqueConstraint(columnNames = "name")
})
public class Rol {
    @Id
    @GeneratedValue(strategy=GenerationType.SEQUENCE, generator="rol_sequence")
    @SequenceGenerator(name="rol_sequence", sequenceName="rol_sequence", allocationSize=100)
    private Long id;

    @Column(nullable = false)
    private String name;

    private Date createdAt;
    @Column(nullable = true)
    private Date updatedAt;
    @Column(nullable = true)
    private Integer createdBy;
    @Column(nullable = true)
    private Integer updatedBy;

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Date getCreatedAt() {
        return createdAt;
    }

    public void setCreatedAt(Date createdAt) {
        this.createdAt = createdAt;
    }

    public Date getUpdatedAt() {
        return updatedAt;
    }

    public void setUpdatedAt(Date updatedAt) {
        this.updatedAt = updatedAt;
    }

    public Integer getCreatedBy() {
        return createdBy;
    }

    public void setCreatedBy(Integer createdBy) {
        this.createdBy = createdBy;
    }

    public Integer getUpdatedBy() {
        return updatedBy;
    }

    public void setUpdatedBy(Integer updatedBy) {
        this.updatedBy = updatedBy;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM