Tarun Gulati
Tarun Gulati

Reputation: 31

Spring data lazy load not working

I have the two entities similar to the ones below:

@Entity
@DynamicUpdate    
public class EntityOne {
    @Id
    public long id;

    public String name;
    public String displayName;

    @JsonManagedReference
    @OneToMany(mappedBy = "id", cascade = CascadeType.ALL)
    @OrderBy(value = "name")
    public List<EntityTwo> entityTwoList;
}

entity two is

@Entity
@DynamicUpdate
public class EntityTwo {
    @Id
    public long id;

    @JsonBackReference
    @ManyToOne
    @JoinColumn(name = "id")
    public EntityOne entityOne;
}

service: Lists.newArrayList(repository.findAll());

The service calls the findAll() method of the CRUDRepository. I don't want the list of entity two objects to load when calling findall() but this solution doesn't work. Anything incorrect that i am doing here to lazy load the collection. I basically don't want the collection to be loaded until its specified.

By default the mappings are lazy load.

Upvotes: 2

Views: 852

Answers (3)

Sibin Muhammed A R
Sibin Muhammed A R

Reputation: 1

First try this,i think you need to specify fetch type lazy or Eager

   ManyToOne(fetch= FetchType.LAZY)

Upvotes: 0

osama yaccoub
osama yaccoub

Reputation: 1866

I don't know about jackson but according to jpa specs you can not force LAZY loading neither can you rely upon it ... you just provide the lazy load hint and it's totally up to the provider to load it lazily or not (unlike EAGER , it forces loading)

Upvotes: 1

surya
surya

Reputation: 2749

I am assuming jackson is trying to load your child objects.

Try followjackson-datatype-hibernate4:2.4.4 Add jackson-datatype-hibernate4:2.4.4 to your dependency and define following bean

@Bean
public Jackson2ObjectMapperBuilder configureObjectMapper() {
return new Jackson2ObjectMapperBuilder()
    .modulesToInstall(Hibernate4Module.class);
}

Upvotes: 1

Related Questions