I'm currently writing a simulation context for objects that are of mixed origin. Some of these objects are persistent hibernate entities that are loaded from the database. Others are new 'simulated' objects that are created transient and might be stored in the database later.
The present application processes directed graphs, that are loaded from the database with Hibernate Entities. Currently it simply checks for cyclic sub-structures. The mapping is arranged such, that it lazily loads Nodes and Edges from the database. This allows my cycle checker to walk the entire graph by lazy fetching adjacent Nodes according to the information stored in the directly related Edges.
Node {
Integer id; // mapped id
List<Edge> edges; // mapped and fetched lazy
}
Edge {
Integer id; // mapped id
Integer predecessorId; // mapped
Integer successorId; // mapped
Node predecessor; // mapped and fetched lazy
Node successor; // mapped and fetched lazy
}
Now I want to add an Edge to the graph, without storing it in the database. Anyhow, the CycleChecker should run first, but take the edge into account. I wonder if I can reuse my existing CycleChecker and solve that problem purely with Hibernate entities, without having to write additional code for "simulated" entities.
Two questions:
If I create a new
Edgeand persist it in the HibernateSession(say scoped by a transaction context), will I be able to lazy load itsNodesuccessor from database before commit()?If I create a new
Nodeand persist it in the session (no commit or flush happens), will Hibernate find it viaget(Id)or lazy loading from another freshEdge?
And if all that is not possible, is there a Hibernate way to hook into the fetch in order to return objects from my simulation context?