Skip child to retrieve parent - JPA

I ran into a problem when data is recursively replicated. I wanted the child to not receive parental data. This causes a recursive problem. I mentioned the code below

Pojo Structure

class Parent  {
    ..
    @OneToMany(mappedBy = "parent", fetch = FetchType.LAZY)
    private List<Child> childs;

    ..
    }

class Child {
    ..
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "parentId")
    private Parent parent;
    ..
    }

Getting data like this

 `  em = EMF.get().createEntityManager();
    Query q = em.createQuery("Select p from Parent p", Parent.class);
    List<Parent> parents = q.getResultList();
    // Till this point all looks good but when the code gets executed 
    parent.getChilds();
`

It retrieves data as follows:

Parent
child1
    Parent
        child2
            Parent
                child2
                    Parent
                ..
        ..
child2
..

What I don't need, I just need data like this:

Parent1
    child1
    child2
Parent2
    child1
    child2
    child3
+4
source share
1 answer

FetchType.EAGER , FetchType.LAZY , . , , JPA, , . .

Child Parent, optional=false @ManyToOne. .

Parent , Children.parent . ? , Children.parent? , , , .

+3

Source: https://habr.com/ru/post/1677861/


All Articles