JPA one to one mapping FetchType.LAZY not working

I have two objects like below

@Entity
@Table(name = "ticket")
public class Ticket {

    @OneToOne(fetch=FetchType.LAZY, targetEntity = com.vahana.entity.TicketBookingAdditionalInfo.class, mappedBy = "ticket", cascade = CascadeType.MERGE)
    private TicketBookingAdditionalInfo ticketBookingAdditionalInfo;

}

@Entity
@Table(name = "ticket_booking_additional_info")
public class TicketBookingAdditionalInfo {

    /** The ticket. */
    @ManyToOne(fetch = FetchType.LAZY, cascade = CascadeType.MERGE)
    @JoinColumn(name = "ticket_id", columnDefinition = "BIGINT UNSIGNED")
    private Ticket ticket;

}

      

My ticket table contains 40,000 entries, but the additional information on ticket reservations does not contain any data. This table will only contain data if I'm going to add more information.

When the ticket table is selected, it joins the ticket_booking_additional_info table and this query takes longer.

I added fetch = FetchType.LAZY to @OneToOne but it still takes a long time to answer. This is the most time consuming query in my DB. How to avoid this problem. Please one of them will help me.

We use mysql DB,

+3


source to share


1 answer


@Entity
@Table(name = "ticket")
public class Ticket {

    //Default is lazy
    @OneToMany(mappedBy = "ticket", cascade = CascadeType.MERGE)
    private List<TicketBookingAdditionalInfo> ticketBookingAdditionalInfo;

}

@Entity
@Table(name = "ticket_booking_additional_info")
public class TicketBookingAdditionalInfo {

    // The ticket. 
    //Default is eager
    @ManyToOne(fetch = FetchType.LAZY, cascade = CascadeType.MERGE)
    @JoinColumn(name = "ticket_id", columnDefinition = "BIGINT UNSIGNED")
    private Ticket ticket;

}

      

note If you have a ManyToOne, the feedback is OneToMany else if you have OneToOne, the reverse is OneToOne. You should see this tutorial



+1


source







All Articles