简体   繁体   中英

@Embeddable is Foo.class' PK and Bar.class' FK to Foo.class

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM