Java 休眠中的 GenerationType.AUTO 与 GenerationType.IDENTITY
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/33096466/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
GenerationType.AUTO vs GenerationType.IDENTITY in hibernate
提问by sethu palaniyappan
Currently we are using mysql as a database and we use
目前我们使用 mysql 作为数据库,我们使用
@Generated Value(strategy = GenerationType.IDENTITY)
@Generated Value(strategy = GenerationType.IDENTITY)
It's working perfectly at certain situation we need to migrate our database to Oracle at that time it's not working properly.If any one knows what's actual difference present behind this and how it's working?
它在某些情况下运行良好,我们需要将我们的数据库迁移到 Oracle 当时它不能正常工作。如果有人知道这背后存在的实际差异以及它是如何工作的吗?
采纳答案by Neil Stockton
How could it "work properly" (you don't define basic info like what you mean by that) with Oracle ? I don't see the relevance of AUTO
to your question - that simply lets an implementation choose what it wants to use.
它如何与 Oracle 一起“正常工作”(您没有像您所说的那样定义基本信息)?我没有看到与AUTO
您的问题的相关性- 这只是让实现选择它想要使用的内容。
"IDENTITY
" (as per JPA javadocs and spec - what you should be referring to) means autoincrement. There is no such concept in Oracle, yet there is in MySQL, SQLServer and a few others. I would expect any decent JPA implementation to flag an error when even trying such a thing.
“ IDENTITY
”(根据 JPA javadocs 和规范 - 您应该指的是什么)表示autoincrement。Oracle 中没有这样的概念,但在 MySQL、SQLServer 和其他一些中却有。我希望任何体面的 JPA 实现在尝试这样的事情时都会标记错误。
Oracle would allow "SEQUENCE
", or "TABLE
" strategies to be used however
Oracle 将允许使用“ SEQUENCE
”或“ TABLE
”策略
回答by Ahmad Al-Kurdi
Quoting Java Persistence/Identity and Sequencing:
Identitysequencing uses special IDENTITY columnsin the database to allow the database to automatically assign an id to the object when its row is inserted. Identity columns are supported in many databases, such as MySQL, DB2, SQL Server, Sybase and Postgres. Oracle does not support IDENTITY columns but they can be simulated through using sequence objects and triggers.
身份排序在数据库中使用特殊的 IDENTITY 列,以允许数据库在插入行时自动为对象分配一个 id。许多数据库都支持标识列,例如MySQL、DB2、SQL Server、Sybase 和 Postgres。Oracle 不支持 IDENTITY 列,但可以通过使用序列对象和触发器来模拟它们。
so I prefer to use SEQUENCEinstead
所以我更喜欢用SEQUENCE代替
Sequence objects use special database objects to generate ids. Sequence objects are only supported in some databases, such as Oracle, DB2, and Postgres. Usually, a SEQUENCE object has a name, an INCREMENT, and other database object settings. Each time the .NEXTVAL is selected the sequence is incremented by the INCREMENT.
序列对象使用特殊的数据库对象来生成 ID。序列对象仅在某些数据库中受支持,例如 Oracle、DB2 和 Postgres。通常,SEQUENCE 对象具有名称、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;
...
}
回答by Jorge Santos Neill
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;
}
}