最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

java - Hibernate Genarate Query is unexpected - Stack Overflow

programmeradmin1浏览0评论

I have this relationship where I have campaign linked with campaign_planning using @OneToMany relation like this:

@Table(name = "campaign")
public class Campaign implements Serializable {
    @Id @Column(name = "id", nullable = false)
    private UUID id;

    @ManyToOne(cascade = {CascadeType.REFRESH})
    @JoinColumn(name = "parent_id", updatable = false)
    private Campaign parent;

    @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "parent", orphanRemoval = true)
    @OrderBy("createTime")
    private List<Campaign> campaigns;

    @OneToOne(mappedBy = "campaign", cascade = CascadeType.ALL, orphanRemoval = true)
    @PrimaryKeyJoinColumn
    private ScheduleInfo scheduleInfo;

    @OneToMany(mappedBy = "superCampaign", fetch = FetchType.LAZY)
    private List<CampaignPlanning> campaignPlanning;
}


@Table(name = "campaign_planning")
public class CampaignPlanning implements Serializable {

    @Id
    private UUID id;

    @OneToOne
    @JoinColumn(name = "campaign_id", insertable = false, updatable = false)
    @NotFound(action = NotFoundAction.IGNORE)
    private Campaign campaign;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "super_campaign_id", insertable = false, updatable = false)
    private Campaign superCampaign;
}

But when we run application & try to get campaign_planinng, Even I set fetch = FetchType.LAZY on campaign_planning, it still included in my query.

What could be the issue here?

select 
  campaignpl0_.*,
  campaign1_.*,
  campaign2_.*,  
  schedulein3_.*,
from 
  campaign_planning campaignpl0_ 
  left outer join campaign campaign1_ on campaignpl0_.campaign_id = campaign1_.id 
  left outer join campaign campaign2_ on campaign1_.parent_id = campaign2_.id 
  left outer join schedule_info schedulein3_ on campaign2_.id = schedulein3_.campaign_id 
where 
  campaignpl0_.super_campaign_id = ?

I have this relationship where I have campaign linked with campaign_planning using @OneToMany relation like this:

@Table(name = "campaign")
public class Campaign implements Serializable {
    @Id @Column(name = "id", nullable = false)
    private UUID id;

    @ManyToOne(cascade = {CascadeType.REFRESH})
    @JoinColumn(name = "parent_id", updatable = false)
    private Campaign parent;

    @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "parent", orphanRemoval = true)
    @OrderBy("createTime")
    private List<Campaign> campaigns;

    @OneToOne(mappedBy = "campaign", cascade = CascadeType.ALL, orphanRemoval = true)
    @PrimaryKeyJoinColumn
    private ScheduleInfo scheduleInfo;

    @OneToMany(mappedBy = "superCampaign", fetch = FetchType.LAZY)
    private List<CampaignPlanning> campaignPlanning;
}


@Table(name = "campaign_planning")
public class CampaignPlanning implements Serializable {

    @Id
    private UUID id;

    @OneToOne
    @JoinColumn(name = "campaign_id", insertable = false, updatable = false)
    @NotFound(action = NotFoundAction.IGNORE)
    private Campaign campaign;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "super_campaign_id", insertable = false, updatable = false)
    private Campaign superCampaign;
}

But when we run application & try to get campaign_planinng, Even I set fetch = FetchType.LAZY on campaign_planning, it still included in my query.

What could be the issue here?

select 
  campaignpl0_.*,
  campaign1_.*,
  campaign2_.*,  
  schedulein3_.*,
from 
  campaign_planning campaignpl0_ 
  left outer join campaign campaign1_ on campaignpl0_.campaign_id = campaign1_.id 
  left outer join campaign campaign2_ on campaign1_.parent_id = campaign2_.id 
  left outer join schedule_info schedulein3_ on campaign2_.id = schedulein3_.campaign_id 
where 
  campaignpl0_.super_campaign_id = ?
Share Improve this question asked Mar 30 at 7:38 Opt SDOpt SD 213 bronze badges 1
  • You get the planning, which has a non- lazy campaign, which has a non lazy campaign again (in the form of the parent), which also retrieve the schedule info. So at first sight I would say nothing is wrong with this it does exactly what you ask it to do (which might nog be what you expect but that is how you configured your mappings). – M. Deinum Commented Mar 31 at 7:20
Add a comment  | 

1 Answer 1

Reset to default -1

Is it required to put fetchtype.lazy on supercampaign attribute? The only place where the supercampaignid is used in the where clause as per what you have put in the @JoinColumn on supercampaign it should be referring to the list of campaign in the parent class by its id ,so,did you check removing manytoone attribute?

发布评论

评论列表(0)

  1. 暂无评论