我的数据库中有两个表:
CREATE TABLE "D"."EMPDIR_DISPLAY"
( "PST_ENTITY_ID" NUMBER NOT NULL ENABLE,
"PST_GRP_ID" NUMBER NOT NULL ENABLE,
"FIRST_CHAR" CHAR(1 BYTE),
"DISPLAY_URL" VARCHAR2(250 BYTE) NOT NULL ENABLE,
"DISPLAY_TEXT" VARCHAR2(250 BYTE) NOT NULL ENABLE,
"ACTIVE_JOBS" NUMBER DEFAULT 0,
CONSTRAINT "PK_EMPDIR_DISPLAY_ID" PRIMARY KEY ("PST_ENTITY_ID")
USING INDEX PCTFREE 10 INITRANS 2 MAXTRANS 255 COMPUTE STATISTICS
STORAGE(INITIAL 65536 NEXT 1048576 MINEXTENTS 1 MAXEXTENTS 2147483645
PCTINCREASE 0 FREELISTS 1 FREELIST GROUPS 1 BUFFER_POOL DEFAULT)
TABLESPACE "PROVIDER" ENABLE
) PCTFREE 10 PCTUSED 40 INITRANS 1 MAXTRANS 255 NOCOMPRESS LOGGING
STORAGE(INITIAL 65536 NEXT 1048576 MINEXTENTS 1 MAXEXTENTS 2147483645
PCTINCREASE 0 FREELISTS 1 FREELIST GROUPS 1 BUFFER_POOL DEFAULT)
TABLESPACE "PROVIDER" ;
和
CREATE TABLE "D"."PST_ENTITY"
( "PST_ENTITY_ID" NUMBER(*,0) NOT NULL ENABLE,
"PST_ENTITY_GRP_NAME" VARCHAR2(50 BYTE) NOT NULL ENABLE,
"PST_ENTITY_REGION" NUMBER(*,0) NOT NULL ENABLE,
"PST_ENTITY_NAME" VARCHAR2(50 BYTE),
"PST_ENTITY_CITY" VARCHAR2(30 BYTE),
"PST_ENTITY_STATE" CHAR(2 BYTE),
"PST_ENTITY_SMALL_LOGO" VARCHAR2(100 BYTE),
"PST_ENTITY_LARGE_LOGO" VARCHAR2(100 BYTE),
"PST_ENTITY_HOME_URL" VARCHAR2(500 BYTE),
"PST_ENTITY_CAREER_URL" VARCHAR2(100 BYTE),
"PST_ENTITY_LOGO_PLACEMENT" VARCHAR2(50 BYTE),
"PST_ENTITY_TEXT_COLOR" VARCHAR2(50 BYTE),
"PST_ENTITY_BG_COLOR" VARCHAR2(50 BYTE),
"PST_ENTITY_PROFILE" VARCHAR2(2048 BYTE),
"FIRST_CHAR" CHAR(1 BYTE),
"PST_ENTITY_STYLESHEET" VARCHAR2(512 BYTE),
"PST_ENTITY_CUSTOM_JSP" VARCHAR2(512 BYTE),
"PST_ENTITY_LOCATION_OPTION" NUMBER(*,0),
"PST_ENTITY_ECP_IMAGE" VARCHAR2(512 BYTE),
CONSTRAINT "PK_PST_ENTITY" PRIMARY KEY ("PST_ENTITY_ID")
USING INDEX PCTFREE 10 INITRANS 2 MAXTRANS 255 COMPUTE STATISTICS
STORAGE(INITIAL 196608 NEXT 1048576 MINEXTENTS 1 MAXEXTENTS 2147483645
PCTINCREASE 0 FREELISTS 1 FREELIST GROUPS 1 BUFFER_POOL DEFAULT)
TABLESPACE "PROVIDER" ENABLE,
CONSTRAINT "FK_PST_REGION_ID" FOREIGN KEY ("PST_ENTITY_REGION")
REFERENCES "D"."PST_REGION" ("PST_REGION_ID") ENABLE
) PCTFREE 10 PCTUSED 40 INITRANS 1 MAXTRANS 255 NOCOMPRESS LOGGING
STORAGE(INITIAL 4194304 NEXT 1048576 MINEXTENTS 1 MAXEXTENTS 2147483645
PCTINCREASE 0 FREELISTS 1 FREELIST GROUPS 1 BUFFER_POOL DEFAULT)
TABLESPACE "PROVIDER" ;
我已经创建了具有序列的模型,其中ENTITY_ID是从DB SEQ获得的:
@Entity
@Table(name="PST_ENTITY")
@Cache(region = "com.daoservice.service.model.EntityModel", usage = CacheConcurrencyStrategy.READ_WRITE)
public class EntityModel {
@Id
@SequenceGenerator(name="entityIdGen" , sequenceName = "PST_ENTITY_ID_SEQ", allocationSize = 1)
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "entityIdGen")
@Column(name="PST_ENTITY_ID", unique = true, nullable = false )
private Integer pstEntityId;
@Column(name="PST_ENTITY_GRP_NAME", nullable = false )
private String pstEntityGrpName;
@Column(name="PST_ENTITY_REGION", nullable = false )
private Integer pstEntityRegion;
...
和
@Entity
@Table(name="EMPDIR_DISPLAY")
@Cache(region = "com.companies.daoservice.service.model.EmployerDirModel", usage = CacheConcurrencyStrategy.READ_WRITE)
public class EmployerDirModel {
@Id
@GenericGenerator(name = "entityIdGen", strategy = "foreign", parameters = @Parameter(name = "property", value = "entityModel"))
@GeneratedValue(generator = "entityIdGen")
@Column(name="PST_ENTITY_ID", unique = true, nullable = false )
private Integer pstEntityId;
@Column(name="PST_GRP_ID", nullable = false )
private Integer pstGrpId;
...
现在,当我坚持这些模型时,EntityModel会被持久化甚至获取生成的seq。然而,由于EntityId存在一些问题,EmployerDirModel无疑会抛出一个NPE。
答案 0 :(得分:0)
如果要在两个类之间共享ID,请使用连接列。
@Entity
class MedicalHistory implements Serializable {
@Id @OneToOne
@JoinColumn(name = "person_id")
Person patient;
}
@Entity
public class Person implements Serializable {
@Id @GeneratedValue Integer id;
}
可选地,
@Entity
class MedicalHistory implements Serializable {
@Id Integer id;
@MapsId @OneToOne
@JoinColumn(name = "patient_id")
Person patient;
}
@Entity
class Person {
@Id @GeneratedValue Integer id;
}