Java 如何使用oracle db自动增加jpa注释中的id值?如何生成uuid值? @实体 @表(name=“联系小组”) @JsonInclude(JsonInclude.Include.NON_NULL) 公共类ContactGroup实现可序列化 { 私有静态最终长serialVersionUID=7161778136151592279L; @身份证 @GenericGenerator(name=“increment”,strategy=“increment”) @生成值(生成器=“增量”) @列(name=“GRPOUP\u ID”) 私人长id; @OneToMany(mappedBy=“contactGroup”,cascade={CascadeType.ALL},targetEntity=Contact.class) 设置联系人; } @实体 @表(name=“CONTACT”) @JsonInclude(JsonInclude.Include.NON_NULL) 公共类联系人实现可序列化 { 私有静态最终长serialVersionUID=7161778136151592279L; @身份证 @GenericGenerator(name=“increment”,strategy=“increment”) @生成值(生成器=“增量”) @列(name=“CONTACT\u ID”) 长id; @GeneratedValue(generator=“系统uuid”) @GenericGenerator(name=“system uuid”,strategy=“uuid2”) @列(name=“group”,unique=true) 字符串组; @manytone(cascade={CascadeType.ALL}) @JoinColumn(name=“GRPOUP\u ID”) 联系人组联系人组; }

Java 如何使用oracle db自动增加jpa注释中的id值?如何生成uuid值? @实体 @表(name=“联系小组”) @JsonInclude(JsonInclude.Include.NON_NULL) 公共类ContactGroup实现可序列化 { 私有静态最终长serialVersionUID=7161778136151592279L; @身份证 @GenericGenerator(name=“increment”,strategy=“increment”) @生成值(生成器=“增量”) @列(name=“GRPOUP\u ID”) 私人长id; @OneToMany(mappedBy=“contactGroup”,cascade={CascadeType.ALL},targetEntity=Contact.class) 设置联系人; } @实体 @表(name=“CONTACT”) @JsonInclude(JsonInclude.Include.NON_NULL) 公共类联系人实现可序列化 { 私有静态最终长serialVersionUID=7161778136151592279L; @身份证 @GenericGenerator(name=“increment”,strategy=“increment”) @生成值(生成器=“增量”) @列(name=“CONTACT\u ID”) 长id; @GeneratedValue(generator=“系统uuid”) @GenericGenerator(name=“system uuid”,strategy=“uuid2”) @列(name=“group”,unique=true) 字符串组; @manytone(cascade={CascadeType.ALL}) @JoinColumn(name=“GRPOUP\u ID”) 联系人组联系人组; },java,oracle,hibernate,jpa,spring-data-jpa,Java,Oracle,Hibernate,Jpa,Spring Data Jpa,你能告诉我上面的代码没有自动递增有什么错吗 给出ORA-00001:违反唯一约束 当我一次又一次地运行测试用例时 我知道如何生成uuid值,它被插入到第一条记录的空白vlaue中 您能否提前告诉我谢谢由于您使用的是Oracle DB,您可以定义并使用Oracle的序列来获取uuid,执行如下操作: 在数据库中创建序列 @Entity @Table(name = "CONTACT_GROUP") @JsonInclude(JsonInclude.Include.NON_NULL) public c

你能告诉我上面的代码没有自动递增有什么错吗

给出ORA-00001:违反唯一约束

当我一次又一次地运行测试用例时

我知道如何生成uuid值,它被插入到第一条记录的空白vlaue中


您能否提前告诉我谢谢

由于您使用的是Oracle DB,您可以定义并使用Oracle的序列来获取uuid,执行如下操作:

在数据库中创建序列

@Entity
@Table(name = "CONTACT_GROUP")
@JsonInclude(JsonInclude.Include.NON_NULL)
public class ContactGroup implements Serializable
{
private static final long serialVersionUID = 7161778136151592279L;
@Id
@GenericGenerator(name = "increment", strategy = "increment")
@GeneratedValue(generator = "increment")
@Column(name = "GRPOUP_ID")
private Long id;

@OneToMany(mappedBy="contactGroup",cascade = { CascadeType.ALL },  targetEntity = Contact.class)
Set<Contact> contacts;
}

@Entity
@Table(name = "CONTACT")
@JsonInclude(JsonInclude.Include.NON_NULL)
public class Contact implements Serializable
{
private static final long serialVersionUID = 7161778136151592279L;
@Id
@GenericGenerator(name = "increment", strategy = "increment")
@GeneratedValue(generator = "increment")
@Column(name = "CONTACT_ID")
Long id;

@GeneratedValue(generator="system-uuid")
@GenericGenerator(name="system-uuid", strategy = "uuid2")
@Column(name="group", unique=true)
String group;

@ManyToOne(cascade={CascadeType.ALL})
@JoinColumn(name = "GRPOUP_ID")
ContactGroup contactGroup;

}

在本文中,您找不到关于如何将TableGenerator与Oracle DB一起使用的示例。

Oracle不执行“自动增量”(即JPA策略“标识”)。您可以对GeneratedValue或“SEQUENCE”使用JPA“TABLE”策略,因此很容易遵守JPA规范。您的其他内容不是JPA,Hibernate Specific我已经对这些行进行了注释,即/*@GenericGenerator(name=“increment”,strategy=“increment”)@GeneratedValue(generator=“increment”)*/并尝试了@GeneratedValue(strategy=GenerationType.IDENTITY)但给出了以下错误org.hibernate.MappingException:org.hibernate.dialent.oracle10galent不支持标识键生成,正如我前面所说的……Oracle RDBMS不支持“自动增量”(又名JPA strategy IDENTITY)。它将支持表或序列,因此请使用其中之一!
CREATE SEQUENCE "CONTACT_GROUP_SEQUENCE" 
START WITH 1 
INCREMENT BY 1 -- < here is your "autoincrement" simulation
MAXVALUE 999999999
MINVALUE 1 
NOCACHE 
ORDER
public class ContactGroup implements Serializable
{

@Id
@SequenceGenerator(name = "CONTACT_GROUP_SQ", sequenceName = "CONTACT_GROUP_SEQUENCE", allocationSize = 0)
@GeneratedValue(generator = "CONTACT_GROUP_SQ", strategy = GenerationType.SEQUENCE)
@Column(name = "GRPOUP_ID")
private Long id;
...