Using hibernate with generics
What you're looking for is probably Hibernate's implicit polymorphism. There's also a little-known "any" relationship which gives complete flexibility, but it has its tradeoffs. You can also use an "any" in a many-to-any.
Edit: I've created a runnable example on Github based around your "Box" class and using an @Any
mapping. You can browse it (or the Box class specifically) or check it out and run it with
git clone git://github.com/zzantozz/testbed tmp
cd tmp
mvn -q compile exec:java -Dexec.mainClass=rds.hibernate.AnyMapping -pl hibernate-any
I've already done that but with subclasses.
Your generic class must be abstract and subclasses must define the generic parameter