2 Hibernate entities on the same database table. Is this possible?
I am working on a modular Webproject with default functionality that can be extended using a "plugin" which is an additional java library.
The "core" project knows a User entity that is mapped to the User table. The plugin project extends this User as an ExtendedUser which is also mapped to the same table-
Both entities should co-exists, where User only knows some of the columns.
Both entities are declared in different libraries included on the same classapth.
How do you get this to work w开发者_C百科ith hibernate?
Thanks!
Assuming you mean that ExtendedUser
is a class that extends User
, then you can use Hibernate's "table-per-class-hierarchy" inheritance strategy. See the docs here. Using annotations, you use InheritanceType.SINGLE_TABLE
(see docs 2.2.4.2 here).
I've tried once to map a table to two Java entities (without an inheritance scheme). I just wanted to experiment some different mappings while preserving the old entity.
The thing is if your User and ExtenderUser both share the same collection of entities X, Hibernate will complain about not knowing which entity X is connected back to: User or ExtenderUser.
So bottom line, in mine case it didn't work, and it's right not to.
I haven't tried an inheritance scheme, but from what you're saying, you don't one either.
精彩评论