Java hibernate / jpa how to create a dynamic shared object that itself is connected

I would like to create a dynamic and universal superclass with JPA / hibernate, which will be expanded for each hierarchical structured model, such as: role, page, directory, department, resolution, tree. I would like to create a dynamic tree with this object using java recursion and reflection

it should look like this:

enter image description here

This object must have a reference to its own entity.

I would like it to be completely abstract and not have a db table. Only extended objects must have db.

I tried to achieve this. But for so long. Here is my post about it

I am considering solutions:

  • @Inheritance (strategy = InheritanceType.TABLE_PER_CLASS)
  • @ Ani and @AnyMetaDef display
  • @MappedSuperclass
  • @Embeddable and @Embedded

I hope someone offers some suggestions.

+5
source share
1 answer

I came up with the following design. You can also check it out on GitHub :

@MappedSuperclass public abstract class GenericHierarchicalDictionary { public abstract GenericHierarchicalDictionary getParent(); public abstract Set<? extends GenericHierarchicalDictionary> getChildren(); } @Entity @Table(name = "LocalFolder") public class LocalFolder extends GenericHierarchicalDictionary { @Id @GeneratedValue(strategy=GenerationType.IDENTITY) private Long id; @ManyToOne private LocalFolder parent; @OneToMany(mappedBy = "parent") private Set<LocalFolder> children = new HashSet<LocalFolder>(); @Override public LocalFolder getParent() { return parent; } @Override public Set<LocalFolder> getChildren() { return children; } public void addChild(LocalFolder localFolder) { localFolder.parent = this; children.add(localFolder); } } @Entity @Table(name = "RemoteFolder") public class RemoteFolder extends GenericHierarchicalDictionary { @Id @GeneratedValue(strategy=GenerationType.IDENTITY) private Long id; @ManyToOne private RemoteFolder parent; @OneToMany(mappedBy = "parent") private Set<RemoteFolder> children = new HashSet<RemoteFolder>(); @Override public RemoteFolder getParent() { return parent; } @Override public Set<RemoteFolder> getChildren() { return children; } public void addChild(RemoteFolder localFolder) { localFolder.parent = this; children.add(localFolder); } } 

And this is the test:

 @Test public void testTree() { LOGGER.debug("testAddWebResource"); doInTransaction(new TransactionCallable<Void>() { @Override public Void execute(Session session) { LocalFolder rootLocalFolder = new LocalFolder(); session.persist(rootLocalFolder); LocalFolder localFolder1 = new LocalFolder(); rootLocalFolder.addChild(localFolder1); session.persist(localFolder1); LocalFolder localFolder11 = new LocalFolder(); localFolder1.addChild(localFolder11); session.persist(localFolder11); RemoteFolder rootRemoteFolder = new RemoteFolder(); session.persist(rootRemoteFolder); RemoteFolder remoteFolder1 = new RemoteFolder(); rootRemoteFolder.addChild(remoteFolder1); session.persist(remoteFolder1); RemoteFolder remoteFolder11 = new RemoteFolder(); remoteFolder1.addChild(remoteFolder11); session.persist(remoteFolder11); return null; } }); } 
+1
source

Source: https://habr.com/ru/post/1207583/


All Articles