coshikipix
coshikipix

Reputation: 59

JPA HIBERNATE - mapping a column twice (embeddedID and POJO)

I have to map a badly designed DB. And in the class "cote" there a composed PK with FK fields. So i made an embeddedable to avoid using derived entities with IdClass. So I tried this :

@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;

With the following 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;

As you can see i use the columns evaluation critere and seuil once in the embeddedId and once in the class Cote. And of course it doesn't work and i don't know what to try anymore. Is there a way to force JPA to link the two fields?

Upvotes: 1

Views: 1607

Answers (2)

samith kumarasingha
samith kumarasingha

Reputation: 294

Add this annotation to composite primary key

       @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>) ) } )

Upvotes: 0

coshikipix
coshikipix

Reputation: 59

My final solution : @Entity @Table(name="EVA_COTE") public class Cote implements 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;

Upvotes: 1

Related Questions