Hibernate generating SQL queries when accessing associated entity's id

前端 未结 3 482
我寻月下人不归
我寻月下人不归 2020-12-09 06:26

I have Hibernate Entities that look something like this (getters and setters left out):

@Entity
public class EntityA {
    @ManyToOne(fetch = FetchType.LAZY)         


        
3条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-09 06:56

    What you say makes sense - that it would not make a DB hit since EntityA contains the parent ID. I am just not sure if the getParent() call actually loads the EntityB object regardless of whether all you're interested in is the ID. You might try marking the children collection (and any other fields) as Lazy if you want to save the DB hit.

    @Entity
    public class EntityB : SuperEntity {
        @OneToMany(mappedBy = "parent", fetch = FetchType.LAZY)
        @Fetch(FetchMode.SUBSELECT)
        @JoinColumn(name = "parent_id")
        private Set children;
    }
    

提交回复
热议问题