是的,我知道,标题有点令人困惑,但情况如下:
我有两个项目,应该以相同的方式工作。他们都访问 Oracle 数据库。Oracle 数据库上的每个表都有一个作为主键的 TABLENAME_ID 列,该列与执行 TABLENAME_SEQ nextval() 以插入 TABLENAME_ID 列的 BEFORE INSERT 触发器相关联。(了解?)。在这种环境下,通过 SQL 插入可以正常工作。
那就是说让我们来看看这个问题:
我有一个使用 JPA 的 Project1,其中实体是从数据库表生成的。在这个项目上生成的实体可以正常工作,并且可以完美地进行 CRUD 和搜索。实体与 SequenceGenerator 注释相关联,插入时不会执行数据库触发器,因此无论使用什么通道(系统或 SQL DML 语句),都会按顺序生成 id 行。
现在我必须创建第二个项目,该项目的行为方式应该相同但不是。在这个项目中,当我创建一个实体时,它会检索我,即 ID = 3,但实际上在数据库上写入 ID = 4。所以,如果我得到这个相同的实体并尝试对其进行更新,JPA 会尝试再次插入它,并给我一个错误。
有什么线索吗?一些代码如下:
实体示例(来自项目 1):
@Entity
public class Category implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@SequenceGenerator(name = "CATEGORY_CATEGORYID_GENERATOR", sequenceName = "CATEGORY_SEQ", allocationSize = 1)
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "CATEGORY_CATEGORYID_GENERATOR")
@Column(name = "CATEGORY_ID", unique = true, nullable = false, precision = 10)
private long categoryId;
@Column(name = "IS_ACTIVE", nullable = false, precision = 3)
private boolean isActive;
@Column(name = "LONG_TITLE", nullable = false, length = 250)
private String longTitle;
public Category() {
}
public long getCategoryId() {
return this.categoryId;
}
public boolean getIsActive() {
return this.isActive;
}
public String getLongTitle() {
return this.longTitle;
}
public void setCategoryId(long categoryId) {
this.categoryId = categoryId;
}
public void setIsActive(boolean isActive) {
this.isActive = isActive;
}
public void setLongTitle(String longTitle) {
this.longTitle = longTitle;
}
}
实体示例(来自项目 2):
@Entity
public class Author implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@SequenceGenerator(name = "AUTHOR_AUTHORID_GENERATOR", sequenceName = "AUTHOR_AUTHOR_ID_SEQ", allocationSize = 1)
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "AUTHOR_AUTHORID_GENERATOR")
@Column(name = "AUTHOR_ID", unique = true, nullable = false, precision = 10)
private long authorId;
@Column(name = "ADDRESS_COMPLEMENT")
private String addressComplement;
@Column(name = "AUTHOR_KIND")
private int authorKind;
@Column(name = "BUILDING_NUMBER")
private String buildingNumber;
private String city;
// bi-directional many-to-one association to Product
@OneToMany(mappedBy = "author")
private List<Product> products;
public Author() {
}
public String getAddressComplement() {
return addressComplement;
}
public long getAuthorId() {
return authorId;
}
public int getAuthorKind() {
return authorKind;
}
public String getBuildingNumber() {
return buildingNumber;
}
public String getCity() {
return city;
}
public List<Product> getProducts() {
return products;
}
public void setAddressComplement(String addressComplement) {
this.addressComplement = addressComplement;
}
public void setAuthorId(long authorId) {
this.authorId = authorId;
}
public void setAuthorKind(int authorKind) {
this.authorKind = authorKind;
}
public void setBuildingNumber(String buildingNumber) {
this.buildingNumber = buildingNumber;
}
public void setCity(String city) {
this.city = city;
}
public void setProducts(List<Product> products) {
this.products = products;
}
}
编辑
如 JScoobyCed 所问,下面是如何使用第二个实体代码:
// ...
Author author = new Author();
// Here follows setters
// Code below returns, ie, author_id = 3, but saves author_id = 4 on database
author = authotEjb.create(author);
// ...
product.setAuthor(author);
// Don't find author_id = 3 on database and tries to insert author again
// or complains about PERSIST cascade on Product entitiy.
product = productEjb.update(product)
EJB 在 create() 方法上使用 EntityManager.persist(),在 update 方法上使用 EntityManager.merge()。
编辑 2
添加序列和触发代码。每个表都有自己的序列,并且它自己的触发器遵循相同的模式 TABLENAME_SEQ 和 TABLENAME_TRG:
序列码:
CREATE SEQUENCE AUTHOR_SEQ MINVALUE 1 MAXVALUE 9999999999999999999999999999 INCREMENT BY 1 START WITH 1 NOCACHE ORDER NOCYCLE;
触发代码:
create or replace
trigger author_trg before insert on author
for each row
begin
select author_seq.nextval into :new.author_id from dual;
end;