Wednesday, December 7, 2011

How to Dynamically Add Entities to a Persistence Unit

With a simple Spring extension it's possible to crack open a PersistenceUnit and dynamically add additional entity classes. It's also possible to merge multiple PersistenceUnits, but that isn't shown here. And the wiring:

How to Disable Hibernate Cascading

If domain objects are used in multiple contexts, it's possible that cascading is not always desirable. Unfortunately, it's not possible to persistNoCascade(entity). The following is a crude hack that will disable cascading entirely. It is not toggle-able, however. That would be nice, but it's a tricky thing to pull off because SessionFactoryImpl, SessionImpl, and AbstractEntityPersister can't be proxied. And the following is how to wire it: