我有3张桌子(T_BONUS_TAB,T_COND,T_BONUS_COND_LINK)。 其中一个连接(T_BONUS_COND_LINK)另外两个表。
此表有3列:bonus_id,cond_id(对父表的引用)和另一列具有附加值(优先级)。
create table T_BONUS_COND_LINK
(
cond_id NUMBER(9) not null,
bonus_id NUMBER(9) not null,
priority NUMBER(1)
)
alter table T_BONUS_COND_LINK
add constraint T_BONUS_COND_B_FK foreign key (BONUS_ID)
references T_BONUS_TAB (ROW_ID);
alter table T_BONUS_COND_LINK
add constraint T_BONUS_COND_M_FK foreign key (COND_ID)
references T_COND (ROW_ID);
问题是如何获得优先级列的值?
@Entity(name = ENTITY_NAME)
@Table(name = "T_BONUS_TAB")
@Customizer(EclipseLinkIssuesFixer.class)
public class BonusApplication extends AbstractIdentifiableByLongEntity {
...
@OneToMany(fetch = EAGER)
@JoinTable(
name="T_BONUS_COND_LINK",
joinColumns = @JoinColumn( name="BONUS_ID"),
inverseJoinColumns = @JoinColumn( name="COND_ID")
)
private List<Condition> conds;
....
}
@Entity(name = ENTITY_NAME)
@Table(name = "T_COND")
@Customizer(EclipseLinkIssuesFixer.class)
public class CONDITION extends AbstractIdentifiableByLongEntity {
@Id
@Column(name = "ROW_ID", nullable = false, updatable = false)
private Long id;
@Column(name = "NAME", insertable = false, updatable = false)
private String name;
@Column(name = "DESCX", updatable = false)
private String desc;
}
答案 0 :(得分:0)
您必须将T_BONUS_COND_LINK表映射为具有复合主键的常规实体。
您必须创建一个ID类:
@Embeddable
public class BonusCondLinkId {
Long bonusId;
Long condId;
...
}
你可以像这样使用:
@Entity
@IdClass(BonusCondLinkId.class)
public class BonusCondLink {
@Id
private Long bonusId;
@Id
private Long condId;
...
}
或者作为EmbeddedId:
@Entity
@IdClass(BonusCondLinkId.class)
public class BonusCondLink {
@EmbeddedId
private BonusCondLinkId id;
...
}
答案 1 :(得分:0)
将采用这种方式:
@Embeddable
public class BonusCondLinkId implements Serializable {
private long bonusId;
private long condId;
}
@Entity
@Table(name="T_BONUS_COND_LINK")
@IdClass(BonusCondLinkId.class)
public class BonusCondLink implements Serializable {
@Id
@Column(name="bonus_id")
private @Getter @Setter long bonusId;
@Id
@Column(name="cond_id")
private @Getter @Setter long condId;
@Column(name="priority")
private @Getter @Setter Integer priority;
@ManyToOne
@PrimaryKeyJoinColumn(name="BONUS_ID", referencedColumnName="ROW_ID")
private @Getter @Setter Bonus bonus;
@ManyToOne
@PrimaryKeyJoinColumn(name="CON_ID", referencedColumnName="ROW_ID")
private @Getter @Setter Condition cond;
}
@Entity(name = ENTITY_NAME)
@Table(name = "T_BONUS")
@Customizer(EclipseLinkIssuesFixer.class)
public class Bonus extends AbstractIdentifiableByLongEntity {
...
@OneToMany(mappedBy="bonus")
@OrderBy("priority")
private @Getter @Setter List<BonusCondLink> conditions;
}
和类条件与@OneToMany(mappedBy =“cond”)类似于Bonus类。