0

I have 2 model classes: Project and Task. They have a one-to-many relationship; that is, one Project can have many Tasks and one Task has only one Project.

Project class:

@Entity
@Table(name = "project")
public class Project implements Serializable{
   private int  project_id;
   private int manager_id;
   private String name;
   private String description;
   private  Set<Task> Projects = new HashSet<Task>();

   @Id
   @Column(name = "project_id")
   public int getProject_id() {
       return project_id;
   }

   @Column(name = "manager_id")
   public int getManager_id() {
       return manager_id;
   }

   @Column(name = "name")
   public String getName() {
       return name;
   }

   @Column(name = "description")
   public String getDescription() {
       return description;
   }

   public void setProject_id(int project_id) {
       this.project_id = project_id;
   }

   public void setManager_id(int manager_id) {
       this.manager_id = manager_id;
   }

   public void setName(String name) {
       this.name = name;
   }

   public void setDescription(String description) {
       this.description = description;
   }

   @OneToMany(fetch = FetchType.EAGER, mappedBy = "project", cascade = CascadeType.ALL)
   public Set<Task> getProjects() {
       return Projects;
   }

   public void setProjects(Set<Task> Projects) {
       this.Projects = Projects;
   }
}

Task class:

@Entity
@Table(name = "task")
public class Task implements Serializable {
    private int task_id;
    private Project project;
    private int developer_id; 
    private String date ;       
    private String hours; 
    private String description; 
    private String overtime;

    @Id
    @Column(name = "task_id")
    public int getTask_id() {
        return task_id;
    }

    @Column(name = "developer_id")
    public int getDeveloper_id() {
        return developer_id;
    }

    @Column(name = "date")
    public String getDate() {
        return date;
    }

    @Column(name = "hours")
    public String getHours() {
        return hours;
    }

    @Column(name = "description")
    public String getDescription() {
        return description;
    }

    @Column(name = "overtime")
    public String getOvertime() {
        return overtime;
    }

    public void setTask_id(int task_id) {
        this.task_id = task_id;
    }

    public void setDeveloper_id(int developer_id) {
        this.developer_id = developer_id;
    }

    public void setDate(String date) {
        this.date = date;
    }

    public void setHours(String hours) {
        this.hours = hours;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public void setOvertime(String overtime) {
        this.overtime = overtime;
    }

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "project_id")
    public Project getProject() {
        return project;
    }

    public void setProject(Project project) {
        this.project = project;
    }
}

I have used a one-to-many relationship in Hibernate to get a JSON output, but I get the following error:

SyntaxError: JSON.parse: expected ':' after property name in object at line 1 column 81390 of the JSON data

Also there is a huge data set in raw data.

2
  • This question has been asked many times. You're sending a project, which has tasks, which have a project, which have tasks, which have a project, which have tasks, etc. etc. So you have an infinit recursive structure. You need to send back a structure that is not recursive, either by transforming your persistent entities into DTOs designed for your API, or by configuring your JSON marshaller to not serialize some parts of your entities. Commented Apr 18, 2018 at 6:31
  • Possible duplicate of JPA to GET a joined entity returns a recursive fetch loop Commented Apr 18, 2018 at 6:44

0

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.