Task.java我不能列出@OneToMany也不@ManyToOne雙向
@Entity
@Table(name = "t_task")
public class Task {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "idTask")
private long idTask;
...
@OneToMany(fetch = FetchType.EAGER, cascade = {CascadeType.ALL}, mappedBy = "task")
private List<TaskAction> actions;
...
public Task() {}
public Task(String taskname, String description, Date date,
Category category, Priority priority, User user) {
this.taskname = taskname;
this.description = description;
this.date = new Date((date.getTime()/1000) * 1000);
this.deadline = null;// new Date((deadline.getTime()/1000)*1000);
this.category = category;
this.priority = priority;
this.user = user;
this.userResponsible = null;
}
public Task(String taskname, String description, Date date, Date deadline,
Category category, Priority priority, State state, User user,
User userResponsible, String evaluation, int pending) {
this.taskname = taskname;
this.description = description;
this.date = new Date((date.getTime()/1000) * 1000);// delete milis
this.deadline = new Date((deadline.getTime()/1000) * 1000);
this.category = category;
this.priority = priority;
this.state = state;
this.user = user;
this.userResponsible = userResponsible;
this.evaluation = evaluation;
this.pending = pending;
}
TaskAction.java
@Entity
@Table(name = "t_taskaction")
public class TaskAction {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "idTaskAction")
private long idTaskAction;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "idTask", nullable = false)
private Task task;
...
public TaskAction() {
}
public TaskAction(Task task, Date date, String action, String description,
User user) {
this.task = task;
this.date = new Date((date.getTime()/1000) * 1000);
this.actionname = action;
this.description = description;
this.user = user;
}
Test.java
@Test
public void testTaskActionCreate() {
taskPriorityDao.saveOrUpdate(priority);
taskCategoryDao.saveOrUpdate(category);
taskStateDao.saveOrUpdate(state);
userDao.saveOrUpdate(user);
Date rightnow = Calendar.getInstance().getTime();
Task t1 = new Task("My first task", "This is a task", rightnow,
rightnow, category, priority, state, user, user, "okey", 0);
taskDao.merge(t1);//save
t1 = taskDao.get("My first task");
logger.info("id Task >" + t1.getIdTask());
TaskAction ta1 = new TaskAction(t1, rightnow, "Task action 1",
"Task action 1 description", user);
TaskAction ta2 = new TaskAction(t1, rightnow, "Task action 2",
"Task action 2 description", user);
TaskAction ta3 = new TaskAction(t1, rightnow, "Task action 3",
"Task action 3 description", user);
TaskAction ta4 = new TaskAction(t1, rightnow, "Task action 4",
"Task action 4 description", user);
taskActionDao.saveOrUpdate(ta1);
taskActionDao.saveOrUpdate(ta2);
taskActionDao.saveOrUpdate(ta3);
taskActionDao.saveOrUpdate(ta4);
System.out.println(" 1 >" + new TaskActionService().getAllTaskActions().size());
System.out.println(" 2 >" + t1.getActions().size());
儘管將行插入數據庫中,但我仍然收到這些消息。
1>的NullPointerException
2>空列表,大小= 0
數據模型
名字'Action'用來表示'在struts' Action類,它是一個錯誤的做法遵循這種類型的實體的命名慣例類。在未來做正確.. – Dileep