-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 6 posts ] 
Author Message
 Post subject: Assigning object to java class having one to many relation
PostPosted: Thu Jan 26, 2012 5:16 am 
Newbie

Joined: Thu Dec 09, 2010 5:08 am
Posts: 4
hi.. hibernate forum member I need one help from you. I am using Hibernate JPA ORM as my persistence technology.

I am having problem with insertion of records to the table having one to many relationship with each other.
My POJO of the Project class is given below

Code:
@Entity
@Table(name = "project", catalog = "primavera")
public class Project implements java.io.Serializable {

   private Integer id;
   private Date endDate;
   private String projectDesc;
   private String projectName;
   private String projectTitle;
   private Date startDate;
   private Set<Task> tasks = new HashSet<Task>(0);

   public Project() {
   }

   public Project(Date endDate, String projectDesc, String projectName,
         String projectTitle, Date startDate, Set<Task> tasks) {
      this.endDate = endDate;
      this.projectDesc = projectDesc;
      this.projectName = projectName;
      this.projectTitle = projectTitle;
      this.startDate = startDate;
      this.tasks = tasks;
   }

   @Id
   @GeneratedValue
   @Column(name = "project_id")
   public Integer getId() {
      return this.id;
   }

   public void setId(Integer id) {
      this.id = id;
   }

   @Temporal(TemporalType.TIMESTAMP)
   @Column(name = "endDate", length = 19)
   public Date getEndDate() {
      return this.endDate;
   }

   public void setEndDate(Date endDate) {
      this.endDate = endDate;
   }

   @Column(name = "projectDesc")
   public String getProjectDesc() {
      return this.projectDesc;
   }

   public void setProjectDesc(String projectDesc) {
      this.projectDesc = projectDesc;
   }

   @Column(name = "projectName")
   public String getProjectName() {
      return this.projectName;
   }

   public void setProjectName(String projectName) {
      this.projectName = projectName;
   }

   @Column(name = "projectTitle")
   public String getProjectTitle() {
      return this.projectTitle;
   }

   public void setProjectTitle(String projectTitle) {
      this.projectTitle = projectTitle;
   }

   @Temporal(TemporalType.TIMESTAMP)
   @Column(name = "startDate", length = 19)
   public Date getStartDate() {
      return this.startDate;
   }

   public void setStartDate(Date startDate) {
      this.startDate = startDate;
   }

   //@OneToMany(fetch = FetchType.LAZY, mappedBy = "project")
   @OneToMany(cascade = CascadeType.ALL)
   @JoinTable(name = "PROJECT_TASK", joinColumns = { @JoinColumn(name = "project_id") }, inverseJoinColumns = { @JoinColumn(name = "task_id") })
   public Set<Task> getTasks() {
      return this.tasks;
   }

   public void setTasks(Set<Task> tasks) {
      this.tasks = tasks;
   }

}


having many relation with Task POJO class

Code:
@Entity
@Table(name = "task", catalog = "primavera")
public class Task implements java.io.Serializable {

   private Integer id;
   private Integer depth;
   private Double duration;
   private String durationUnit;
   private Date endDate;
   private Integer parentId;
   private Integer percentDone;
   private Integer priority;
   private Date startDate;
   private Integer taskIndex;
   private String taskName;

   public Task() {
   }
   
   public Task(Integer depth, Double duration,
         String durationUnit, Date endDate, Integer parentId,
         Integer percentDone, Integer priority, Date startDate,
         Integer taskIndex, String taskName) {
      this.depth = depth;
      this.duration = duration;
      this.durationUnit = durationUnit;
      this.endDate = endDate;
      this.parentId = parentId;
      this.percentDone = percentDone;
      this.priority = priority;
      this.startDate = startDate;
      this.taskIndex = taskIndex;
      this.taskName = taskName;
   }

   @Id
   @GeneratedValue
   @Column(name = "task_id")
   public Integer getId() {
      return this.id;
   }

   public void setId(Integer id) {
      this.id = id;
   }

   @Column(name = "depth")
   public Integer getDepth() {
      return this.depth;
   }

   public void setDepth(Integer depth) {
      this.depth = depth;
   }

   @Column(name = "duration", precision = 22, scale = 0)
   public Double getDuration() {
      return this.duration;
   }

   public void setDuration(Double duration) {
      this.duration = duration;
   }

   @Column(name = "durationUnit")
   public String getDurationUnit() {
      return this.durationUnit;
   }

   public void setDurationUnit(String durationUnit) {
      this.durationUnit = durationUnit;
   }

   @Temporal(TemporalType.TIMESTAMP)
   @Column(name = "endDate", length = 19)
   public Date getEndDate() {
      return this.endDate;
   }

   public void setEndDate(Date endDate) {
      this.endDate = endDate;
   }

   @Column(name = "parentId")
   public Integer getParentId() {
      return this.parentId;
   }

   public void setParentId(Integer parentId) {
      this.parentId = parentId;
   }

   @Column(name = "percentDone")
   public Integer getPercentDone() {
      return this.percentDone;
   }

   public void setPercentDone(Integer percentDone) {
      this.percentDone = percentDone;
   }

   @Column(name = "priority")
   public Integer getPriority() {
      return this.priority;
   }

   public void setPriority(Integer priority) {
      this.priority = priority;
   }

   @Temporal(TemporalType.TIMESTAMP)
   @Column(name = "startDate", length = 19)
   public Date getStartDate() {
      return this.startDate;
   }

   public void setStartDate(Date startDate) {
      this.startDate = startDate;
   }

   @Column(name = "taskIndex")
   public Integer getTaskIndex() {
      return this.taskIndex;
   }

   public void setTaskIndex(Integer taskIndex) {
      this.taskIndex = taskIndex;
   }

   @Column(name = "taskName")
   public String getTaskName() {
      return this.taskName;
   }

   public void setTaskName(String taskName) {
      this.taskName = taskName;
   }

}


I am using json to send and receive data to server.

my json data I am sending to server is in JSON format
Code:
{
    "projectTitle": "DEPENDENCY",
    "projectName": "DEPENDENCY",
    "projectDesc": "<b>DEPENDENCY<\/b>",
    "startDate": {
        "date": 25,
        "day": 3,
        "hours": 0,
        "minutes": 0,
        "month": 0,
        "seconds": 0,
        "time": 1327429800000,
        "timezoneOffset": -330,
        "year": 112
    },
    "endDate": {
        "date": 25,
        "day": 3,
        "hours": 0,
        "minutes": 0,
        "month": 0,
        "seconds": 0,
        "time": 1327429800000,
        "timezoneOffset": -330,
        "year": 112
    }
}


I am able to insert the record successfully to my Project class and my Database also shows me the inserted record.
But the problem is with the Task POJO. As the relationship says that One Project Has Many Task.

I tried first to insert Task to my database, then I am inserting the Project to my database. Till this i am able to achieve.

But the problem is how do I establish the many to one relationship with them.

I am inserting data from json to Task by below code

Code:
JSONObject jsonObject = JSONObject.fromObject(data);
Task newTask = (Task) JSONObject.toBean(jsonObject, Task.class);


but my Project has one variable
private Set<Task> tasks = new HashSet<Task>(0);

I am inserting the data from json to Project by below code
Code:
JSONObject jsonObject = JSONObject.fromObject(data);
Project newProject = (Project) JSONObject.toBean(jsonObject, Project.class);


but I am not able to understand how to insert the Task. So the one-To-many relationship established between them.
So is there any solution which i can used to get my work done

Yogendra Singh
Sr. Programmer
Kintudesigns.com


Top
 Profile  
 
 Post subject: Re: Assigning object to java class having one to many relation
PostPosted: Fri Jan 27, 2012 12:56 am 
Newbie

Joined: Thu Dec 09, 2010 5:08 am
Posts: 4
still no one to help me to come out of this problem.
I am not able to get how to solve this one to many relationship problem. I am able to insert the data in both of the table but the problem is that How do I insert the primary key of both tables to the third one



Yogendra Singh
Sr. Programmer
Kintudesigns.com


Top
 Profile  
 
 Post subject: Re: Assigning object to java class having one to many relation
PostPosted: Fri Jan 27, 2012 3:01 am 
Senior
Senior

Joined: Tue Oct 28, 2008 10:39 am
Posts: 196
So you don't have a one-to-many... You have a many-to-many. But why does your class "Task" not contain a Set<Project>?


Top
 Profile  
 
 Post subject: Re: Assigning object to java class having one to many relation
PostPosted: Fri Jan 27, 2012 3:12 am 
Newbie

Joined: Thu Dec 09, 2010 5:08 am
Posts: 4
No no.. I am having one-To-many relationship between the Project and Task table i am establishing relationship using below code

Code:
private Set<Task> tasks = new HashSet<Task>(0);
@OneToMany(cascade = CascadeType.ALL)
   @JoinTable(name = "project_task", joinColumns = { @JoinColumn(name = "project_id") }, inverseJoinColumns = { @JoinColumn(name = "task_id") })
   public Set<Task> getTasks() {
      return this.tasks;
   }


I am trying to assign data to my POJO by below code using Json

Code:
Set<Task> taskdata = new HashSet<Task>();
        taskdata.add(new Task(null,null,null,endD1,null,null,null,startD1,null,null));

jsonObject.put("projectTitle", jsonObject.getString("title"));
        jsonObject.put("projectName", jsonObject.getString("name"));
        jsonObject.put("projectDesc", jsonObject.getString("description").replace("\u200b", ""));
        jsonObject.put("startDate", startD);
        jsonObject.put("endDate", endD);
        jsonObject.put("tasks", taskdata);

Project project = (Project) JSONObject.toBean(jsonObject, Project.class);


even though I am passing the set to the my Project POJO class I am not able to insert data to my database.

so what I am doing wrong here in my code which doesn't make my code to work correctly.

Yogendra Singh
Sr. Programmer
Kintudesigns.com


Top
 Profile  
 
 Post subject: Re: Assigning object to java class having one to many relation
PostPosted: Fri Jan 27, 2012 3:26 am 
Senior
Senior

Joined: Tue Oct 28, 2008 10:39 am
Posts: 196
yaryan997 wrote:
still no one to help me to come out of this problem.
I am not able to get how to solve this one to many relationship problem. I am able to insert the data in both of the table but the problem is that How do I insert the primary key of both tables to the third one


There is no "third table" in a normal one-to-many. You only defined a connection from Project to Task. If you say there is a third table to connect project and task, where is the connection from Task to Project? Can a Task t exist without a Project p? Does a Task t belong to one and only one Project p?


Top
 Profile  
 
 Post subject: Re: Assigning object to java class having one to many relation
PostPosted: Fri Jan 27, 2012 3:46 am 
Newbie

Joined: Thu Dec 09, 2010 5:08 am
Posts: 4
yes there is not third table and I am defining connection between project and task using below code

@OneToMany(cascade = CascadeType.ALL)
@JoinTable(name = "project_task", joinColumns = { @JoinColumn(name = "project_id") }, inverseJoinColumns = { @JoinColumn(name = "task_id") })
public Set<Task> getTasks() {
return this.tasks;
}


and Task will not exist without a Project. One Project has many Tasks with it. So with the above code i am doing one-To-many relationship and also there is one annotation @JoinTable which will create the third table. But in java there is only Two POJO Project and Task. With this annotation @JoinTable third table is created by hibernate which maintains the primary key of project as project_id and primary key of task as task_id.

So do you have any solution of problem i am having ..

Yogendra Singh
Sr. Programmer
Kintudesigns.com


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 6 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.