【发布时间】:2016-03-13 09:20:44
【问题描述】:
我正在尝试使用 JPA 将实体保留在 OneToMany 关系中。我有两个实体,Proyecto(项目)和 Participacion(参与)。 Proyecto 可以有多个 Participacion。问题是当我尝试将 Participacion 坚持到现有的 Proyecto 时。
//First, I get the selected project by the user from the database.
Proyecto proyecto = proyectoManager.getProyecto(Integer.parseInt((String) session.getAttribute("idProyecto")));
//Second, I create and set the Participacion parameters, including the project.
Participacion participacion = new Participacion();
participacion.setIdProyecto(proyecto);
participacion.setLogin(usuario);
participacion.setPorcParti(Integer.parseInt(request.getParameter("porc")));
//Finally I persist participacion with his respective project
proyectoManager.addParticipacion(participacion);
//Here, I'm trying to get the project to check if his Participacion collection contains the new participation.
proyecto = proyectoManager.getProyecto(Integer.parseInt((String) session.getAttribute("idProyecto")));
问题是我第二次从数据库中恢复项目,因为他的参与集合不包含新的参与。我尝试过使用 edit() 方法,甚至在 persist() 方法之后刷新,但它不起作用。
下次部署Enterprise Application时,项目中包含了他各自的参与,包括新的,但是第一次persist后需要,不再部署。
谁能帮帮我?谢谢。
【问题讨论】:
标签: java jpa collections entity persistence