1

Implemented one to many relationship and select parent and child I have a onetomany relationship and I want to do a query (select *)

Folders.java

@Entity
@Table(name = "FOLDERS")
public class Folders implements Serializable {
    @Id
    @Column(name = "id")
    Long id;
    @ManyToOne
    @JoinColumn(name = "Folder_Author", insertable=false, updatable=false)
    private Authors author;
    // Getter + setter
}

Authors.java

@Entity
@Table(name = "AUTHORS")
public class Authors implements Serializable {

    @Id
    @Column(name = "id")
    Long id;
    @OneToMany(fetch=FetchType.EAGER, cascade=CascadeType.PERSIST)
    @JoinColumn(name = "Folder_Author", referencedColumnName = "id", nullable=false)
    private List<Folders> folders = new ArrayList<Folders>();
}

My request :

Query query = em.createQuery("SELECT f FROM Folders f");
return query.getResultList();

I got this result :

[
    {   
        "id":29,
        "noFolder":"0017",
        "author":{
            "id":9,
            "name":"Alpha",
            "service":null,
            "folders":[
                {"id":29,
                "noFolder":"0017",
                "author":{
                    "id":9,
                    "name":"Alpha",
                    "service":null,
                    "folders":[
                    {
                        "id":29,
                        "noFolder":"0017",
                        "author":{
                        "id":9,
                        "name":"Alpha",
                        "service":null,
                        "folders":[
                            ..
                            ..
            }
]       

What's wrong in my code ? What is the problem when I execute query I got this result .. .. I would like to get this result

[
    {   
        "id":29,
        "noFolder":"0017",
        "author":{
            "id":9,
            "name":"Alpha",
            "service":null,
            }
    }
]           
Tom
  • 977
  • 11
  • 18
user1814879
  • 984
  • 6
  • 23
  • 49
  • you mean you want the 1-N field to be LAZY loading, and you set it to EAGER? – Neil Stockton Sep 04 '17 at 09:27
  • Yes you right, but when I tried lazy I got this error : `Caused by: org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: business.entities.folders.Authors.folders, could not initialize proxy - no Session at rg.hibernate.collection.internal.AbstractPersistentCollection.throwLazyInitializationException(AbstractPersistentCollection.java:587) at ` .. – user1814879 Sep 04 '17 at 09:33
  • also the `OneToMany` needs a mappedBy specifying to link both sides. – Neil Stockton Sep 04 '17 at 09:33
  • I updated it and now it is like : `@OneToMany(mappedBy="author", targetEntity=Folders.class, fetch=FetchType.LAZY, cascade=CascadeType.PERSIST) @Fetch(FetchMode.SELECT) private List folders = new ArrayList(); ` – user1814879 Sep 04 '17 at 09:36
  • wtf is field "initiator" ? Should be "author" from the code you post – Neil Stockton Sep 04 '17 at 09:38
  • Sorry It is an error.. it is author .. – user1814879 Sep 04 '17 at 09:39

2 Answers2

3

Just use the @JsonIgnore on your collection field like this:

@OneToMany(fetch=FetchType.EAGER, cascade=CascadeType.PERSIST)
    @JoinColumn(name = "Folder_Author", referencedColumnName = "id", nullable=false)
    @JsonIgnore
    private List<Folders> folders = new ArrayList<Folders>();
Chaitanya
  • 15,403
  • 35
  • 96
  • 137
  • yes, but if I do it, how can I get values of the parent (Author) ? Like for each folders I would like to get his Author ... – user1814879 Sep 04 '17 at 12:34
  • @user1814879 You have to break the infinite loop using `@JsonIgnore` or else it never works. So you have to put on one-to-many side or many-to-one side. – Chaitanya Sep 04 '17 at 12:36
2

Use @JsonBackReference on the list of folders in the author class to prevent recursive serialization.

See also here

Marco Kunis
  • 112
  • 5