Using hibernate with generics

后端 未结 2 2025
天命终不由人
天命终不由人 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
    
    0 讨论(0)
  • 2020-12-08 23:05

    I've already done that but with subclasses.

    Your generic class must be abstract and subclasses must define the generic parameter

    0 讨论(0)
提交回复
热议问题