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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T11:44:09+00:00 2026-06-14T11:44:09+00:00

Using Hibernate 3.3, JPA 1.x and Hsqldb 1.8 I’m having an issue with cascading

  • 0

Using Hibernate 3.3, JPA 1.x and Hsqldb 1.8

I’m having an issue with cascading updates to objects that contain collections of other objects. Here’s a contrived (slightly) example to illustrate (getters/setters omitted). Sorry its lots of code, I can’t find another way to express the issue and its all fairly straightforward here.

@Entity
public class Salesman
{
  @Id
  @GeneratedValue
  @Column(name="salesman_id")
  private long id;

  @OneToMany(mappedBy=salesman, targetEntity=ContactSet.class, fetch=FetchType.EAGER, cascade=CascadeType.All)
  private Set<ContactSet> contacts;
}


@Entity
public class Company
{
  @Id
  @GeneratedValue
  @Column(name="company_id")
  private long id;

  @Column(name="company_name")
  private String name;
}


@Entity
public class ContactSet
{
  @Id
  @GeneratedValue
  @Column(name="contact_set_id")
  private long id;

  @ManyToOne(fetch=FetchType.EAGER)
  @JoinColumn(name="salesman_id",referencedColumnName="salesman_id", updatable=false, nullable=false)
  private Salesman salesman

  @ManyToOne
  @JoinColumn(name="company_id", referencedColumnName="company_id", updatable=false, nullable=false)
  private Company company;

  @OneToMany(mappedBy="contactSet", targetEntity=Contact.class, fetch=FetchType.EAGER, cascade=CascadeType.ALL)
  private Set<Contact> contacts;
}


@Entity
public class Contact
{
  @Id
  @GeneratedValue
  @Column(name="contact_id")
  private long id;

  @ManyToOne(fetch=FetchType.EAGER)
  @JoinColumn(name="contact_set_id",referencedColumnName="contact_set_id", updatable=true, nullable=false)
  private ContactSet contactSet;

  @Column(name="contact_name", updatable=false, nullable=false)
  private String name;
}

So a salesman has a set of contacts for each company, and there may be many salesman that service a company, each with different contacts. I can add a new company (and associated contacts) to a salesman, everything is fine.

The issue comes when adding a new company (and contacts) to a salesman that already has some:

session.beginTransaction();
session.lock(salesman, LockMode.NONE)
ContactSet newSet = new ContactSet();
/*...Add contacts to ContactSet...*/
salesman.addContactSet(newSet);
session.saveOrUpdate(salesman);
session.commit();

When running this with hibernate.show_sql=true, I can see this has the expected effect of running an Insert for each Contact, and insert for the new ContactSet. I also see an update for the Salesman itself, as well as an update for every other ContactSet and Contact that the salesman in question has. Since there are thousands of contacts for each salesman, this transaction takes a long time.

My question is, how do I avoid all the updates for ContactSets and Contacts that I know haven’t changed? I have tried adding the @Version tags, changing CascadeType (anything but ALL fails in my application) and loads of more specific code tinkering, but no success!

Thanks in advance for any pointers,

**Edit: Have found that adding session.lock(salesman, LockMode.NONE) to the transaction means the other objects don’t get updated, but the code is still slow. I suspect because its still adding thousands of objects to the session to lock them. Updated example code to reflect this. The LockMode.NONE simply reassociates the detached objects with the session.

Phil

  • 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-06-14T11:44:10+00:00Added an answer on June 14, 2026 at 11:44 am

    I was unable to find a good solution to this, so I ended up storing the ContactSet in the Company as well as the Salesman, and removing all cascade from Salesman->ContactSet. Now I only have to save a new Company, and all new ContactSets are saved. There are still situations where all a Salesman’s ContactSets must be updated, and this can only be solved (as far as I can see) by saving all of a salesman’s Companies.

    If anyone finds a better solution please add!

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

Sidebar

Related Questions

We have a setup where we are using an embedded HSQLDB for backing Hibernate/JPA
I'm using Hibernate's JPA impl to model some tables. I'm having trouble mapping a
I am using Spring 3, JPA + Hibernate for a CMS application. In that
I'm having a little trouble using Hibernate with a char(6) column in Oracle. Here's
I'm using JPA (Hibernate implementation) to save objects to the database. Selecting works fine,
I am using Hibernate with JPA annotations. I have a one-directional many-to-many association that
I'm using Hibernate 4.1.0.Final and hibernate-jpa-2.0-api. I'm having a problem using an entity manager
I have a JPA/Hibernate data model that I am using the Hibernate hbm2ddl tool
I am new to JPA and hibernate. I am using HSQLDB 2.0 and hibernate-JPA
I'm using Hibernate with JPA and have a relationship that looks like this: public

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.