我必须映射设计糟糕的数据库。并且在班级" cote"有一个组成PK与FK字段。所以我做了一个可嵌入的,以避免使用带有IdClass的派生实体。所以我尝试了这个:
@Entity
@Table(name="EVA_COTE")
public class Cote implements Serializable{
/**
*
*/
private static final long serialVersionUID = -3902534586060936635L;
@EmbeddedId
private CotePK cotePK;
@ManyToOne
@JoinColumn(name="Evaluation", referencedColumnName = "NUDOSS")
private Evaluation evaluation;
@ManyToOne
@JoinColumn(name="CRITERE", referencedColumnName = "CODE")
private Critere critere;
@ManyToOne
@JoinColumn(name="SEUIL", referencedColumnName = "CODE")
private Seuil seuil;
@Column(name="VALEUR", columnDefinition="Decimal(5,0)")
private Integer valeur;
@Column(name="OBSERVATIONS", length=500)
private String observations;
使用以下EmbeddedId:
@Embeddable
public class CotePK implements Serializable{
/**
*
*/
private static final long serialVersionUID = 6835265195560184935L;
@Column(name="Evaluation", nullable=false)
private Integer evaluationId;
@Column(name="CRITERE", nullable =false)
private String critereId;
@Column(name="SEUIL", nullable=false)
private String seuilId;
正如您所看到的,我在embeddedId中使用了列评估critere和seuil,在Cote类中使用了一次。当然,它不起作用,我不知道该怎么做。有没有办法强制JPA链接这两个字段?
答案 0 :(得分:1)
我的最终解决方案: @实体 @table(名称=" EVA_COTE&#34) 公共类Cote实现Serializable {
/**
*
*/
private static final long serialVersionUID = -3902534586060936635L;
@EmbeddedId
private CotePK cotePK;
@ManyToOne
@MapsId("evaluationId")
@JoinColumn(name="EVALUATION", referencedColumnName = "NUDOSS")
private Evaluation evaluation;
@ManyToOne
@MapsId("critereId")
@JoinColumn(name="CRITERE", referencedColumnName = "CODE")
private Critere critere;
@ManyToOne
@MapsId("seuilId")
@JoinColumn(name="SEUIL", referencedColumnName = "CODE")
private Seuil seuil;
答案 1 :(得分:0)
将此注释添加到复合主键
@EmbeddedId
@AttributeOverrides( {
@AttributeOverride(name="column_varible_name_1", column=@Column(name="column_name_1", nullable=false, length=<x>) ),
@AttributeOverride(name="column_varible_name_2", column=@Column(name="column_name_2", nullable=false, length=<x>) ) } )