2014-06-17 69 views
0

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

數據模型

enter image description here

+0

名字'Action'用來表示'在struts' Action類,它是一個錯誤的做法遵循這種類型的實體的命名慣例類。在未來做正確.. – Dileep

回答

0

你缺少添加TaskActionTaskTaskAction列表actions

task.getActions().add(TaskAction) 

要使JPA在雙向映射中完美工作,需要在兩側正確設置關係。

在純英語 - TaskAction知道TaskTask是不知道的TaskAction

+0

我添加了任務構造函數,但映射在Task和TaskAction屬性中 – Joe

+0

您可以發佈您的TaskAction構造函數嗎?即 - 新的TaskAction(t1,現在,「任務操作1」,「任務操作1描述」,用戶);' –

+0

添加TaskAction構造函數 – Joe

相關問題