我不能列出@onetomany或@manytoone双向

时间:2014-06-17 08:33:07

标签: java hibernate java-ee one-to-many many-to-one

Task.java

@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&gt; NullPointerException

2&gt;空列表,size = 0

数据模型

enter image description here

1 个答案:

答案 0 :(得分:0)

您错过将TaskAction添加到Task TaskAction列表actions

task.getActions().add(TaskAction)

为了使JPA在双向映射中完美地工作,需要在两侧正确设置关系。

纯英语 - TaskAction知道TaskTask不知道TaskAction