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