Skip to main content

Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » EclipseLink » eclipselink jpa lazy loading does not work(@ManyToOne lazy loading does not work)
eclipselink jpa lazy loading does not work [message #1790314] Fri, 08 June 2018 09:07 Go to next message
Lealhom Li is currently offline Lealhom LiFriend
Messages: 1
Registered: June 2018
Junior Member
When I use @ManyToOne(optional = false,fetch = FetchType.LAZY) in a property and it seems like lazy loading not work. I use 'spring-boot-starter-data-jpa' . My code as follows
public class Person implements Serializable {

    private final String id = UUID.randomUUID().toString();

    private String name;

    @OneToMany(cascade = CascadeType.ALL, mappedBy = "person", orphanRemoval = false, fetch = FetchType.LAZY)
    @Size(min = 1)
    private List<BankCard> bankCards = new ArrayList<>();
@Table(name = "lazy_test_bank_card")
public class BankCard implements Serializable {

    private final String id = UUID.randomUUID().toString();

    private String bankName;

    @ManyToOne(optional = false,fetch = FetchType.LAZY)
    @JoinColumn(name = "person_id")
    private Person person;
import com.omg.lazyLoadingTest.model.BankCard;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

public interface BankCardRepository extends JpaRepository<BankCard, String>,
                                 JpaSpecificationExecutor<BankCard> {

import com.omg.lazyLoadingTest.model.BankCard;
import com.omg.lazyLoadingTest.model.Person;
import com.omg.lazyLoadingTest.repository.BankCardRepository;
import com.omg.lazyLoadingTest.repository.PersonRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

public class TestController {
    private PersonRepository personRepository;

    private BankCardRepository bankCardRepository;

    @GetMapping(path = "/person1/{id}")
    public Person person1(@PathVariable String id) {
        Person person = personRepository.findOne(id);
        //At this case ,@OneToMany lazy loading worked, it will not load the 'bankCards'
        return person;

    @GetMapping(path = "/bankCard/{id}")
    public BankCard bankCard(@PathVariable String id) {
        BankCard bankCard = bankCardRepository.findOne(id);
		//However at this case, @ManyToOne lazy loading not worked, it will load the 'person' although I configured FetchType.LAZY
        return bankCard;

When I test it , the 'bankCards' property in can use lazy loading but the 'person' property in lazy loading not worked.
When call bankCardRepository.findOne(id); The sql in the console are:
Hibernate: select as id1_0_0_, bankcard0_.bank_name as bank_nam2_0_0_, bankcard0_.person_id as person_i3_0_0_ from lazy_test_bank_card bankcard0_ where
Hibernate: select as id1_2_0_, as name2_2_0_ from lazy_test_person person0_ where
Re: eclipselink jpa lazy loading does not work [message #1790829 is a reply to message #1790314] Mon, 18 June 2018 16:23 Go to previous message
Chris Delahunt is currently offline Chris DelahuntFriend
Messages: 1389
Registered: July 2009
Senior Member
Have you tried using EclipseLink as the JPA provider, otherwise you might try posting your problem in the Hibernate forum. You might also want to check that your serialization isn't causing the lazy relationship to be fetched.
Previous Topic:Disable database change notification type
Next Topic:Eclipselink support - duplicate
Goto Forum:

Current Time: Mon Nov 28 21:41:21 GMT 2022

Powered by FUDForum. Page generated in 0.08040 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top