I'm trying to switch to hibernate from torque. One thing that Torque does is automatically create a superclass that you can add code to.
Specifically, if I have a DVD table in the database then when the java object is created from the xml schema it would create a BaseDVD class that contains all the code and then an empty DVD which derives from BaseDVD.
The upshot is that you can make changes to the xml schema and then run the code generator again and it'll overwrite the BaseDVD class, but it won't overwrite the DVD class. I can add my own methods to the DVD class while still being able to automatically regenerate the base class.
In hibernate I can create a BaseDvd.hbm.xml file and then generate a BaseDvd.java file from it. If I then created a new class that derived from BaseDvd then that would not really be helpful. I wouldn't be able to just do:
Query query = session.getNamedQuery("com.oreilly.hh.dvdsById");
query.setInteger("id", id);
Dvd found = (Dvd)query.uniqueResult();
I'd have to do something like:
Query query = session.getNamedQuery("com.oreilly.hh.dvdsById");
query.setInteger("id", id);
BaseDvd found = (BaseDvd)query.uniqueResult();
Dvd dvd = new Dvd(found);
which would just be silly because I'd have to write a ton of special code that copies the BaseDvd into my own Dvd. And then I'd lose all kinds of functionality that I want to keep.
I've looked at the subclass stuff, but it appears to only be used between other hibernate objects that represent actual tables.
Is there a way to do this in hibernate?
|