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 6564813
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T14:00:52+00:00 2026-05-25T14:00:52+00:00

In JPA, I am trying to create an Update screen for an JPA object

  • 0

In JPA, I am trying to create an Update screen for an JPA object with a list of categories for that object. This is a many to many relationship.

I am creating a join relationship on the table within the JPA object that has the following properties.

Classes.java

@JoinTable(name = "classes_has_class_categories",joinColumns = {
@JoinColumn(name = "classes_id", referencedColumnName = "id")}, inverseJoinColumns = {
@JoinColumn(name = "class_categories_id", referencedColumnName = "id")})
@ManyToMany(cascade = CascadeType.ALL)
private Collection<ClassCategories> classCategoriesCollection;

The key to my question is contained within the below logic as I have updated the database and everything is great within the DB. However, my problem is that when I access the object at a later time, the classCategoriesCollection property within the object doesn’t have all of the correct objects. I would have thought that my flush and refresh calls below would have synchronized everything up with the database, but that object property is still empty UNTIL I redeploy/recompile the application. Please help me with knowing how to get this object to sync up with the current database in the object.

ClassManager.java

for(String c : cats)
{
      int cId = ConvertToInt(c);
      ClassCategories ca = em.find(ClassCategories.class, cId);

      if(ca != null)
      {

        try{
           if(!cla.getClassCategoriesCollection().contains(ca))
           {
               ClassHasCategoriesPK classHasCategoriesPK= new ClassHasCategoriesPK();

               classHasCategoriesPK.setCategoryId(ca.getId());
               classHasCategoriesPK.setClassId(cla.getId());

                ClassHasCategories cd = new ClassHasCategories(classHasCategoriesPK);

                em.persist(cd);
                em.flush();                               

            }
        }
        catch (Exception ex)
        {}    
        }
      }

      em.persist(cla); //cla is the Class object instance.
      em.flush();
      em.refresh(cla);

Later on in the code….

request.setAttribute("classCategories",cl.getClassCategoriesCollection());

Doesn’t have the right classes until a recompile of the application.

  • 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-25T14:00:53+00:00Added an answer on May 25, 2026 at 2:00 pm

    Why do you have a ClassHasCategories class for a M_N relationship table when your relationship has no extra parameters? Since Classes has a @ManyToMany(cascade = CascadeType.ALL) relationship with ClassCategories, all you need to be doing is something like:

    classObj.getClassCategoriesCollection().add(classCategoryObj);
    em.merge(classObj).
    

    The provider will take care of updating the relationship table. You can also remove items from classCategoriesCollection using similar logic:

    classObj.getClassCategoriesCollection().remove(classCategoryObj);
    em.merge(classObj).
    

    So, the answer is:

    • If classes_has_class_categories has no columns but the pk with two fks, get ride of ClassHasCategories and ClassHasCategoriesPK. Work with classCategoriesCollection to update the relationship table.
    • If classes_has_class_categories has extra columns, then redo your mapping in a way that ClassHasCategoriesPK has two @ManyToOne relationships, respectively for Classes and ClassCategories. Update relationships by directly manipulating ClassHasCategories. Here is a code example.

    The rule of thumb is, never mix both strategies (like you are doing by having both a @ManyToMany relationship and a separate ClassHasCategories object for the same table).

    Cheers,

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

Sidebar

Related Questions

i am trying to persist two entities(with one to many relationship) using jpa but
I am trying to create an Eclipse application that uses JPA for persistence. I
I'm trying to create a query with the Criteria API from JPA 2.0, but
I am trying to figure out the difference between Memcached and JPA, this is
I've mapped two entities using JPA (specifically Hibernate). Those entities have a one-to-many relationship
I have an object that contains a list as on of its fields, the
I'm trying to wire up Spring Data JPA objects manually so that I can
I'm trying to learn JPA and I want create a simple Java command line
I have created an EJB3 project and a JPA project. I'm trying to create
I am trying my way around JPA but I cant get this to work

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.