目前,我们使用MySQL作为数据库,并使用
@Generated Value(strategy = GenerationType.IDENTITY)
在某些情况下,它工作得很好,我们需要将我们的数据库迁移到Oracle,那时它不能正常工作。如果有人知道这背后的实际区别是什么,以及它是如何工作的?
发布于 2015-10-13 15:29:50
它怎么能在Oracle中“正常工作”(你不能像你所说的那样定义基本信息)?我看不出AUTO与你的问题有什么关系--它只是让一个实现选择它想要使用的东西。
"IDENTITY“(根据JPA javadoc和规范--你应该指的是)意味着自动递增。在甲骨文中没有这样的概念,但在MySQL、SQLServer和其他一些公司中有。我希望任何像样的JPA实现在尝试这样的事情时都会标记出错误。
但是,Oracle将允许使用"SEQUENCE“或"TABLE”策略
发布于 2017-11-14 09:05:44
引用Java Persistence/Identity and Sequencing
IDENTITY sequencing使用数据库中的特殊标识列,以允许数据库在插入对象的行时自动为其分配id。许多数据库都支持标识列,例如MySQL、DB2、SQL Server、Sybase和Postgres。Oracle不支持标识列,但可以通过使用序列对象和触发器来模拟这些列。
所以我更喜欢使用SEQUENCE
序列对象使用特殊的数据库对象来生成ids。序列对象仅在某些数据库中受支持,如Oracle、DB2和Postgres。通常,序列对象具有名称、增量和其他数据库对象设置。每次选择.NEXTVAL时,序列都会按增量递增。
示例:
@Entity
public class Employee {
@Id
@GeneratedValue(strategy=GenerationType.SEQUENCE, generator="EMP_SEQ")
@SequenceGenerator(name="EMP_SEQ", sequenceName="EMP_SEQ", allocationSize=100)
private long id;
...
}发布于 2019-11-07 23:54:57
我正在使用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;
}
}https://stackoverflow.com/questions/33096466
复制相似问题