I have used this solution for my issue.
All my entities have a commun superclass.
This superclass have one public abstract method that all subclass need to implement.
See:
Code:
@Transient
public Collection all;
public abstract Collection getAll();
All subclass implements getAll. This method add all dependent object from one entity to a collection.
When I persist Entity 1, re gets all related objects.
For example I have:
Entity 1 class:Code:
@OneToMany(cascade=CascadeType.ALL, mappedBy="entity1",fetch=FetchType.LAZY)
private List<Entity2> entities2;
@Override
public Collection getAll() {
if (all == null)
all = new ArrayList();
if (entities2 != null && entities2.size() > 0) {
for (Entity2 et2 : entities2) {
all.add(et2);
et2.getAll();
}
}
return all;
}
In Entity 2 class:Code:
@ManyToOne(cascade=CascadeType.ALL, fetch=FetchType.LAZY)
private Entity1 entity1;
@OneToMany(cascade=CascadeType.ALL, fetch=FetchType.LAZY)
private List<Entity3> entities3;
@Override
public Collection getAll() {
if (all == null)
all = new ArrayList();
if (entities3 != null && entities3.size() > 0) {
for (Entity3 et3 : entities3) {
all.add(et3);
et3.getAll();
}
}
return all;
}
And so on...
Thus I can handle all instances, all dependents objects. This is not the most clear and elegant solution, but this is the solution that I thought. I hope I was clear, cuz my english is not so good.
If someone else have another solution, please take a time and post here, will help others. Thanks all.