Hibernate version: 3
Summary: How do I specify the table name for a hibernate persisted object at runtime? And how can I ensure instances of the same Class can reference different tables without interfering with each other?
----------------------------------------------
I'm trying to convert a partially-complete application to Hibernate, and I'm not having any luck finding info about how to have a class that can have its table specified at runtime.
The class in question implements hierarchical trees in the database. Each "named tree" has its own table, named "tree_{treename}". Each tree table has identical column names.
The code as it stands works like (edited for simplicity):
Tree tree = TreeManager.instance().getTreeRef("table-name");
Node root = tree.getRoot();
List<Node> toplevel = root.getChildren();
... etc ...
Each 'Node' has a reference to the 'Tree' object, which contains the name of the database table that was specified in the .getTreeRef() method call. The DAO uses that table name in all the SQL calls.
------------------------------
How would I go about converting this to Hibernate? I've googled, and can't find my way around these stumbling blocks:
(1) Having Hibernate use a dynamically chosen table.
(2) Not having hibernate explode when Nodes n1 and n2 have the same ID but different tables.
(3) Doing two-step commits (I'm using materialized paths for the tree functionality, so on creating a new node, I need to save it to get its ID value, then change its path to (parent + id) and save that field.)
(4) Those things I haven't thought of yet ;)
Any help is appreciated!
Cheers,
-Darren
|