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 classCode:
@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