This is a similar problem to "Hibernate @OneToMany without a separate join table", in that I need a @OneToMany relationship without a join table. However, I would also like to not define the inverse relationship. Removing the inverse seems to result in a join table being automatically generated... is there a workaround for this?
3 Answers
In JPA 2.0+ you can use @JoinColumn as a way to avoid to generate joined table.
Try it.
@OneToMany
@JoinColumn(name="COLUMN_NAME")
UPDATE
The info provided above has been extracted from EJB 3.0 o'reilly book (Look for The @JoinColumn annotation references the CUSTOMER_ID column in the PHONE table). However, plain JPA 1.0 specification does not support this feature. What it says is
Unidirectional one-to-many relationships may be implemented using one-to-many foreign key mappings, however, such support is not required in this release. Applications that want to use a foreign key mapping strategy for one-to-many relationships should make these relationships bidirectional to ensure portability
So in 1.0 it is a vendor-specific implementation (And it makes sense, The author works at JBoss - The red hat divison behind the hibernate)
But it is supported by JPA 2.0 implementation
If the join is for a unidirectional OneToMany mapping using a foreign key mapping strategy, the foreign key is in the table of the target entity.

- 7,953
- 9
- 42
- 61

- 33,349
- 20
- 110
- 136
-
This is actually not allowed by the JPA 1.0 specification, this is Hibernate specific. – Pascal Thivent Aug 18 '10 at 23:25
-
1@Pascal Thivent I agree with JPA specification. It makes sense To create a joined table. It avoids issues like shown here (Yes, the favorite star has been marked by me): http://stackoverflow.com/questions/1275481/hibernate-onetomany-mapping-to-multiple-join-tables But the provided info has been colected in the EJB 3.0 o reilly Look for *The @JoinColumn annotation references the CUSTOMER_ID column in the PHONE table* As the author works for JBoss, it makes sense you see stuffs like that – Arthur Ronald Aug 19 '10 at 03:32
-
The good news is that all this is "fixed" in JPA 2.0. But you could maybe just clarify in your answer that this works with Hibernate (but is non standard JPA 1.0). Then I could upvote :) – Pascal Thivent Aug 19 '10 at 05:38
-
use `mappedBy` in Parent class that refers to the foreign key column. In child class just create a normal `@Column` for the foreign key column without using the `@ManyToOne/@JoinColumn` in the child class. This seem to be doing the same i,e works fine but doesnt create join table – firstpostcommenter Oct 13 '22 at 19:19
The JPA 1.0 specification does NOT support unidirectional OneToMany mapping without a Join Table.
And using a JoinColumn
on a OneToMany
isn't allowed in standard JPA 1.0 (only on a OneToOne
, ManyToOne
or ManyToMany
). It is in JPA 2.0 though.
From the JPA 1.0 specification:
2.1.8.5.1 Unidirectional OneToMany Relationships
The following mapping defaults apply:
Entity A is mapped to a table named
A
. Entity B is mapped to a table namedB
. There is a join table that is namedA_B
(owner name first). This join table has two foreign key columns. One foreign key column refers to tableA
and has the same type as the primary key of tableA
. The name of this foreign key column is formed as the concatenation of the following: the name of entity A; "_"; the name of the primary key column in tableA
. The other foreign key column refers to table B and has the same type as the primary key of tableB
and there is a unique key constraint on it. The name of this foreign key column is formed as the concatenation of the following: the name of the relationship property or field of entity A; "_"; the name of the primary key column in tableB
.
To sum up, if you don't want a Join Table (and full read/write support) and still want to be JPA compliant, make the association bidirectional (with an inverse
side).
The wiki book link below discusses a trick (mapping the target table as the join table) to "work around" the problem but this only works for reads, writes won't work.
References
- JPA 1.0 specification
- 2.1.8.2 Bidirectional ManyToOne / OneToMany Relationships
- 2.1.8.5.1 Unidirectional OneToMany Relationships
- 9.1.6 JoinColumn Annotation (discusses in which context this annotation can be used)
- JPA Wiki Book

- 562,542
- 136
- 1,062
- 1,124
-
@Arthur Hello Arthur! You have my vote too, your answer is just fine in the context of the OP. My answer is just for readers interested by what JPA 1.0 has to say about this. – Pascal Thivent Sep 22 '10 at 13:37
If there is no join table in database, then the relationship between two tables in database would be achieved by foreign key referring to primary key. If the relationship is through PK/FK, there has to be a property in the target class that refers back to the source so that the FK column is populated with a value. This property in the target class could be an id or a source object. If it is a source object then you need to have an inverse @ManyToOne in target class.

- 223
- 2
- 8