简体   繁体   English

具有嵌套属性的 JPA MapKey

[英]JPA MapKey with Nested Attributes

I have 3 elements like this:我有 3 个这样的元素:

public class ItemType {
  @Id
  private Long id = null;
  ...
  @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, orphanRemoval = true, mappedBy = "itemTypeVO")
  @MapKey(name = "company.id")
  private Map<Long, ItemTypePurpose> purposeHash = null;
  ...
}


public class ItemTypePurpose {
  @Id
  private Long id = null;
  ...
  @ManyToOne(fetch = FetchType.LAZY, optional = false)
  @JoinColumn(name = "idcompany")
  private Company company = null;
  ...
}

public class Company {
  @Id
  private Long id = null;
  ...
}

My problem is, I want the ID of Company as key of my map inside ItemType .我的问题是,我希望 Company 的 ID 作为 ItemType 中地图的键。

I can compile and deploy the application without any errors.我可以编译和部署应用程序而不会出现任何错误。 Can persist ItemType, and everything goes well to DB.可以持久化ItemType,DB一切顺利。 But when I get it back, the Map key is "wrong", I don't know what information is being used, but for sure it's not the Company id.但是当我取回它时,Map 键是“错误的”,我不知道正在使用什么信息,但肯定不是公司 ID。 Perhaps the ItemTypePurpose's ID.也许是 ItemTypePurpose 的 ID。

The Company is being loaded into Map correctly, just the map key is wrong.公司正在正确加载到地图中,只是地图键错误。 I've tryied to google, bu can't find anything.我试过谷歌,但找不到任何东西。 Does any way to JPA create my map with this "nested attribute"? JPA 有没有办法用这个“嵌套属性”创建我的地图?

*Sorry about my english, feel free if you understand what I need and can do a better writing in english to edit my question. *抱歉我的英语,如果您了解我的需要并且可以用英语写出更好的文字来编辑我的问题,请随意。

This doesn't exactly solves the question, but solve my needs for now.这并不能完全解决问题,但暂时解决了我的需求。

Since que ID of Company was in table of ItemTypePurpose, I could change the MapKey to:由于公司的 que ID 在 ItemTypePurpose 表中,我可以将 MapKey 更改为:

public class ItemType {
  @Id
  private Long id = null;
  ...
  @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, orphanRemoval = true, mappedBy = "itemTypeVO")
  @MapKeyColumn(name = "idcompany", insertable = false, updatable = false)
  private Map<Long, ItemTypePurpose> purposeHash = null;
  ...
}

Instead of @MapKey, I used @MapKeyColumn.我使用了@MapKeyColumn 而不是@MapKey。 The @MapKeyColumn(name = "idcore_company", insertable = false, updatable = false is to turn the "Key Information" ReadOnly and avoid mapping conflict, since the same column is used in ItemTypePurpose to map the Entity. @MapKeyColumn(name = "idcore_company", insertable = false, updatable = false是将“关键信息”转为只读,避免映射冲突,因为ItemTypePurpose使用同一列来映射Entity。

Not exactly an answer, but "worked around" to solve my needs.不完全是一个答案,而是“解决”来解决我的需求。 This solution does not cover if you want a field as Map Key other than the ID.如果您想要一个字段作为除 ID 以外的映射键,此解决方案不包括在内。

Late reply, but can be helpful to someone else.回复晚了,但可以对其他人有所帮助。

@MapKeyColumn seems to be the official solution here. @MapKeyColumn 似乎是这里的官方解决方案。 As per the documentation, it seems the annotation to be used depends on the key type of the Map, regardless of the mapped fields.根据文档,似乎要使用的注释取决于 Map 的键类型,而不管映射的字段如何。 In your case, the key type is a Long , hence below will apply:在您的情况下,密钥类型是Long ,因此以下将适用:

https://docs.oracle.com/cd/E19226-01/820-7627/giqvn/index.html https://docs.oracle.com/cd/E19226-01/820-7627/giqvn/index.html

Using Map Collections in Entities在实体中使用地图集合

Collections of entity elements and relationships may be represented by java.util.Map collections.实体元素和关系的集合可以由 java.util.Map 集合表示。 A Map consists of a key and value. Map 由键和值组成。

If the key type of a Map is a Java programming language basic type, use the javax.persistence.MapKeyColumn annotation to set the column mapping for the key.如果 Map 的键类型是 Java 编程语言基本类型,则使用 javax.persistence.MapKeyColumn 注解设置键的列映射。 By default, the name attribute of @MapKeyColumn is of the form RELATIONSHIP FIELD/PROPERTY NAME_KEY.默认情况下,@MapKeyColumn 的名称属性的格式为 RELATIONSHIP FIELD/PROPERTY NAME_KEY。 For example, if the referencing relationship field name is image, the default name attribute is IMAGE_KEY.例如,如果引用关系字段名称为 image,则默认名称属性为 IMAGE_KEY。

In summary: For nested fields go for MapKeyColumn(name="myNestFiled_key"), then you will set the value manually in your code like:总之:对于嵌套字段,请使用 MapKeyColumn(name="myNestFiled_key"),然后您将在代码中手动设置该值,例如:

ItemType.getPurposeHash().put(ItemTypePurpose.getCompany().getId(), ItemTypePurpose); ItemType.getPurposeHash().put(ItemTypePurpose.getCompany().getId(), ItemTypePurpose);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM