makeItEasier
makeItEasier

Reputation: 216

Problems with Relationship Composite Primary Key on Hibernate

I have this diagram enter image description here

How can I create the relationship on Hibernate of the entities EscalaDetalhes and DefinicaoEscala, because on DefiniciaoEscala entity there is a composite primary key formed by idDetalhe (foreign with (EscalaDetalhes)) and idTurma(foreign with (Turma)).

I've already done the class like below :

@Embeddable
public class DefinicaoEscalaPK implements Serializable {

    @Column(name="myColumn")
    private Integer idTurma;
    @Column(name="myColumn2")
    private Integer idEscalaDia;

    //GETTERS , SETTERS , HASH AND EQUALS
}

@Entity
public class DefinicaoEscala implements Serializable {

    @EmbeddedId
    private DefinicaoEscalaPK id;

    @ManyToOne()
    @JoinColumn(name = "idTurno")
    private TurnoBean turno;

    //GETTERS , SETTERS , HASH AND EQUALS
}


@Entity
@Table(name ="table")
public class EscalaDetalhes{
    @id
    private Integer idDetalhe;
    @ManyToOne()
    @JoinColumn(name="mybdColumn")
    private EscalaBean escala;
    @Column(name="myColumn") 
    private Calendar dia;
    //MY QUESTION IS HERE , HOW WOULD I DESCRIBE THE RELATION HERE?  
    private List<DefinicaoEscala> escalaDiaDetalhes;

    //GETTERS , SETTERS , HASH AND EQUALS

}

My question is here, How can I describe the relation?

private List<DefinicaoEscala> escalaDiaDetalhes;

Upvotes: 0

Views: 566

Answers (1)

codeLover
codeLover

Reputation: 2592

referencedColumnName is basically used to convey that which column of the other entity will be mapped could be used for mapping in the current entity, thus, you can use the property referencedColumnName while mentioning the relationship:

@Entity
@Table(name ="table")
public class EscalaDetalhes{
    @id
    private Integer idDetalhe;
    @ManyToOne()
    @JoinColumn(name="mybdColumn")
    private EscalaBean escala;
    @Column(name="myColumn") 
    private Calendar dia;
    //MY QUESTION IS HERE , HOW WOULD I DESCRIBE THE RELATION HERE? 
    @OneToMany
    @JoinColumn(name="idDetalhe",referencedColumnName="idEscalaDia")
    private List<DefinicaoEscala> escalaDiaDetalhes;

    //GETTERS , SETTERS , HASH AND EQUALS

}

P.S: It is entirely on the basis of code you have shared.

Upvotes: 1

Related Questions