我不明白为什么在我的应用程序中插入新用户时, usr 表的主键总是递增2而不是1。我正在使用Oracle11g数据库。如果我手动插入 usr 表,一切正常。
此外, usr 表和角色表具有 usr_role 连接表,其中包含组合主键。 JPA将 usr 的正确主键插入到连接表中。只有在 usr 表中,它才会增加2。
我用来创建用户架构的SQL:
function ConvertFrom-Xml {
<#
.SYNOPSIS
Converts XML object to PSObject representation for further ConvertTo-Json transformation
.EXAMPLE
# JSON->XML
$xml = ConvertTo-Xml (get-content 1.json | ConvertFrom-Json) -Depth 4 -NoTypeInformation -as String
.EXAMPLE
# XML->JSON
ConvertFrom-Xml ([xml]($xml)).Objects.Object | ConvertTo-Json
#>
param([System.Xml.XmlElement]$Object)
if (($Object -ne $null) -and ($Object.Property -ne $null)) {
$PSObject = New-Object PSObject
foreach ($Property in @($Object.Property)) {
if ($Property.Property.Name -like 'Property') {
$PSObject | Add-Member NoteProperty $Property.Name ($Property.Property | % {ConvertFrom-Xml $_})
} else {
if ($Property.'#text' -ne $null) {
$PSObject | Add-Member NoteProperty $Property.Name $Property.'#text'
} else {
if ($Property.Name -ne $null) {
$PSObject | Add-Member NoteProperty $Property.Name (ConvertFrom-Xml $Property)
}
}
}
}
$PSObject
}
}
用户实体:
CREATE TABLE usr (
id NUMBER(19) PRIMARY KEY,
email VARCHAR2(40) NOT NULL UNIQUE,
password VARCHAR2(10) NOT NULL CHECK(LENGTH(password) >= 6),
username VARCHAR2(15) NOT NULL UNIQUE
);
GRANT SELECT, INSERT, UPDATE, DELETE ON usr TO forum_user;
DROP SEQUENCE seq_usr;
CREATE SEQUENCE seq_usr;
GRANT ALL ON seq_usr TO forum_user;
CREATE OR REPLACE TRIGGER tri_usr
BEFORE INSERT ON usr
FOR EACH ROW
BEGIN :NEW.id := seq_usr.NEXTVAL;
END;
/
这是JPA SQL输出:
@Entity
@Table(schema = "FORUM", name = "USR")
public class User implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@SequenceGenerator(
name = "USR_ID_GENERATOR",
sequenceName = "SEQ_USR",
schema = "FORUM",
allocationSize = 1,
initialValue = 1
)
@GeneratedValue(
strategy = GenerationType.SEQUENCE,
generator = "USR_ID_GENERATOR"
)
private Long id;
@NotNull
@Size(min = 3, max = 40)
@Pattern(regexp = "[a-zA-Z0-9]+@[a-zA-Z0-9]+\\.[a-zA-Z0-9]+", message = "has to be valid")
private String email;
@NotNull
@Size(min = 6, max = 10)
private String password;
@NotNull
@Size(min = 4, max = 20)
private String username;
@ManyToMany(fetch = FetchType.EAGER)
@JoinTable(name = "USR_ROLE",
joinColumns = @JoinColumn(name = "USR_ID", referencedColumnName = "ID"),
inverseJoinColumns = @JoinColumn(name = "ROLE_ID", referencedColumnName = "ID")
)
private List<Role> roles;
public User() {}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public List<Role> getRoles() {
return roles;
}
public void setRoles(List<Role> roles) {
this.roles = roles;
}
public boolean addRole(Role role) {
if (roles == null) {
roles = new ArrayList<>();
}
return roles.add(role);
}
}
答案 0 :(得分:0)
尝试连接到数据库并要求nextval在那里隔离问题是在Oracle中还是在JPA配置中有些疯狂的事情。
答案 1 :(得分:0)
我建议使用如下序列:
i
而不是使用触发器。我相信触发器只会在数据更新后执行,一旦有更新就会导致另一次更新。