重写基本类型映射的映射
我正在为我的实习开发 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
首先作为旁注:您的示例中的 Map 甚至不应该编译。原因是int是原始类型,java.util.Collection集合和map接口和实现仅适用于引用类型。
让我们使用以下示例:
默认情况下,testMap 映射到表
SomeEntity_TESTMAP(SOMEENTITY_ID, TESTMAP, TESTMAPKEY)
。我们有默认的表名和三个默认的列名称。可以覆盖所有这些。以下将映射到表 testmap_table(join_column, value_column, key_column):
@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:
By default testMap is mapped to the table
SomeEntity_TESTMAP(SOMEENTITY_ID, TESTMAP, TESTMAPKEY)
. We have defaulted table name and three defaultedcolumn names. It is possible to override all of these. Following will map to the table testmap_table(join_column, value_column, key_column):
@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:
derived from the embedded class
is used as key or value in element collection.