-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 
Author Message
 Post subject: Generic Self related Entity
PostPosted: Thu Nov 27, 2014 8:24 am 
Newbie

Joined: Thu Nov 27, 2014 8:10 am
Posts: 1
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


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.