基于sprign+jpa和oracle11g base的应用 根据提议的JPA实体设置表生成选项
properties.put("hibernate.hbm2ddl.auto", "create");
实体代码,在创建出现问题的表时:
import lombok.*;
import org.hibernate.annotations.GenericGenerator;
import org.hibernate.envers.Audited;
import javax.persistence.*;
@Entity
@Table(name = "UserRecord")
@NoArgsConstructor(access = AccessLevel.PUBLIC)
@AllArgsConstructor
@Getter
@Setter
@Access(AccessType.FIELD)
public class UserRecord {
@Id
@GeneratedValue(generator = "uuid")
@GenericGenerator(name = "uuid", strategy = "uuid2")
@Column(name = "PR_KEY", unique = true)
private String prKey;
//Business Key
@Column(name = "name", length = 100, unique = false)
private String name;
//surname Key
@Column(name = "surname", length = 100, nullable = false)
private String surname;
//type Key
@Column(name = "type", length = 100, nullable = false)
private String type;
//idPayment Key
@Column(name = "idPayment", length = 64, nullable = false)
private String idPayment;
@Column(name = "User", length = 100000)
@Lob
private byte[] User;
public UserRecord(String name, String surname, String type, byte[] User) {
this.name = name;
this.surname = surname;
this.type = type;
this.User = User;
}
public UserRecord(String name, String surname, String type,
String idPayment, byte[] User) {
this.name = name;
this.surname = surname;
this.type = type;
this.User = User;
this.idPayment = idPayment;
}
}
我在这里prKey- 这是数据库中的关键。
name, surname, typeand idPayment- 业务键,它们的组合在表中必须是唯一的,但在数据库计划中它应该只是字段。
当我尝试生成此表时,出现错误
.SchemaExport perform ORA-02261: such unique or primary key already exists in the table jpa
同时,日志中的请求尝试如下执行:
Hibernate: create table UserRecord (PR_KEY varchar2(255) not null unique, name varchar2(100), idPayment varchar2(64) not null, riskMetric blob, type varchar2(100) not null, surname varchar2(100) not null, version number(10,0) not null, primary key (PR_KEY), unique (name, surname, type, idPayment))
我对hibernate不太熟悉(一点也不熟悉),但是,据我所知,问题出在此处:
问题是在生成的查询中
你要求做同样的事情两次。Oracle 在您编写
PR_KEY varchar2(255) not null unique和编写primary key (PR_KEY). 您需要一件事,在这种情况下,unique从描述中删除该字段 - 然后将执行请求。我怀疑,为了让 Hibernate 正确生成它,将其unique从注释中删除就足够了。