开发者

Hibernate mapping an interface as entity with multiple implementations

I have the following structure i would like to map in hibernate.

class User implements iUser
class VIPUser implements iUser

class preference
   public var user:iUser;

I'开发者_开发百科ve read that it is possible to map a interface when you provide the target class. In my case however i dont know the target class since it can be a User or VIPUser.

The User and VIPUser are two separate entities which are stored in different tables.

Is this mapping possible in hibernate, and does anyone have an example of how to build it?

Thanks in advance


it's a bit late to the party but @Any/<any> mapping supports this

@Any(metaColumn = @Column(name = "USER_TYPE"))
@AnyMetaDef(idType = "long", metaType = "string", 
    metaValues = { 
         @MetaValue(targetEntity = User.class, value = "user"),
         @MetaValue(targetEntity = VIPUser.class, value = "vip"),
    })
@JoinColumn(name="USER_ID")
private IUser user;

or hbm

<any name="user" meta-type="string" id-type="long">
    <meta-value value="user" class="User"/>
    <meta-value value="vip" class="VIPUser"/>
    <column name="USER_TYPE"/>
    <column name="USER_ID"/>
</any>


Logically, I suppose that VIPUser would extend User. In such case, you can map the "root" entity (User) and tell Hibernate which inheritance strategy you want to use. In this case, I suppose it would be "Table per concrete class".

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜