Hi Uden,

If you'd rather keep the relation unidirectional just remove the `children` field-- the `parent` field alone is enough to encode the relation without recourse to a separate join table. Either way, I don't see how the quoted section below results in a join table. Both fields rely solely on a "parent_id" column within the "ModelObject" table, not on some other join table. Am I missing something?

Andy

Uden wrote:
The solution quoted below but has a consequence for the Java class model: the
OneToMany relationship becomes bi-directional instead of uni-directional. What is the reason for creating the join-table?
I thought (based on my database experience) that a join-table is only
required for ManyToMany relationships. If you look at the data in the join-table of a uni-directional relation (no
mappedBy attribute), the relation between the join-table and master table is
always OneToOne, so this relation could be handled by a FK-field in the
detail-table.

thanks for your explanation,
Uden


Andy Schlaikjer-2 wrote:
Marco Schwarz wrote:
How can I make only 2 tables?
Here's my guess:

First, use the "mappedBy" property of the @OneToMany annotation, like
this:

@Entity
class ModelObject {
   ...

   @ManyToOne
   ModelObject parent;

   @OneToMany(mappedBy="parent")
   List<ModelObject> children;

   ...
}

This way, an extra join table won't be necessary to encode the parent child relationship. Only the "parent_id" column in the "ModelObject" table will be used to encode the relationship.


Reply via email to