简体   繁体   English

JPA 按实体或 Id 映射 OneToOne

[英]JPA mapping OneToOne by Entity or By Id

What is the reason of following mapping?以下映射的原因是什么?

  @Column(name = "inspection_platform_id")
  private Long inspectionPlatformEntityId;

  @OneToOne(fetch = FetchType.LAZY)
  @JoinColumn(name = "inspection_platform_id", insertable = false, updatable = false)
  private InspectionPlatformEntity inspectionPlatformEntity;

Why do we need to specify mapping for foreign key if we specified mapping for whole entity and we can easily extract ID from that entity.如果我们为整个实体指定映射并且我们可以轻松地从该实体中提取 ID,为什么我们需要为外键指定映射。

I have a suggestion that this is due to lazy loading so we do not need to fetch the whole entity just to extract it's ID, but I also seen this approach with EAGER fetch type so I don't understand the reason我有一个建议,这是由于延迟加载,所以我们不需要获取整个实体只是为了提取它的 ID,但我也看到了这种 EAGER 提取类型的方法,所以我不明白原因

Reasons to do so:这样做的理由:

  • JPA dot notation '.' JPA 点符号 '.' over a relationship in JPQL specifies an inner join, or you have to explicitly define the join to be left out join. over JPQL 中的关系指定一个内部连接,或者您必须显式定义要忽略连接的连接。

Some providers might optimize and understand "Select entity from Entity e where e.inspectionPlatformEntity.id =:value" does not require a table join, but you are at the JPA provider's mercy, and would have to understand which side of relationship the FK exists on.一些提供商可能会优化和理解"Select entity from Entity e where e.inspectionPlatformEntity.id =:value"不需要表连接,但您受 JPA 提供商的摆布,并且必须了解 FK 存在于哪一方的关系上。 Having basic mappings makes it more clear and allows you do define precisely what you want if you need to.拥有基本映射可以使其更加清晰,并允许您在需要时准确定义您想要的内容。

  • Serialization.序列化。 You may want to have the ID serialized elsewhere in some situations, but not require the object be serialized.在某些情况下,您可能希望在别处序列化 ID,但不需要序列化 object。 When writing out this object, because the join column for inspectionPlatformEntity is insertable/updatable=false, you don't need this referenced object serialized with this entity all over the place.当写出这个 object 时,因为inspectionPlatformEntity 的连接列是可插入的/可更新的=false,你不需要这个引用的 object 序列化这个实体到处都是。

  • This makes batch updates more efficient, so you don't need to fetch a bunch of InspectionPlatformEntity instances to check if they exist and need updating.这使得批量更新更加高效,因此您无需获取一堆 InspectionPlatformEntity 实例来检查它们是否存在并需要更新。 I can leave that relationship as null and JPA will just ignore it, lazy or not.我可以将这种关系保留为 null 和 JPA 会忽略它,不管是否懒惰。

  • refactoring.重构。 Our devs don't know if they need the reference or just the FK value.我们的开发人员不知道他们是需要参考还是只需要 FK 值。 Having everything go through that reference object forces me to always have it loaded where they need it.通过该参考 object 拥有所有 go 迫使我始终将其加载到他们需要的地方。 Giving them the FK value gives me the option to load the references in some use cases, or get them to look it up using the FK value for others.给他们 FK 值让我可以选择在某些用例中加载引用,或者让他们使用 FK 值查找其他用例。 They don't need to know what is more efficient for their use case when they are writing it, and we can go out to testing before determining what should be done.他们在编写时不需要知道什么对他们的用例更有效,我们可以在确定应该做什么之前先进行测试。

  • caching.缓存。 As per above with serialization, I may need to cache this entity and in some cases want the referenced entity cached with it, or in some object graphs, the ability to null it out without losing knowledge of the relationship still existing.根据上面的序列化,我可能需要缓存这个实体,并且在某些情况下希望引用的实体与它一起缓存,或者在某些 object 图中,能够在不丢失仍然存在的关系的情况下将其 null 取出。

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

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