0

I get multiple select queries ('1' for parent and 'n' select queries for n child records) while fetching child parent entities based on the provided parent Id.

To overcome this I put @Fetch(FetchMode.JOIN) on both parent and child entities. But that didn't help. Parent (MessageDetails) and child (MessageHits) entities I used are as below

 @Entity
    @Table(name = "Scantit_msg_dtls")
    public class ScanitMessageDetails implements DomainObject{
 private String icgreference;
 private Long messageId;
private Set<ScanitMessageHit> scanitMessageHits = new HashSet<ScanitMessageHit>();

     @Id
@Column(name = "ICGREFERENCE")
public String getIcgreference() {
    return icgreference;
}

        @Column(name = "PK_MRS_MESSAGE")
    public Long getMessageId() {
        return messageId;
    }

@OneToMany(mappedBy="scanitMessageDetails", orphanRemoval = true, 
    cascade = { javax.persistence.CascadeType.ALL }, fetch=FetchType.EAGER)

        @Fetch(FetchMode.JOIN)
    public Set<ScanitMessageHit> getScanitMessageHits() {
        return scanitMessageHits;
    }

     }

and the child entity is

@Entity
@Table(name = "Scanit_msg_hit")

public class ScanitMessageHit implements DomainObject {

    private static final long serialVersionUID = 1L;
    private Long hitId;
private ScanitMessageDetails scanitMessageDetails;

@Id
    @Column(name = "PK_MRS_HIT")
    public Long getHitId() {
        return hitId;
    }

    public void setHitId(Long hitId) {
        this.hitId = hitId;
    }

    @ManyToOne(cascade = { javax.persistence.CascadeType.ALL }, fetch=FetchType.EAGER)
    @Fetch(FetchMode.JOIN)
    @JoinColumn(name="FK_MRS_MESSAGE",referencedColumnName="PK_MRS_MESSAGE")
    public ScanitMessageDetails getScanitMessageDetails() {
        return scanitMessageDetails;
    }

    public void setScanitMessageDetails(ScanitMessageDetails scanitMessageDetails) {
        this.scanitMessageDetails = scanitMessageDetails;
}
}

Though I am using join fetching strategy, I still get multiple (n+1) select queries while fetching data. This is hampering the application performance.

KINDLY SUGGEST IF I HAVE MADE ANY MISTAKE HERE OR IF THERE IS ANY OTHER WAY TO OVERCOME THIS ISSUE. REALLY APPRECIATE IF YOU CAN HELP ME ON THAT.THANKS IN ADVANCE

Rishi

1 Answer 1

4

can you try same after removing fetching strategy from class ScanitMessageHit kindly remove fetch=FetchType.EAGER hope fully it will work. please change your mapping as below.

@OneToMany(mappedBy="scanitMessageDetails", cascade = CascadeType.ALL, fetch = FetchType.EAGER)
public Set<ScanitMessageHit> getScanitMessageHits() {
    return scanitMessageHits;
}

@ManyToOne
@JoinColumn(name="FK_MRS_MESSAGE",referencedColumnName="PK_MRS_MESSAGE")
    public ScanitMessageDetails getScanitMessageDetails() {
    return scanitMessageDetails;
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks Arjun,I tried that but it did not work.Still it generates n+1 select statments:( Is there any other thing I need to do?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.