双向多对一jpa

时间:2018-02-24 17:18:23

标签: java jpa eclipselink

我尝试使用双向多对一。

User.class

@OneToMany(mappedBy = "executor", cascade = CascadeType.ALL, fetch =FetchType.EAGER)
private Set<Task> executeTasks = new HashSet<Task>();

Task.class

@ManyToOne(cascade = CascadeType.MERGE) @JoinColumn(name = "executor_id")
private User executor;

TaskBean.class

@Named
@RequestScoped
public class TaskBean extends AbstractBean implements Serializable {

    @EJB
    private TaskManager taskManager;

    @EJB
    private UserManager userManager;

    private Task task = new Task();

   public String doCreateTask() {
        task.setExecutor(currentUser);
        currentUser.getExecuteTasks().add(task);
        taskManager.createTask(task);
    }

TaskManager.class

 @Stateless
public class TaskManager implements Serializable {
    @PersistenceContext
    private EntityManager em;

    @Inject
    private Logger logger;

    public Task createTask(Task task) {
        em.persist(task);
        return task;
    }

我有带有id的User.class对象。他存在于DB上。我创建Task.class对象并在DB中插入。它会成功插入用户的ID。但是当我从DB中选择User.class对象时,它被选中而没有任务。我希望,你能给我一个解决方案。谢谢!

0 个答案:

没有答案