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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T12:49:24+00:00 2026-05-26T12:49:24+00:00

I have 2 Hibernate classes in a Spring-driven Application like these: @Entity public class

  • 0

I have 2 Hibernate classes in a Spring-driven Application like these:

@Entity
public class Image {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;

@ManyToMany(mappedBy = "picture", fetch = FetchType.LAZY)
private Set<InspectionObjectDetail> inspectionObjectDetails = new HashSet<InspectionObjectDetail>();

@Override
public int hashCode() {
    // Eclipse generated hashCode()
}

@Override
public boolean equals(Object obj) {
    // Eclipse generated equals()
 }
}

and

@Entity
public class InspectionObjectDetail {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;


@ManyToMany (fetch = FetchType.LAZY)
@JoinTable(
name = "MXInspectionObjectDetail_picture",
joinColumns = {@JoinColumn(name = "INSPECTIONOBJECTDETAIL_ID")},
inverseJoinColumns = {@JoinColumn(name = "PICTURE_ID")}
)
@Where(clause = "active = true")
private Set<UOCImage> picture = new HashSet<UOCImage>();
@Override
public int hashCode() {
    // Eclipse generated hashCode()
}

@Override
public boolean equals(Object obj) {
    // Eclipse generated equals()
 }
}

I can add elements to this relation and query them just fine, but a jUnit test where I try to remove the relation without actually deleting one of these objects fails.

What I’m currently doing is unlinking each object from the other one in a transactional context:

// Junit Test

@Test
public void deleteInspectionObjectDetailImage() {
    InspectionObjectDetail inspectionObjectDetail = new InspectionObjectDetail();
    dao.save(inspectionObjectDetail);
    someClass.saveInspectionObjectDetailImage(controlObject.getId());
            //passes
    assertTrue(inspectionObjectDetail.getPictures().size() == 1);
    UOCImage image = inspectionObjectDetail.getPictures().iterator().next();
    someClass.deleteImage(image);
    Set<UOCImage> images = inspectionObjectDetail.getPictures();
            //fails
    assertTrue(images.size() == 0);
}

// BO Class

@Transactional
@Component
public class SomeClass() {
    public void delteImage(Image image) {
        if(image!= null) {
        image.setActive(false);
        while(image.getInspectionObjectDetails().iterator().hasNext()) {
             InspectionObjectDetail inspectionObjectDetail = image.getInspectionObjectDetails().iterator().next();
             inspectionObjectDetail.getPictures().remove(image);
             image.getInspectionObjectDetails().remove(inspectionObjectDetail);

                }
        }
        sessionFactory.getCurrentSession().flush();
     }

}

The Hibernate Log tells me that it did an insert to the relationship to the relationship is executed correctly, my remove statements however do not appear (Even after the session.flush() ).
If possible I’d like to keep the lazy fetching the way it is, because I expect a lot of reads on the either table that won’t need the according other class loaded.

Has someone any pointers for me?

UPDATE

regarding @Thor84no ‘s suggestion, I tried to remove each association by removing it through the iterator.remove() function, so my BO Object now looks like this:

// BO Class

@Transactional
@Component
public class SomeClass() {
    public void delteImage(Image image) {
        if(image!= null) {
        image.setActive(false);
        Iterator<InspectionObjectDetail> inspectionObjectDetailsIterator = image.getInspectionObjectDetails().iterator();
        while (inspectionObjectDetailsIterator.hasNext()) {
            InspectionObjectDetail inspectionObjectDetail = inspectionObjectDetailsIterator.next();
            Iterator<UOCImage> imageIterator = inspectionObjectDetail.getPictures().iterator();
            while (imageIterator.hasNext()) {
                if (imageIterator.next() == image) {
                    imageIterator.remove();
                }
            }
            inspectionObjectDetailsIterator.remove();
        }
    }
    sessionFactory.getCurrentSession().flush();
}

}

In my (edited) Junittest:

    // ...
    someClass.deleteImage(image.getId());
    assertTrue(image.getInspectionObjectDetails().size() == 0);
    assertTrue(inspectionObjectDetail.getPictures().size() == 0);

The first assertion succeeds, but the second one fails, so the bidirectional association between

Image <–> InspectionObjectDetail was only reduced to
Image <– InspectionObjectDetail

  • 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-26T12:49:25+00:00Added an answer on May 26, 2026 at 12:49 pm

    For anyone with a similar problem, the real issue was that the picture Set in inspectionObjectDetail still had the image in it after the deleteImage call. In order to most easily test whether this is the case you could either use a debugger to step through the code and check whether the Set changes or print it out before and after calling the remove method.

    If you find that the object is not removed, make sure you check the hashCode() and equals() methods of the object you are removing from the Set. The Set may only use hashCode(), but do remember that hashCode() and equals() are supposed to be consistent and there’s no guarantee they ignore equals().

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

Sidebar

Related Questions

We have a Hibernate/Spring application that have the following Spring beans: <bean id=transactionManager class=org.springframework.orm.hibernate3.HibernateTransactionManager
I have a Java\Spring\Hibernate application - complete with domain classes which are basically Hibernate
I have a hierarchy of model classes in my Spring/Hibernate application. When submitting a
I am using spring and hibernate together. In my application context, I have this
I have a multi-threaded, multi-server web application with hibernate and spring managing transactions with
I have an application that is based on Swing, Spring 2.5.2, Hibernate 3.3.1. If
I am using spring + hibernate in my project; I have two classes Reminder
I have the following entity class (in Groovy): import javax.persistence.Entity import javax.persistence.Id import javax.persistence.GeneratedValue
I build an application which use Hibernate JPA2 + Spring. I have problem with
I have JSF 2.1 application uisng Spring and Hibernate. In order to solve LazyLoading

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.