Home » Java » java – HIbernate ignore fetching data from OnetoMany field-Exceptionshub

java – HIbernate ignore fetching data from OnetoMany field-Exceptionshub

Posted by: admin February 25, 2020 Leave a comment

Questions:

I would like to ignore @OnetoMany field in my entity. fetch data need to get actual fields but don’t want to fire query to dependent table. But deleting data from parent table needs deletion from dependent table

I have tried @Transient that ignores but the delete is also being ignored. Is there any other option to tell JPA not to fetch data from childs table when i call the parent entity?

@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;



How to&Answers:

If you don’t want to fire query to dependent table, you can use (fetch = FetchType.LAZY) on UserActionEntity property.

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