83

I have a following problem with entity mapping in JPA. I have two entities, first one is Lookup and the second is Text which represents translations for entities. Now I need to bound Lookup to the Text but I don't want Text to have reference to Lookup. To make this more complicated, Text does not use its primary key in this relationship but a metacode defined in a TXTHEAD_CODE column.

Lookup.java

@Entity
@Table(name = "DATREG")
public class Lookup implements PersistableEntity {

    @Id
    @Column(name = "DATREG_META_CODE")
    private String metaCode;

    @OneToMany
    @JoinTable(name="TXT", 
            joinColumns=@JoinColumn(name="DATREG_META_CODE", referencedColumnName="TXTHEAD_CODE"),
            inverseJoinColumns=@JoinColumn(name="DATREG_META_CODE"))
    private List<Text> text;

Text.java

@Entity
@Table(name = "TXT")
public class Text {

    @Id
    @Column(name = "TXT_ID")
    private Long id;

    @Column(name = "TXTHEAD_CODE")
    private String code;

So I have tried this (and few other variations) but with no result. I also can't create join table in the DB and I don't want bound Lookup to my Text class. So can anyone please tell me if there is some other way?

Petr Mensik
  • 26,874
  • 17
  • 90
  • 115

1 Answers1

170

My bible for JPA work is the Java Persistence wikibook. It has a section on unidirectional OneToMany which explains how to do this with a @JoinColumn annotation. In your case, i think you would want:

@OneToMany
@JoinColumn(name="TXTHEAD_CODE")
private Set<Text> text;

I've used a Set rather than a List, because the data itself is not ordered.

The above is using a defaulted referencedColumnName, unlike the example in the wikibook. If that doesn't work, try an explicit one:

@OneToMany
@JoinColumn(name="TXTHEAD_CODE", referencedColumnName="DATREG_META_CODE")
private Set<Text> text;
Tom Anderson
  • 46,189
  • 17
  • 92
  • 133
  • Wow, it really worked, thank you. I thought that I always need to create a join table if there is only one side of the relationship (the other side does not hold the foreign keys). – Petr Mensik Aug 21 '12 at 06:14
  • 5
    Bingo! This "Java Persistence Wikibook" link is what i have been looking for... Thanks for sharing... – Ahmet Jan 27 '18 at 10:31
  • I'm trying the same but, the foreign key is not getting set in the child entity in the DB, I'm getting this exception: `oracle.jdbc.OracleDatabaseException: ORA-01400: cannot insert NULL into` – Sandeep Kumar Apr 04 '20 at 04:53
  • @SandeepKumar What do you mean by "child entity"? The equivalent of `Text` in this example? I suspect that JPA does not consider the referenced column to be a foreign key, in which case it won't populate it with the ID of the parent; i'm afraid you will have to set it yourself before saving. – Tom Anderson Apr 06 '20 at 13:58
  • 1
    I fixed the issue by removing the property from my child class. Now, JPA is implicitly considering the key mentioned in the parent class and inserting the value. Thanks! – Sandeep Kumar Apr 06 '20 at 15:49