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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T18:36:58+00:00 2026-05-24T18:36:58+00:00

I am unable to update the contained set within an entity. I do not

  • 0

I am unable to update the contained set within an entity. I do not get any errors, but I when I attempt to update the product with a different set of upgrade entities, the contents of the database are remaining unchanged.

I have a Product entity which contains a set of Upgrade entities.

@Entity
@Table(name="product")
public class Product {
    private Long id;
    private Set<Upgrade> upgrades;

    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    @Column(name="id")
    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    @ManyToMany(mappedBy = "products",
                targetEntity = Upgrade.class,
                cascade = CascadeType.ALL,
                fetch = FetchType.EAGER)
    public Set<Upgrade> getUpgrades() {
        return upgrades;
    }

    public void setUpgrades(Set<Upgrade> upgrades) {
        this.upgrades = upgrades;
    }
}

@Entity
@Table(name="upgrade")
public class Upgrade {
    private Long id;
    private Set<Product> products;

    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    @Column(name="id")
    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    @ManyToMany(targetEntity = Product.class,
                cascade = CascadeType.ALL,
                fetch = FetchType.EAGER)
    @JoinTable(name = "upgrade_product",
               joinColumns = @JoinColumn(name = "upgrade_id"),
               inverseJoinColumns = @JoinColumn(name="product_id"))
    public Set<Product> getProducts() {
        return products;
    }

    public void setProducts(Set<Product> products) {
        this.products = products;
    }
}

This links to the following database structure:

CREATE TABLE `product` (
  `id` bigint(20) NOT NULL AUTO_INCREMENT,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=30 DEFAULT CHARSET=latin1;

CREATE TABLE `upgrade` (
  `id` bigint(20) NOT NULL AUTO_INCREMENT,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=778 DEFAULT CHARSET=latin1;

CREATE TABLE `upgrade_product` (
  `upgrade_id` bigint(20) NOT NULL,
  `product_id` bigint(20) NOT NULL,
  PRIMARY KEY (`upgrade_id`,`product_id`),
  KEY `FK169831CC92B40B3C` (`upgrade_id`),
  KEY `FK169831CC6DAEB65C` (`product_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

When I change the Product.upgrades and calling sessionFactory.getCurrentSession().update(product) I am not seeing these changes set. I am sure I am missing something here, but can’t work out what it is.

This is inside a MySQL 5 database.

I am updating the set of Upgrades like this:

    List<Upgrade> upgradeList = getSelectedUpgrades(editProduct.getAllUpgrades());
    if (compareUpgradeLists(new ArrayList<Upgrade>(product.getUpgrade()), upgradeList) == false) {
        product.setUpgrade(new HashSet<Upgrade>(upgradeList));
        isDirty = true;
    }

    if (isDirty) {
        productDao.update(product);
    }

where getSelectedUprades is :

private List<Upgrade> getSelectedUpgrades (List<EditProductUpgradeDetails> list) {
        List<Upgrade> upgradeList = new ArrayList<Upgrade>();
        // Update the upgrades for this product
        for (EditProductUpgradeDetails upgradeDetails : list) {
            if (upgradeDetails.getSelected()) {
                // Add upgrade to new upgrade list
                Upgrade upgrade = upgradeDao.get(upgradeDetails.getId());
                upgradeList.add(upgrade);
            }
        }
        return upgradeList;
    }

and EditProductUpgradeDetails is a Form Backing Object which contains a field selected linked to a checkbox for that upgrade.

I have used debugger to check if the created Set is different, and that the Product contains that Set, however, when I call the upgrade method on the session it is failing to propagate that change to the database.

  • 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-24T18:36:59+00:00Added an answer on May 24, 2026 at 6:36 pm

    Never, really never replace a Hibernate Managed Collection with an new/other collection. You always must use the original collection and use the add and remove methods!

    never do something like this: product.setUpgrade(new HashSet<Upgrade>(upgradeList));

    you need to do something like

    class Product {
      ...
      private Set<Upgrade> upgrades;
      ...
    
    
      public void replaceUpgrades(Set<Upgrade> newUpgrades) {
         this.upgrades.retainAll(newUpgrades);
    
         HashSet<T> reallyNew = new HashSet<T>(newUpgrades);
         reallyNew.removeAll(this.upgrades);
    
         this.upgrades.addAll(reallyNew);
      }
    

    }

    If you use hibernate with field injection then it is the best not to have setter on the hibernate maintained collections.

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

Sidebar

Related Questions

I'm trying to passing an arguments (array) for update, but seems CouchDB unable to
I'm unable to run any selenium tests since I updated Firefox to 3.6. Is
For some reason I am unable to get the pattern match for preference_network_ followed
Ok, I've searched quite a bit, but seem unable to find an answer or
-update start Unable to cast object of type 'namespace.Student' to type 'System.Data.DataRowView' <%# FixNull(((System.Data.DataRowView)Container.DataItem).Row,
As of yesterday, I'm unable to update my bundle. It gets stuck at Fetching
I do not have a %CLASSPATH% set up. As I understand, this should not
This should be easy, but I cannot seem to get it right as I
Unable to find a SQL diff tool that meets my needs, I am writing
Error: Unable to read data from the transport connection: A blocking operation was interrupted

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.