Marco Schwarz wrote:
@MappedSuperclass
@Inheritance(strategy=InheritanceType.TABLE_PER_CLASS)
class ModelObject {
    @Transient
    ModelObject parent;

    @OneToMany(cascade={CascadeType.ALL}, fetch=FetchType.EAGER)
    List<ModelObject> children;
}

When I start my app the openjpa create 4 tables (Company, Company_children, Emplpyer and Employer_children).

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.

Next, you want to make sure you're using "single table" mapping to encode inheritance relations. This way all classes derived from ModelObject should utilize the same table. Although single table is supposed to be the default inheritance strategy, it might not hurt to explicitly state it:

@Entity
@Inheritance(strategy=InheritanceType.SINGLE_TABLE)
class ModelObject {
  ...

  @ManyToOne
  ModelObject parent;

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

  ...
}

Reply via email to