0

I am trying to define JPA layer over a db I cannot redesign. That layer contains a OneToMany relationship where the Many part has a foreign key consisting of the same 2 fields that the one part has as primary key.

@Embeddable
public class FooKey implements Serializable {

    @Column(name="foo_id")
    private String id;
    private String secondaryId;
}

public class Foo {
   @EmbeddedId
   private FooKey id;
   (...)
}

public class Bar {
    @Id
    private Long id;
    (...)

    //@Embedded FooKey fooKey;
    @ManyToOne
    private Foo foo;
   }

How can I solve this? I am getting the error @Column(s) not allowed on a @ManyToOne property

Whimusical
  • 5,757
  • 8
  • 51
  • 91

1 Answers1

0

I managed to do it by mean of:

  @ManyToOne
  @JoinColumns({
            @JoinColumn (name = "foo_id", referencedColumnName = "foo_id"),
            @JoinColumn (name = "secondary_id", referencedColumnName = "secondary_id")
  })
  private Foo foo;

Although if anyone has a more elegant solution, I will accept it

Whimusical
  • 5,757
  • 8
  • 51
  • 91