• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
Sheriffs:
  • Rob Spoor
  • Devaka Cooray
  • Liutauras Vilda
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Piet Souris

Assigning jsonobject to java class having one to many relationship

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi.. JavaRanch forum member I am having one problem in inserting data to my POJO having one to many relationship with each other.
I am using JPA persistence layer with spring and Ext JS 4.0.2a as a frontend technology

my Project pojo class is



my Task pojo class is


the relationship is like One project may have many Task.

the json data i am receiving on my server side is


as my id is unique and autoincreement so no need to pass id. But i am having problem with the insertion of this JSON data i am receiving as the Project primary key is associated in relation with Task.
I tried to assign the values with the below method



I tried to insert the Task with jsonTask but not able to assign task to POJO Task. When i removed the jsonTask all data in assigned to Project POJO.

So is there any way i can insert data to Project table of my database and Project's startDate, endDate,projectId will also be inserted to my Task table

Yogendra Singh
Sr. Programmer
Kintudesigns.com
 
Ranch Hand
Posts: 553
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why not just create the Project and Task directly and assign the values instead of using toBean, especially since your data does not match anyway.
Ensure that you set both sides of the task relationships (set the task's project).

You may also try using EclipseLink Moxy which now support mappings objects to/from JSON data.
 
YogendraK Singh
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi.. james thanks for your quick response

I am using Hibernate JPA as my ORM. I just want to know that first I have to insert all the data to my Project Table and then I have to select the Data available inside Project table and pass them to my Task Table.

My project table id will be the foreign key of the Task table projectId.

 
YogendraK Singh
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Bartender
Posts: 4113
72
Mac TypeScript Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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


What "third" table are you talking about? You seems to have many @OneToMany relationships defined here, which one exactly you refer here?
 
YogendraK Singh
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yes Vijitha Kumara, I am having one to many relationship between PROJECT 1------* TASK currently i am able to insert the records in both tables.

My Project table has fields id, name, title, startDate, endDate and Task table has fields id, depth, duration, durationUnit, startDate, endDate, parentId, percentDone, priority, taskIndex, taskName.

Now I want to establish one To many relation between Project and Task.

I take the input as project name, title, startDate, endDate. As soon as the project detail is entered I am also inserting the startDate and endDate to my Task table. And I am one to many relationship due to which third table is generated as project_task. But the problem is that project_task not able to hold the primary key's of project ID and task ID.

In my project pojo i am maintaining relationship with task with

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;
}


for the task insertion i am also passing the set but doesn't seems to be work. I am getting error

35802 [http-8085-4] ERROR org.hibernate.property.BasicPropertyAccessor - IllegalArgumentException in class: com.kintu.projectmgt.model.Task, getter method of property: taskId


Yogendra Singh
Sr. Programmer
Kintudesigns.com
 
Vijitha Kumara
Bartender
Posts: 4113
72
Mac TypeScript Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Before look at your solution why you need a third table to manage a One-to-Many relationship? Can't you have the "Many" side (Task table in this case) keep the foreign key to hold the relationship as that is the normal way of handling this?
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic