I have a class, Task. I need to retrieve all tasks for a given Keyword. However, due to some database schema issues, I have to do this as a SQL query.
I have mapped everything in a @SqlResultSetMapping except for attribute that contains the parent object. I've declared a @ManyToOne relationship for the object to its parent. However, I don't know how to map that one field in the @SqlResultSetMapping.
Here's the class (I've only just started using annotations, so feel free to note if something else looks out of place):
Code:
package org.igprsweb.model;
import javax.persistence.*;
import java.sql.Date;
/**
* Model object for an inspected task
*
* @author Jason Ferguson
*/
@Entity
@Table(name="TASK")
// TODO: Map task parent
@SqlResultSetMapping(name="explicit", entities=@EntityResult(entityClass=Task.class, fields = {
@FieldResult(name="taskId", column="TSK_ID"),
@FieldResult(name="taskNum", column="TSK_NUM"),
@FieldResult(name="taskName", column="TSK_NAME"),
@FieldResult(name="taskShortTitle", column="SHRT_TITLE"),
@FieldResult(name="taskParent", column=""),
@FieldResult(name="taskIsEssential", column="IS_ESSEN"),
@FieldResult(name="taskActivationDate", column="ACT_DATE"),
@FieldResult(name="taskInactivationDate", column="INACT_DATE")
}))
@NamedNativeQuery(name="getTasksByKeyword",
query="SELECT * FROM TASK WHERE TASK.TASK_ID=EOUT_STANDARD.TSK_ID AND EOUT_STANDARD.KEYWORD=:key",
resultSetMapping = "explicit")
public class Task {
private int taskId;
private String taskNum;
private String taskName;
private String taskDesc;
private String taskShortTitle;
private Task taskParent;
private boolean taskIsEssential;
private Date taskActivationDate;
private Date taskInactivationDate;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name="TSK_ID")
public int getTaskId() {
return taskId;
}
public void setTaskId(int taskId) {
this.taskId = taskId;
}
@Column(name="TSK_NUM")
public String getTaskNum() {
return taskNum;
}
public void setTaskNum(String taskNum) {
this.taskNum = taskNum;
}
@Column(name="TSK_NAME")
public String getTaskName() {
return taskName;
}
public void setTaskName(String taskName) {
this.taskName = taskName;
}
@Column(name="TSK_DESC")
public String getTaskDesc() {
return taskDesc;
}
public void setTaskDesc(String taskDesc) {
this.taskDesc = taskDesc;
}
@Column(name="SHRT_TITLE")
public String getTaskShortTitle() {
return taskShortTitle;
}
public void setTaskShortTitle(String taskShortTitle) {
this.taskShortTitle = taskShortTitle;
}
// TODO: Map Parent Task
@ManyToOne
@JoinColumn(name="PAR_ORG_ID")
public Task getTaskParent() {
return taskParent;
}
public void setTaskParent(Task taskParent) {
this.taskParent = taskParent;
}
@Column(name="IS_ESSEN")
// TODO: Fix taskIsEssential
public boolean isTaskIsEssential() {
return taskIsEssential;
}
public void setTaskIsEssential(boolean taskIsEssential) {
this.taskIsEssential = taskIsEssential;
}
@Column(name="ACT_DATE")
public Date getTaskActivationDate() {
return taskActivationDate;
}
public void setTaskActivationDate(Date taskActivationDate) {
this.taskActivationDate = taskActivationDate;
}
@Column(name="INACT_DATE")
public Date getTaskInactivationDate() {
return taskInactivationDate;
}
public void setTaskInactivationDate(Date taskInactivationDate) {
this.taskInactivationDate = taskInactivationDate;
}
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
final Task task = (Task) o;
if (taskId != task.taskId) return false;
return true;
}
public int hashCode() {
return taskId;
}
}
[/code]