重写基本类型映射的映射

发布于 2024-12-29 10:48:04 字数 677 浏览 2 评论 0原文

我正在为我的实习开发 JPA 2.0 合规性套件...该套件的一部分是测试极端情况。

JSR-317 在第 360 页指出“AttributeOverride 注释可以应用于包含可嵌入类实例的元素集合,或者应用于其键和/或值是可嵌入类的映射集合。 .”

那么,根据 JPA 2.0,我如何覆盖基本类型映射的映射呢?我知道我可以使用 @MapKeyColumn 来映射映射的键,并且我确信也有某种方法可以映射 @CollectionTable 的值侧...

但是我将如何覆盖这些呢?

考虑带有地图的 @Embeddable

@CollectionTable
@MapKeyColumn(name="differentname_KEY")
Map<Integer, String> testMap;

我将如何覆盖键和值?我使用@AttributeOverride 还是其他什么? (或者这是不可能的?!)

我在这里假设这样的地图将使用 @CollectionTable 进行映射,所以如果我错了,请纠正我。 如果 JPA 没有给出答案,我有兴趣了解持久性提供者如何解决这个问题。

编辑: Viruzzo 评论说基本类型是可嵌入类型。 我愿意接受这一点,但有些东西阻碍了我: JSR-317 指的是可嵌入的(参见上面的引用)。类型和类别不一样...

I'm working on a JPA 2.0 compliancy kit for my internship... Part of that kit is testing corner cases.

JSR-317 states on Page 360 that "The AttributeOverride annotation may be applied to an element collection containing instances of an embeddable class or to a map collection whose key and/or value is an embeddable class."

How then, do I, according to JPA 2.0, override the mapping of a map of basic types? I know I can use @MapKeyColumn to map the key of the map, and I'm sure there is some way to map the value side of the @CollectionTable as well...

But how would I go about overriding these?

Consider an @Embeddable with a map

@CollectionTable
@MapKeyColumn(name="differentname_KEY")
Map<Integer, String> testMap;

How would I go about overriding the key and the value? Do I use @AttributeOverride, or something else? (Or is it impossible?!)

I'm assuming here that such a map would be mapped with a @CollectionTable, so please correct me if I'm wrong.
If JPA doesn't give an answer, I'd be interested in knowing how persistence providers have solved this problem.

EDIT:
Viruzzo commented that basic types are embeddable types.
I'm willing to accept that, but something is holding me back:
JSR-317 is referring to an embeddable class (see upper quote). Type and class are not the same...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

幽蝶幻影 2025-01-05 10:48:04

首先作为旁注:您的示例中的 Map 甚至不应该编译。原因是int是原始类型,java.util.Collection集合和map接口和实现仅适用于引用类型。

让我们使用以下示例:

SomeEntity {
  @Id private int id;
  @ElementCollection
  private Map<Integer, String> testmap; 
}

默认情况下,testMap 映射到表 SomeEntity_TESTMAP(SOMEE​​NTITY_ID, TESTMAP, TESTMAPKEY)。我们有默认的表名和三个默认的
列名称。可以覆盖所有这些。以下将映射到表 testmap_table(join_column, value_column, key_column):

@ElementCollection
@CollectionTable(name = "testmap_table", 
                 joinColumns = @JoinColumn(name = "join_column"))
@MapKeyColumn(name = "key_column")
@Column(name= "value_column")
private Map<Integer, String> testMap;

@AttributeOverride 在这里没有用,因为没有可嵌入的键或值。它用于覆盖从其他地方派生的映射,而不是用于覆盖 ElementCollection 的默认值。它确实有两种用途:

  • 覆盖从映射超类派生的属性映射或
    从嵌入类派生
  • 覆盖从可嵌入类派生的属性映射
    用作元素集合中的键或值。

First as a side note: Map in your example should not even compile. Reason is that int is primitive type, java.util.Collection collection and map interfaces and implementations are only for references types.

Lets use instead following example:

SomeEntity {
  @Id private int id;
  @ElementCollection
  private Map<Integer, String> testmap; 
}

By default testMap is mapped to the table SomeEntity_TESTMAP(SOMEENTITY_ID, TESTMAP, TESTMAPKEY). We have defaulted table name and three defaulted
column names. It is possible to override all of these. Following will map to the table testmap_table(join_column, value_column, key_column):

@ElementCollection
@CollectionTable(name = "testmap_table", 
                 joinColumns = @JoinColumn(name = "join_column"))
@MapKeyColumn(name = "key_column")
@Column(name= "value_column")
private Map<Integer, String> testMap;

@AttributeOverride have no use here because no key or value is embeddable. It is for overriding mappings derived from elsewhere, not for overriding defaults of ElementCollection. It does have two usages:

  • overriding attribute mappings derived from mapped superclass or
    derived from the embedded class
  • overriding attribute mappings derived from the embeddable class that
    is used as key or value in element collection.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文