Search code examples
javadatabasehibernatejpahibernate-mapping

Composite key and many to one


I need a field to be part of the composite key, but I also need a Many to one relationship with the corresponding object. Is something like this possible ?

@Entity
public class Instrument {

    @EmbeddedId
    private InstrumentPk instrumentPk;

    @ManyToOne;
    private Transaction transaction;

}

@Embeddable
public class InstrumentPk {

    private Integer productId;

    private Integer transId;

}

Solution

  • In your case @ManyToOne relationship has nothing to do with @Embeddable:

    @ManyToOne;
    private Transaction transaction;
    

    This is goig to work just fine provided there is a Transaction entity and mapping is well defined.