Thanks, so it works fine, but another question:
Why I must say @Entity on the class ModelObject? So I have a table
ModelObject. With @MappedSuperclass I don't have the table ModelObject
but the framework throw this error:
"net.cioppino.fita.core.model.ModelObject.children" declares a to-many
relation strategy, but its elements are not direct, non-embedded
relations to another mapped persistence-capable object.
Thanks
Marco
Andy Schlaikjer schrieb:
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;
...
}