我想用JPA对此查询执行语义同等的操作:
INSERT INTO second_table(second_id, second_name, audit_cre_at, audit_cre_from)
SELECT first_id, first_name, audit_cre_at, audit_cre_from
FROM FIRST_TABLE f
WHERE f.FIRST_VALID = 1;
DELETE FROM FIRST_TABLE WHERE FIRST_VALID = 1;
在那些桌子上:
CREATE TABLE FIRST_TABLE(
FIRST_ID RAW(16) NOT NULL CONSTRAINT FIRST_PK PRIMARY KEY,
FIRST_NAME VARCHAR2(2000),
FIRST_VALID NUMBER(1) NOT NULL,
AUDIT_CRE_AT TIMESTAMP(0) DEFAULT CURRENT_TIMESTAMP NOT NULL,
AUDIT_CRE_FROM VARCHAR2(32) DEFAULT 'system' NOT NULL,
CONSTRAINT SECOND_VALID CHECK (SECOND_VALID IN (0,1))
);
CREATE TABLE SECOND_TABLE(
SECOND_ID RAW(16) NOT NULL CONSTRAINT SECOND_PK PRIMARY KEY,
SECOND_NAME VARCHAR2(2000),
AUDIT_CRE_AT TIMESTAMP(0) DEFAULT CURRENT_TIMESTAMP NOT NULL,
AUDIT_CRE_FROM VARCHAR2(32) DEFAULT 'system' NOT NULL,
);
这是我的FirstTable
实体:
@Entity
@Table(name = "FIRST_TABLE")
public class FirstTable{
@Id
@NotNull
@Convert("UuidConverter")
@Column(name = "FIRST_ID")
private UUID id;
@Column(name = "FIRST_NAME")
private String name;
@NotNull
@Column(name = "FIRST_VALID")
private int valid;
@NotNull
@Temporal(TIMESTAMP)
@Column(name = "AUDIT_CRE_AT")
private Date createdTime;
@NotNull
@Column(name = "AUDIT_CRE_FROM")
private String creator;
}
这是我的SecondTable
实体:
@Entity
@Table(name = "SECOND_TABLE")
public class SecondTable{
@Id
@NotNull
@Convert("UuidConverter")
@Column(name = "SECOND_ID")
private UUID id;
@Column(name = "SECOND_NAME")
private String name;
@NotNull
@Temporal(TIMESTAMP)
@Column(name = "AUDIT_CRE_AT")
private Date createdTime;
@NotNull
@Column(name = "AUDIT_CRE_FROM")
private String creator;
}
我想在我的FirstTable
实体中执行此操作。所以我想我必须设置一个SecondaryTable
来访问它:
@Entity
@Table(name = "FIRST_TABLE")
@SecondaryTable(name = "SECOND_TABLE")
@NamedQuery(...)
public class FirstTable
我不想执行上面提到的查询,但我宁愿用JPA执行类似的操作(结果应该像查询一样)。我怎么能这样做?