PDA

View Full Version : One to Many relationship Hibernate Problem with Json data



rushi2440
26 Jan 2012, 2:39 AM
Hi.. sencha forum member. I am having one problem in which I am not able to establish One-to-Many relationship with Hibernate Object.

I am having two POJO class one is Project having many relation with Task.

my Project.java POJO is

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


private Integer projectId;
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(strategy = IDENTITY)
@Column(name = "project_id", unique = true, nullable = false)
public Integer getProjectId() {
return this.projectId;
}


public void setProjectId(Integer projectId) {
this.projectId = projectId;
}


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


@ManyToMany(fetch = FetchType.LAZY)
@JoinTable(name = "project_task", catalog = "primavera", uniqueConstraints = @UniqueConstraint(columnNames = "task_id"), joinColumns = { @JoinColumn(name = "project_id", nullable = false, updatable = false) }, inverseJoinColumns = { @JoinColumn(name = "task_id", unique = true, nullable = false, updatable = false) })
public Set<Task> getTasks() {
return this.tasks;
}


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


}

my Task.java POJO is

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


private Integer taskId;
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;
private Set<Project> projects = new HashSet<Project>(0);


public Task() {
}


public Task(Integer depth, Double duration, String durationUnit,
Date endDate, Integer parentId, Integer percentDone,
Integer priority, Date startDate, Integer taskIndex,
String taskName, Set<Project> projects) {
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;
this.projects = projects;
}


@Id
@GeneratedValue(strategy = IDENTITY)
@Column(name = "task_id", unique = true, nullable = false)
public Integer getTaskId() {
return this.taskId;
}


public void setTaskId(Integer taskId) {
this.taskId = taskId;
}


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


@ManyToMany(fetch = FetchType.LAZY, mappedBy = "tasks")
public Set<Project> getProjects() {
return this.projects;
}


public void setProjects(Set<Project> projects) {
this.projects = projects;
}


}

my Json data is

{
"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 tried a lot to insert data to my both table. I am able to insert the data to Project and Task table but the problem is that how to establish the One-to-Many relation with them.

i.e I am having another table wich manages the primary key of both the tables.

I am inserting record to Project class by


JSONObject jsonObject = JSONObject.fromObject(data);
Project newProject = (Project) JSONObject.toBean(jsonObject, Project.class);

and in Task class by


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

but my main problem is how to insert both the Id's to my third table that manages the relationship.
Any one having idea how can I solve the problem and proceed further can answer this thread..

I am using Hibernate JPA as persistence layer, and ext Js 4.0.2a as the frond end technology.

Yogendra Singh
Sr. Programmer
Kintudesigns.com

friend
26 Jan 2012, 6:30 AM
This is Hibernate relational mapping issue, not an ExtJs issue. Your question would be better answered in the Hibernate Forums (https://forum.hibernate.org/).

Regardless, here's a helpful Hibernate mapping article (http://www.vaannila.com/hibernate/hibernate-example/hibernate-mapping-one-to-many-using-annotations-1.html). Also, you might be in need of a Composite Primary Key (http://java-aap.blogspot.com/2006/04/hibernate-annotations-composite.html) for your join table.

rushi2440
26 Jan 2012, 9:02 PM
hi.. friend

I know that this is the hibernate problem, but I am using ext js with JAVA as my server side technology.
So though some one might be having this kind of relationship problem, so just asked a question.

I am just having problem with the third table which is created by the relationship of One-to-Many between Project and Task table.

Third table Project_Task holds the primary key of both the table, and I am not able to understand how to achieve this. If you have solved any kind of this issue you can give me some way to solve this problem

Yogendra Singh
Sr. Programmer
Kintudesigns.com

friend
27 Jan 2012, 6:10 AM
Sorry rushi, my message wasn't intended to be offensive. The best way I know to assist you is to point you to a more appropriate forum (Hibernate's, in this case).

Again, the question you're asking is very Hibernate specific and isn't easily answered here.