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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T05:45:37+00:00 2026-05-20T05:45:37+00:00

I have this 2 methods in the same service class: boolean meth1(DomClass dom1) {

  • 0

I have this 2 methods in the same service class:

boolean meth1(DomClass dom1) { //parameter is an instance of domain class DomClass
    ...
    meth2(dom1)
    ...
    dom1.delete(flush: true)
    ... 
    return true
}

boolean meth2(DomClass dom1) {
   ...
   dom1.changeSomeProperty
   dom1.save(flush:true)
   return true
}

The problem is that on the line calling dom1.delete(flush: true) the program crashes with deleted object would be re-saved by cascade (remove deleted object from associations).

Now, I don’t know Hibernate very well, but my guess is that either method is creating a new transaction, and meth1 has the first transaction, and meth2 the second one. And indeed, if I remove the dom1.save everything works well.

Now, my question: I can make the meth1 to contain all the code from meth2, but this will mean that I duplicate code a lot (in my real example I want to reuse some logic in many places). How can I reuse the code on one method by making all the services methods stack run in the same transaction (if this is really the problem, else: “what is the problem?”)

  • 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-20T05:45:37+00:00Added an answer on May 20, 2026 at 5:45 am

    that error happens when an instance of ClassA is referenced by an instance of ClassB, and the cascade settings of B would cause instances of A to be saved, and you delete the instance of A. The fix is simple, do exactly what the exception says and remove classA from whichever domain class references it.

    So in your case dom1 is referenced by another object in the hibernate session. You need to go thru your domain model and figure out what the association is, and then remove dom1 from the other instance.

    The reason removing the save call makes the code appear to work is because without the save, you are probably not saving the other instance that is accessing dom1 and causing the error to begin with. Without that instance being saved, there is no cascade.

    Unless you are configuration transactions yourself, all service method invocations that occur within the first service method invocation should be using the same transaction. All service methods participate in the same transaction unless you explicitly write code to not do it that way. There might be something in the … you posted.

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

Sidebar

Related Questions

I have read this post about how to test private methods. I usually do
Ok so I have a number of methods that look like this:- which sorts
Some existing web services I consume have methods that look something like this: List<Employee>
Not many are aware of this feature, but Python's functions (and methods) can have
I have this method in my db class public function query($queryString) { if (!$this->_connected)
Currently i have this method: static boolean checkDecimalPlaces(double d, int decimalPlaces){ if (d==0) return
I have a WCF service and methods are exposed as below: public interface IService
I have this method on a webpart: private IFilterData _filterData = null; [ConnectionConsumer(Filter Data
I have this method: public bool CanExecute() And after 70 commits, I added an
I have this method Verify_X which is called during databind for a listbox selected

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.