在JPA中,这是可嵌入值的映射,这些值具有嵌入式实体作为键 [英] In JPA, a Map of embeddable values, that have an embedded entity used as the key

查看:159
本文介绍了在JPA中,这是可嵌入值的映射,这些值具有嵌入式实体作为键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我仍然对JPA(以及Hibernate,我正在使用它作为提供程序)不熟悉,所以也许这无法完成,但是无论如何...

I'm still new to JPA (and Hibernate, which I'm using as my provider), so maybe this just can't be done, but anyway...

考虑以下代码:

@Entity
class Root {
    @Id
    private long id;
    private String name;

    @ElementCollection
    private Map<ResourceType, Resource> resources;
    ...
}

@Entity
class ResourceType {
    @Id
    private long id;

    private String name;
}

@Embeddable
class Resource {
    private ResourceType resourceType;
    private long value;
}

在数据库中,有一个收集表'Root_resources',用于存储地图的值,但是资源类型出现两次(实际上,资源类型ID出现)两次,一次作为地图的KEY,并且一次作为价值的一部分.

In the database, there is a collection table, 'Root_resources', that stores the values of the map, but the resource type appears twice (actually, the resource type ID does), once as the KEY of the map, and once as part of the value.

是否有一种类似于@MapKey注释的方式来指示键是值的列之一(即嵌入式)?

Is there a way, similar to, say, the @MapKey annotation, to indicate that the key is one of the columns of the value (i.e. embedded)?

推荐答案

我认为您要的是这样的东西:

I think what you're asking for is something like:

@MapKeyJoinColumn("RESOURCE_TYPE_ID")

在ElementCollection上.

on the ElementCollection.

然后还为可嵌入对象上的资源类型的FK映射指定列名.

Then also specify the column name for the FK mapping of the resource type on your embeddable.

这篇关于在JPA中,这是可嵌入值的映射,这些值具有嵌入式实体作为键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆