我尝试使用双向多对一。
@OneToMany(mappedBy = "executor", cascade = CascadeType.ALL, fetch =FetchType.EAGER)
private Set<Task> executeTasks = new HashSet<Task>();
@ManyToOne(cascade = CascadeType.MERGE) @JoinColumn(name = "executor_id")
private User executor;
@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);
}
@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对象时,它被选中而没有任务。我希望,你能给我一个解决方案。谢谢!