简体   繁体   English

在不获取实际实体的情况下查询外键字符串 ID

[英]Query for Foreign Key string ID without fetching the actual entity

Given the model below, when fetching an entity from the myentity table with JPA, is there a way to prevent the secondary fetch that would get the entity from the anotherentity table, but would provide us with the externalPk String value (which is used as a Foreign Key) itself?给定下面的模型,当使用 JPA 从myentity表中获取实体时,有没有办法防止从anotherentity实体表中获取实体的二次获取,但会为我们提供externalPk字符串值(用作外键)本身?

As much as I understand that due to the declared types in Java it wouldn't specifically be possible, I do wonder if there's way to access that information without the extra fetch considering it is indeed right there in that myentity table as an actual varchar column.虽然我明白,因为在Java中声明类型不会特别是可能的,我不知道是否有方法来访问,如果没有信息的额外获取考虑它确实正确的,在该myentity表作为一个实际的varchar列.

If it can help, we use EclipseLink as our JPA provider.如果有帮助,我们将使用 EclipseLink 作为我们的 JPA 提供程序。

Current model当前型号

@Entity
@Table(name = "myentity")
@JsonIgnoreProperties(ignoreUnknown = true)
@Data
@NoArgsConstructor
@AllArgsConstructor
@Builder
@IdClass(MyIdClass.class)
public class MyEntity {

    @Id
    @Column(updatable = false)
    private String foo;

    @Id
    @Column(updatable = false)
    private String bar;

    @OneToOne(cascade = CascadeType.ALL, orphanRemoval = true)
    @JoinColumn(name = "my_foreign_key", referencedColumnName = "external_pk")
    private AnotherEntity anotherEntity;
}
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@Embeddable
public class MyIdClass implements Serializable {

    private String foo;
    private String bar;
}
@Entity
@Table(name = "anotherentity")
@JsonIgnoreProperties(ignoreUnknown = true)
@Data
@SuperBuilder
@NoArgsConstructor
@AllArgsConstructor
public class AnotherEntity {

    @Id
    @Column(name = "external_pk", nullable = false, updatable = false)
    private String externalPk;

    @Column
    private String something;
}

If the oneToOne relation is not optional, you can append the anotation with optional = false, fetch = FetchType.LAZY like such:如果 oneToOne 关系不是可选的,您可以使用optional = false, fetch = FetchType.LAZY附加注释,如下所示:

@OneToOne(cascade = CascadeType.ALL, orphanRemoval = true, optional = false, fetch = FetchType.LAZY)
@JoinColumn(name = "my_foreign_key", referencedColumnName = "external_pk")
private AnotherEntity anotherEntity;

You'll then be able to access its key with myEntity.getAnotherEntity().getKey() without loading the relation.然后,您将能够使用myEntity.getAnotherEntity().getKey()访问其密钥,而无需加载关系。 Here's a POC using hibernate这是一个使用休眠的 POC

If you want to be able to access the my_foreign_key foreign key value without having to call MyEntity.getAnotherEntity().getId() and perform a full fetch of anotherEntity, you should probably map it as a read-only basic mapping within the MyEntity instance:如果您希望能够访问 my_foreign_key 外键值而不必调用 MyEntity.getAnotherEntity().getId() 并执行对 anotherEntity 的完整获取,您可能应该将其映射为 MyEntity 实例中的只读基本映射:

public class MyEntity {
...
    @Column(updatable = false)
    private String bar;

    @OneToOne(fetch = FetchType.LAZY, cascade = CascadeType.ALL, orphanRemoval = true)
    @JoinColumn(name = "my_foreign_key", referencedColumnName = "external_pk")
    private AnotherEntity anotherEntity;

    @Column(name = "my_foreign_key",updatable = false, insertable=false)
    private String myForeignKey;
}

As an added benefit, while EclipseLink is pretty good about it on this type of mapping, it can be used in queries to prevent inadvertently forcing a table join unnecessarily.作为一个额外的好处,虽然 EclipseLink 在这种类型的映射上做得很好,但它可以用于查询以防止无意中不必要地强制表连接。 Draw backs though are that you must set this value from the relationship yourself to keep the value in synch with the relationship value - @MapsId might work, but it is meant for relationships involved in ID mappings, so might not be portable.但缺点是您必须自己从关系中设置此值以保持该值与关系值同步 - @MapsId 可能有效,但它适用于 ID 映射中涉及的关系,因此可能不可移植。

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

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