Hey. I would like to create abstract and @
MappedSuperclass Entity. Lets call it a "
GenericHierarchicalDictionary".
This entity will be a
skeleton for all my
hierarchical data like:
folders, cms pages, roles, etc.
I would like to have in GenericHierarchicalDictionary entity parameters:
Code:
id - primary key,
name - the name of element,
parent_id - foreign key pointing to Entity GenericHierarchicalDictionary (pointing to self)
and methods:
Code:
getParents(Long id) {}
getChildren(Long parent_id) {}
printTreeStructuredData(Long parent_id) {}
generateJsonTreeStructuredObject(Long parent_id) {}
I would like to have that code to not be repeated in extending classes, like: localFolder entity.
I would like to know is it even possible to do such complex generic model?
I've even make a post on stackoverflow. But nothing helpfull so far.
Here are links:
https://stackoverflow.com/questions/27104464/java-hibernate-jpa-how-to-create-dynamic-generic-entity-that-is-self-related/27168120#27168120
https://stackoverflow.com/questions/27101716/apply-generic-class-mappedsuperclass-as-targetentity-error-manytoone-on-model
I have some code already:
Code:
@MappedSuperclass
public abstract class GenericHierarchicalDictionary {
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
private Long id;
@ManyToOne
private GenericHierarchicalDictionary parent;
@OneToMany(mappedBy = "parent")
private Set<GenericHierarchicalDictionary> children = new HashSet<GenericHierarchicalDictionary>();
public GenericHierarchicalDictionary getParent() {
return parent;
}
public Set<GenericHierarchicalDictionary> getChildren() {
return children;
}
public void addChild(GenericHierarchicalDictionary localFolder) {
localFolder.parent = this;
children.add(localFolder);
}
}
But I am getting an error:
Quote:
Caused by: org.hibernate.AnnotationException: @OneToOne or @ManyToOne on models.LocalFolder.parent references an unknown entity: models.GenericHierarchicalDictionary
I understand if it is not decorated with @Entity it isn't an entity. It is mapped as @MappedSuperclass cause it is only a skeleton for concrete entity. I have to somehow put dynamically (maybe using reflection) the name of current class that extend this Skeleton super class(GenericHierarchicalDictionary)
Maybe something like that:
Code:
@MappedSuperclass
public abstract class GenericHierarchicalDictionary<T extends GenericHierarchicalDictionary> {
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
private Long id;
@ManyToOne
private GenericHierarchicalDictionary<T> parent;
}
Thanks in advance