在Hibernate中获取JOIN后触发不需要的查询

时间:2018-06-29 06:01:46

标签: hibernate

我正在尝试使用JOIN查询来获取子代和父表。 当我尝试获取数据时,查询会被触发。现在问题来了。 一旦它从JOIN提取中检索数据并将其分配给Parent类,发生了什么事便自动触发了我标记为LAZY提取的子表。

代码:

DAO:

@SuppressWarnings("unchecked")
@Override
public Event retrieveCommunicationEvent(Integer eventId) throws CommunicationException {

//  List<Predicate> conditions = new ArrayList<>();
    CriteriaBuilder builder = manager.getCriteriaBuilder();
    CriteriaQuery<Event> criteriaQuery = builder.createQuery(Event.class);
    Root<Event> root = criteriaQuery.from(Event.class);
    Join<Event, SMSCommunicationRecipient> joinSMSRecipient = root.join(Event_.smsCommunicationRecipient, JoinType.LEFT);
    criteriaQuery.select(root).where(
            builder.equal(root.get(Event_.eventId), eventId),
            builder.equal(joinSMSRecipient.get(SMSCommunicationRecipient_.isDeleted), CommunicationConstants.FLAG_N));

        Query query = manager.createQuery(criteriaQuery);
        //TypedQuery<Event> query = manager.createQuery(criteriaQuery);
        List<Event> eventList = query.getResultList();
        Event event = null;
        if (null != eventList && !eventList.isEmpty()) {
            event = eventList.get(0);
        }
        return event;

}

服务:

    @Override
@Transactional
public EventVO retrieveCommunicationEvent(Integer eventId) throws CommunicationException{
    Event event = communicationDAO.retrieveCommunicationEvent(eventId);
    EventVO eventVO= DozerUtil.map(mapper, event, EventVO.class);
    return eventVO;


}

活动POJO:

     @Entity
     @Table(name = "communication.event_master")
      public class Event extends BaseModel implements Serializable {


    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "eventid_generator")
    @SequenceGenerator(name = "eventid_generator", sequenceName = "communication.event_seq", allocationSize = 1)
    @Column(name = "event_id", unique = true, nullable = false)
    private Integer eventId;

    @Column(name = "event_name")
    private String eventName;

    @Column(name = "event_module_identifier")
    private String eventModule;

    @Column(name = "event_code")
    private String eventCode;

    @Column(name = "trigger_point")
    private String triggerPoint;

    @Column(name = "event_template_recipient")
    private String eventTemplate;

    @Column(name = "pref_email")
    private Character prefEmail;

    @Column(name = "pref_hard_copy")
    private Character prefHardCopy;

    @Column(name = "pref_sms")
    private Character prefSMS;

    @Column(name = "email_template_followed")
    private String emailTemplateFollowed;

    @Column(name = "sms_template_followed")
    private String smsTemplateFollowed;

    @Column(name = "is_deleted")
    private Character eventStatus;

    @OneToMany(mappedBy = "event", fetch = FetchType.LAZY)
    private Set<SmsTemplateMaster> smsTemplateMaster;

    @OneToMany(mappedBy = "event", fetch = FetchType.LAZY)
    private Set<EmailTemplateMaster> emailTemplateMaster;

    @OneToMany(mappedBy = "event", fetch = FetchType.LAZY)
    private Set<SMSCommunicationRecipient> smsCommunicationRecipient;

    @OneToMany(mappedBy = "event",  fetch = FetchType.LAZY)
    private Set<EmailCommunicationRecipient> emailCommunicationRecipient;
}

EmailCommunication收件人:

     @Entity
     @Table(name = "communication.email_communication_recipient")
    public class EmailCommunicationRecipient extends BaseModel implements 
  Serializable {


    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "email_recipientid_generator")
    @SequenceGenerator(name = "email_recipientid_generator", sequenceName = "communication.email_recipient_id_seq", allocationSize = 1)
    @Column(name = "email_recepient_id", unique = true, nullable = false)
    private Integer emailRecipientId;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "recipient_group_id", referencedColumnName = "reference_data_id")
    private CommunicationReferenceData recipientGroupId;

    @Column(name = "recipient_type_id")
    private String recipientTypeId;

    @Column(name = "is_deleted")
    private Character isDeleted;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "event_id")
    private Event event;

}

SMSCommunicationRecipient:

@Entity
@Table(name = "communication.sms_communication_recipient")
public class SMSCommunicationRecipient extends BaseModel implements Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "sms_recipientid_generator")
    @SequenceGenerator(name = "sms_recipientid_generator", sequenceName = "communication.sms_recipient_id_seq", allocationSize = 1)
    @Column(name = "sms_recipient_id", unique = true, nullable = false)
    private Integer smsRecipientId;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "recipient_group_id", referencedColumnName = "reference_data_id")
    private CommunicationReferenceData recipientGroupId;

    @Column(name = "is_deleted")
    private Character isDeleted;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "event_id")
    private Event event;
}

当我尝试运行此服务时,将触发以下查询:

Hibernate: select smstemplat0_.event_id as event_id8_4_0_, smstemplat0_.sms_template_identifier as sms_temp1_8_0_, smstemplat0_.sms_template_identifier as sms_temp1_8_1_, smstemplat0_.created_by as created_2_8_1_, smstemplat0_.created_on as created_3_8_1_, smstemplat0_.updated_by as updated_4_8_1_, smstemplat0_.updated_on as updated_5_8_1_, smstemplat0_.event_id as event_id8_8_1_, smstemplat0_.is_deleted as is_delet6_8_1_, smstemplat0_.sms_template_text as sms_temp7_8_1_ from communication.sms_template_master smstemplat0_ where smstemplat0_.event_id=?

之后,下面的查询也被触发。 不需要的查询:

Hibernate: select smstemplat0_.event_id as event_id8_4_0_, smstemplat0_.sms_template_identifier as sms_temp1_8_0_, smstemplat0_.sms_template_identifier as sms_temp1_8_1_, smstemplat0_.created_by as created_2_8_1_, smstemplat0_.created_on as created_3_8_1_, smstemplat0_.updated_by as updated_4_8_1_, smstemplat0_.updated_on as updated_5_8_1_, smstemplat0_.event_id as event_id8_8_1_, smstemplat0_.is_deleted as is_delet6_8_1_, smstemplat0_.sms_template_text as sms_temp7_8_1_ from communication.sms_template_master smstemplat0_ where smstemplat0_.event_id=?
ibernate: select emailtempl0_.event_id as event_i10_4_0_, emailtempl0_.email_template_identifier as email_te1_3_0_, emailtempl0_.email_template_identifier as email_te1_3_1_, emailtempl0_.created_by as created_2_3_1_, emailtempl0_.created_on as created_3_3_1_, emailtempl0_.updated_by as updated_4_3_1_, emailtempl0_.updated_on as updated_5_3_1_, emailtempl0_.email_attachment_type as email_at6_3_1_, emailtempl0_.email_subject_name as email_su7_3_1_, emailtempl0_.email_template_text as email_te8_3_1_, emailtempl0_.event_id as event_i10_3_1_, emailtempl0_.is_deleted as is_delet9_3_1_ from communication.email_template_master emailtempl0_ where emailtempl0_.event_id=?
Hibernate: select smscommuni0_.event_id as event_id7_4_0_, smscommuni0_.sms_recipient_id as sms_reci1_7_0_, smscommuni0_.sms_recipient_id as sms_reci1_7_1_, smscommuni0_.created_by as created_2_7_1_, smscommuni0_.created_on as created_3_7_1_, smscommuni0_.updated_by as updated_4_7_1_, smscommuni0_.updated_on as updated_5_7_1_, smscommuni0_.event_id as event_id7_7_1_, smscommuni0_.is_deleted as is_delet6_7_1_, smscommuni0_.recipient_group_id as recipien8_7_1_ from communication.sms_communication_recipient smscommuni0_ where smscommuni0_.event_id=?
Hibernate: select emailcommu0_.event_id as event_id8_4_0_, emailcommu0_.email_recepient_id as email_re1_2_0_, emailcommu0_.email_recepient_id as email_re1_2_1_, emailcommu0_.created_by as created_2_2_1_, emailcommu0_.created_on as created_3_2_1_, emailcommu0_.updated_by as updated_4_2_1_, emailcommu0_.updated_on as updated_5_2_1_, emailcommu0_.event_id as event_id8_2_1_, emailcommu0_.is_deleted as is_delet6_2_1_, emailcommu0_.recipient_group_id as recipien9_2_1_, emailcommu0_.recipient_type_id as recipien7_2_1_ from communication.email_communication_recipient emailcommu0_ where emailcommu0_.event_id=?

我只需要SMSCommunicationRecipient子表以及父表。 但是,即使我将Fetch type标记为LAZY时,也要获取另一个子表。 我可以知道我在哪里输入了错误的代码吗?

注意:我正在@Trancational批注中使用Dozer映射。是问题所在吗?

0 个答案:

没有答案