簡體   English   中英

HIbernate 忽略從 OnetoMany 字段獲取數據

[英]HIbernate ignore fetching data from OnetoMany field

我想忽略實體中的@OnetoMany字段。 獲取數據需要獲取實際字段但不想觸發對依賴表的查詢。 但是從父表中刪除數據需要從依賴表中刪除

我試過忽略但刪除也被忽略的@Transient 當我調用父實體時,還有其他選項可以告訴 JPA 不要從子表中獲取數據嗎?

@Entity
Table(name = "User")
public class UserEntity implements Serializable {
    @Id
    @Column(name = "id")
    private int id;

    @Column(name = "SERIAL", unique = true, nullable = false)
    private String serial;

    @OneToMany(mappedBy = "serialBySerialId", cascade = CascadeType.ALL)
    private Set<UserActionEntity> userActionsById;

}

@Table(name = "user_action")
public class UserActionEntity implements Serializable {

    @Id
    @Column(name = "id")
    private int id;

    @Column(name = "action")
    private String action;

    @ManyToOne
    @JoinColumn(name = "USER_ID", referencedColumnName = "ID", nullable = false)
    private UserEntity userByUserId;



如果您不想觸發對依賴表的查詢,您可以在 UserActionEntity 屬性上使用 (fetch = FetchType.LAZY)。

 @OneToMany(mappedBy = "serialBySerialId", cascade = CascadeType.ALL, fetch = FetchType.LAZY)
        private Set<UserActionEntity> userActionsById;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM