0

I am using PlayFramework 2.4 to connect to DB by EBean 4. I face a problem which is I would like to join 2 table with non primary key as follow and using referencedColumnName to map ColA with ColB.

Table_1                   Table_2
----------                ----------
Id_T1(PK)                 Id_T2(PK)
ColA------(OneToMany)-----ColB
...                       ...

Table_1.java
@OneToMany(mappedBy="table1")
private List<Table_2> table2s;

Table_2.java
@ManyToOne
@JoinColumn(name="ColA", referencedColumnName="ColB")  
private Table_1 table1;

But it return the following error:

Error injecting constructor, javax.persistence.PersistenceException: Error with the Join on [models.Table_1.table2s]. Could not find the matching foreign key for [itemUID] in table[Table_2]? Perhaps using a @JoinColumn with the name/referencedColumnName attributes swapped?

I find that EBean4 doc did not talk about the referencedColumnName JPA notation https://ebean-orm.github.io/docs#relationships

And in EBean2, it seems it report it as a bug and marked fixed already http://www.avaje.org/bugdetail-263.html

So I would like to ask is my code has problem? Or EBean did not support join by the Non-PK at all?

Thanks!

Neil Stockton
  • 11,383
  • 3
  • 34
  • 29
  • I don't recommend joining on non primary key. It takes much longer for query to execute. If you can, make joining column a primary key itself. – Mon Calamari Jun 12 '15 at 09:13
  • It is because Table_1 is main table which get the information from other tables and Table_2 ColB will be duplicate record so I can't use primary key for Table_2. Of cause, I will add index in Table_2 for performance issue. – strikerlie Jun 12 '15 at 09:20
  • http://stackoverflow.com/questions/13885916/one-to-many-association-join-tables-with-non-primary-key-column-in-jpa – Mon Calamari Jun 12 '15 at 09:25
  • http://stackoverflow.com/questions/5818373/does-the-jpa-specification-allow-references-to-non-primary-key-columns – Mon Calamari Jun 12 '15 at 09:26
  • http://stackoverflow.com/questions/11386610/jpa-joincolumn-issues-while-joining-on-non-primary-key-columns – Mon Calamari Jun 12 '15 at 09:26
  • Is it means that it support by Hibernate but not in EBean? – strikerlie Jun 12 '15 at 09:44
  • Ebean partially is a JPA implementation. JPA annotation does not support join with no foreign/primary key, thus I would not expect it from Ebean. – Mon Calamari Jun 12 '15 at 09:49
  • Then how about if i use a bridge table which using ColA/ColB value as PK? I follow this line http://stackoverflow.com/questions/19850491/how-do-i-describe-a-bridge-table-to-ebean but it still show can map the PK it show :Could not find the matching foreign key for [Id_T2] in table[Table_3]? Perhaps using a @JoinColumn with the name/referencedColumnName attributes swapped? – strikerlie Jun 14 '15 at 10:13

0 Answers0