0

I have something like this:

 class A{
    @OneToMany(mappedBy="a")
    private List<B> bs;
}

class B {
    @ManyToOne
    private A a;
    private String name;

    @OneToOne(mappedBy = "image", cascade = CascadeType.ALL, fetch = 
    FetchType.LAZY, orphanRemoval = true)
    private C c;
}
class C{
    @Lob
    private byte[] img;

    @OneToOne(fetch = FetchType.LAZY)
    private B b;

}

When I invoke getAll() method, I need to get all except class C. I set fetch lazy, but it doesn't work at all. I have an idea to create query which will be join field bs from class A with class B but without joining class C.

And when I invoke getOne(id) method, I need to get all entities.

In every class, I have obviously id field. Can you help me write this query ?

Thanks a lot.

  • If `byte[] img` is the reason you want to use lazy fetching why not set it on that attribute instead of the relationship? – Joakim Danielson Aug 16 '18 at 13:25
  • because it doesn't work too or maybe I did it wrong. I just added @Basic(fetch = fetchType.LAZY) to field 'img' but it was getting from database anyway even if I didn't use getter... – Karol Patryk Drozdowski Aug 16 '18 at 13:27
  • You are aware that LAZY is a hint rather than a rule to not fetch data, JPA might still decide to do it. If you have to little data in your db it might be that the compiler decides to fetch all data all the time. Anyway I would primarily try to figure out why it doesn't work rather than implementing some workaround. – Joakim Danielson Aug 16 '18 at 13:32
  • I want know why it doesn't work. I am using spring data. When I annotate field as @OneToOne(fetch = FetchType.LAZY) it's loaded with spring data method anyway. – Karol Patryk Drozdowski Aug 16 '18 at 14:50
  • See [here](https://stackoverflow.com/questions/1444227/making-a-onetoone-relation-lazy) for possible reasons why it won't work (note that bytecode enhancement might also be required for simple properties to be lazily fetched) – crizzis Aug 16 '18 at 19:31

0 Answers0