2

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.

2
  • 1
    Jackson is trying to fetch this unfetched object. Commented Feb 5, 2018 at 3:34
  • @surya Yeah, it's the JsonManagedReference annotation that makes Spring load everything Commented Dec 15, 2018 at 14:23

3 Answers 3

1

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);
}
Sign up to request clarification or add additional context in comments.

Comments

1

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)

Comments

0

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

   ManyToOne(fetch= FetchType.LAZY)

2 Comments

@surya ManyToOne by default is EAGER! And OneToMany is LAZY readTheDocs
You are right , I may have misread the answer here , actually I meant for onetomany, where default is lazy.anyway I think this solution won’t work. Because in the question, user said he is trying to load the object and doesn’t want to load the collections Bd it’s not the other way.

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.