Mapping with composite key over relationship

62 views Asked by At

I would like to know if it is possible to use JoinColumn giving a referenced column of a related object.

I have something simlar to:

public class A {

  @ManyToOne
  private C c;

  @Column(name = "KEY_THAT_B_WILL_USE_CONTAINED_IN_A")
  private String key_that_B_will_use; 

  @OneToMany(mappedBy = "a", fetch = FetchType.LAZY)
  private List<B> myBs = new ArrayList<>();
}

public class B {
  @ManyToOne(fetch = FetchType.LAZY)
  @JoinColumns({@JoinColumn(name = "KEY_THAT_B_WILL_USE_CONTAINED_IN_A",
            referencedColumnName = "KEY_THAT_B_WILL_USE_CONTAINED_IN_A"),
            @JoinColumn(name = "KEY_THAT_B_WILL_USE_CONTAINED_IN_C", referencedColumnName = "C.KEY_THAT_B_WILL_USE_CONTAINED_IN_C")
    })
}

public class C {
  @Column(name = "KEY_THAT_B_WILL_USE_CONTAINED_IN_C")
  private String key_that_B_will_use; 
}

Of course in class C, referencedColumnName = "C.KEY_THAT_B_WILL_USE_CONTAINED_IN_C" won't work.

But how do I create a reference which such a model that I can not change?

Thanks :-)

0

There are 0 answers