Using hibernate with generics

后端 未结 2 2033
天命终不由人
天命终不由人 2020-12-08 22:22

I am having some trouble understanding how Hibernate deals with generics and was wondering the best way to accomplish my goal.

Given a simple generic entity:

2条回答
  •  忘掉有多难
    2020-12-08 23:01

    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
    

提交回复
热议问题