Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4033740
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T11:53:05+00:00 2026-05-20T11:53:05+00:00

I got stuck trying to make a ManyToMany cascading event to work. I’ve bean

  • 0

I got stuck trying to make a ManyToMany cascading event to work. I’ve bean browsing through similar posts, but can’t see where my code goes wrong, compared to others posts.

I have two tables Organisation and Project, Organisation is the owner of the relationship and there is a relation table between the two.

I am trying to persist a new project entity, that can have one or more organisation associated to it. I use a selectManyListbox(JSF 2) to let the user choose more than one organisation. All this works fine until I try to cascade the relationship. Either only the project is getting persisted or the persistent manager creates new copies of the same project, for every organisation that is associated to it. I.e. different orgID and projID (instead of different orgID refer to the same projID)

Why does JPA makes new instance of the same Project (identical properties, except the ID) when I try to persist the relation ?

I am trying to learn JPA/JSF and any help is very welcomed. Greetings Chris.

PS. In the Project bean controller I got the right instances of Organisations that the user selects.

Entity objects with primary keys

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 {


            //Use the OrganisationFacade, who is the owner of the relationship and uses Cascade to update the relation
            for(Organisation org : current.getOrganisationCollection()) {
                org.getProjectCollection().add(current);
                ejbOrganisationFacade.edit(org);

            }

            //getFacade().create(current);

            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 Organiasation facade edit function

@Override
    public void edit(Organisation entity) {
        getEntityManager().merge(entity);
    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T11:53:06+00:00Added an answer on May 20, 2026 at 11:53 am

    Solved the problem, and posting a replay to others who may do the same error that I did. Hope that is ok.

    The problem was in the create function of the Project bean. Before merging (updating) the Organisation entities, I had to call the create (persist) on the project first.

    try {
    
                getFacade().create(current);
    
                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) {
    

    Also I had a error in the create method of the project facade, which called merge instead of persist. Which cause much of the problem. Sorry for taking up space for such a trivial solution.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to work through this guide to Rails routing , but I got
I've been trying to get an NHibernate ICompositeUserType mapping to work. But am stuck
Got stuck while trying to make an element in website behave like this: <div
I've got an small issue, I'm trying to make my own forum, but I
I am trying to make a mysql table and have got stuck with making
I got stuck with a problem that looked pretty easy but i cant make
I am new to git and was trying to commit, but I got stuck
I'm trying to make some simple XML with Java and org.w3c.dom, but I got
I got stuck again between browsers compatability issues, what I was trying todo is
I've got stuck in a problem with gflags when trying to find some memory

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.