I was stuck trying to cascade the ManyToMany event . I’m bean looking at similar entries, but don’t see where my code is going wrong compared to other posts.
I have two tables Organization and Project , Organization is the owner of the relationship, and there is a relationship table between them.
I am trying to save a new project object that may have one or more organizations associated with it. I am using selectManyListbox (JSF 2) so that a user can select more than one organization. All this works fine until I try to cascade the relationship. Either the project is saved, or the permanent manager creates new copies of the same project for each organization that is associated with it. That is, different orgID and projID (instead of another orgID refer to the same projID)
Why does the JPA create a new instance of the same project (identical properties except ID) when I try to maintain the relationship?
I am trying to learn JPA / JSF and any help is greatly appreciated. Greetings to Chris.
PS. In the Project bean controller, I got the correct instances of the organizations that the user selects.
Primary Key Entity Objects
public class Organisation implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Basic(optional = false)
@Column(name = "idOrganisation")
private Integer idOrganisation;
..
@JoinTable(name = "organisationproject", joinColumns = {
@JoinColumn(name = "fkOrganisation", referencedColumnName = "idOrganisation")}, inverseJoinColumns = {
@JoinColumn(name = "fkProject", referencedColumnName = "idProject")})
@ManyToMany (fetch=FetchType.EAGER, cascade={CascadeType.PERSIST, CascadeType.MERGE, CascadeType.REFRESH})
private Collection<Project> projectCollection;
..
}
public class Project implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Basic(optional = false)
@Column(name = "idProject")
private Integer idProject;
..
@ManyToMany(mappedBy = "projectCollection")
private Collection<Organisation> organisationCollection;
..
}
Project bean controller
@ManagedBean (name="projectController")
@SessionScoped
public class ProjectController implements Serializable {
private Project current;
private UISelectMany selectedOrganisations;
...
public String create() {
UserController userBean = JsfUtil.findBean("userController", UserController.class);
current.setUser(userBean.getSessionUser());
current.setTimeStamp(JsfUtil.getTimeStamp());
try {
for(Organisation org : current.getOrganisationCollection()) {
org.getProjectCollection().add(current);
ejbOrganisationFacade.edit(org);
}
JsfUtil.addSuccessMessage(ResourceBundle.getBundle("/resources/Bundle").getString("ProjectCreated"));
return prepareCreate();
} catch (Exception e) {
JsfUtil.addErrorMessage(e, ResourceBundle.getBundle("/resources/Bundle").getString("PersistenceErrorOccured"));
return null;
}
}
public UISelectMany getSelectedOrganisations() {
return selectedOrganisations;
}
public void setSelectedOrganisations(UISelectMany selectedOrganisations) {
this.selectedOrganisations = selectedOrganisations;
}
...
}
Finally, the facade editing function of Organizationia
@Override
public void edit(Organisation entity) {
getEntityManager().merge(entity);
}