在很多情况下,我必须维护/改进旧系统。 例子
create table COMPANY(
COMP_ID_COMPANY NUMBER(10),
COMP_NAME VARCHAR(32),
/*... MANY FIELDS*/
PRIMARY KEY (COMP_ID_COMPANY)
);
create table BUSINESS_UNIT(
BUZZ_ID_COMPANY NUMBER(10),
BUZZ_ID_BUZZUNIT NUMBER(10),
/*... MANY FIELDS*/
PRIMARY KEY (BUZZ_ID_COMPANY, BUZZ_ID_BUZZUNIT),
CONSTRAINT FK_BUZZ_COMP FOREIGN KEY (BUZZ_ID_COMPANY) REFERENCES COMPANY (COMP_ID_COMPANY)
)
create table BRANCH(
BRAN_ID_COMPANY NUMBER(10),
BRAN_ID_BRANCH NUMBER(10),
BRAN_NAME VARCHAR(32),
/*... MANY FIELDS*/
BRAN_ID_BUZZUNIT NUMBER(10) nullable, /*HERE IS THE PROBLEM*/
PRIMARY KEY (BRAN_ID_COMPANY, BRAN_ID_BRANCH),
CONSTRAINT FK_BRAN_BUZZ FOREIGN KEY (BRAN_ID_COMPANY, BRAN_ID_BUZZUNIT) REFERENCES BUSINESS_UNIT (BUZZ_ID_COMPANY, BUZZ_ID_BUZZUNIT) /*HERE IS THE PROBLEM*/
);
前两个单元的映射很简单,我必须为COMPANY PK创建一个类,并使用@EmbeddedId和@Embeddable进行注释。 但是,在Branch类上,我有这个问题
@Embeddable
class BranchPk{
@Column(name="BRAN_ID_COMPANY")
private Long branIdCompany;
@Column(name="BRAN_ID_BRANCH")
private Long branIdBranch;
}
@Entity
@Table(name="BRANCH"
class Branch {
@EmbeddedId
private BranchPk pk;
@ManyToOne
@JoinColumns({
@JoinColumn(name="BRAN_ID_COMPANY", insertable=false, updatable=false), //I cannot keep this field insertable or updatable because makes part of the primary key
@JoinColumn(name="BRAN_ID_BUZZUNIT"), //JPA doesn't allow me to mix the insertable/updatable fields with noninsertable/nonupdatable ones
})
private BusinessUnit businessUnit;
}
问题是:如何映射允许businessUnit属性为null且不从PK删除branIdCompany的此类? 我希望仅使用JPA来解决此问题,但是使用休眠特定性对我来说不是问题。