aspose file tools*
The moose likes Object Relational Mapping and the fly likes Assigning jsonobject to java class having one to many relationship Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Assigning jsonobject to java class having one to many relationship" Watch "Assigning jsonobject to java class having one to many relationship" New topic
Author

Assigning jsonobject to java class having one to many relationship

YogendraK Singh
Greenhorn

Joined: Jan 25, 2012
Posts: 7
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
James Sutherland
Ranch Hand

Joined: Oct 01, 2007
Posts: 553
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.


TopLink : EclipseLink : Book:Java Persistence : Blog:Java Persistence Performance
YogendraK Singh
Greenhorn

Joined: Jan 25, 2012
Posts: 7
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

Joined: Jan 25, 2012
Posts: 7
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
Vijitha Kumara
Bartender

Joined: Mar 24, 2008
Posts: 3832

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?


SCJP 5 | SCWCD 5
[How to ask questions] [Twitter]
YogendraK Singh
Greenhorn

Joined: Jan 25, 2012
Posts: 7
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

Joined: Mar 24, 2008
Posts: 3832

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?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Assigning jsonobject to java class having one to many relationship