Hibernate PersistentSet remove() operation not working


Chacko Mathew

I have a collection in parent entity like this:

Class Parent {
 @OneToMany(mappedBy = parent, cascade = CasacadeType.ALL)
 Set<Child> children;
}

Class Child {
 @Column(nullable=false)
 @ManyToOne
 Parent parent;
}

Now the event happens, if I do a remove() operation on the Set on one of its elements, it doesn't actually remove it.

Vlad Mihalcea

Your mapping should look like this:

public class Parent { 
    @OneToMany(mappedBy = parent, cascade = CasacadeType.ALL, orphanRemoval = true) 
    private Set<Child> children = new HashSet<>();

    public void removeChild(Child child) {
        children.remove(child);
        child.setParent(null);
    }
}

public class Child {
    @ManyToOne
    private Parent parent;
}

As described in this article , since you have a two-way association, you must synchronize both sides.

So the best thing to do is to call:

parent.removeChild(child);

As a result, it removeChildis to be removed Childfrom children Setand set up the Child parentassociation null.

Related


Hibernate PersistentSet remove() operation not working

Chacko Mathew I have a collection in parent entity like this: Class Parent { @OneToMany(mappedBy = parent, cascade = CasacadeType.ALL) Set<Child> children; } Class Child { @Column(nullable=false) @ManyToOne Parent parent; } Now the event happens, if I d

Hibernate PersistentSet remove() operation not working

Chacko Mathew I have a collection in parent entity like this: Class Parent { @OneToMany(mappedBy = parent, cascade = CasacadeType.ALL) Set<Child> children; } Class Child { @Column(nullable=false) @ManyToOne Parent parent; } Now the event happens, if I d

Hibernate PersistentSet remove() operation not working

Chacko Mathew I have a collection in parent entity like this: Class Parent { @OneToMany(mappedBy = parent, cascade = CasacadeType.ALL) Set<Child> children; } Class Child { @Column(nullable=false) @ManyToOne Parent parent; } Now the event happens, if I d

Hibernate compares PersistentSet and HashSet

Antoine I have a question. In my JUnit test, I want to test inserts in the database done by hibernate. I have an entity which has a list and I want to see if the inserted list is equal to the list I set. assertEquals(listDerogationLinux, derogation.getListDero

Operation timeout in Hibernate

NoobEditor Learning to hibernate.. take it easy :) Here is the main class I use to perform actions based on user selection: package hibernate_tut_emp; import java.util.Iterator; import java.util.List; import java.util.Scanner; import org.hibernate.Query; imp

Operation timeout in Hibernate

NoobEditor Learning to hibernate.. take it easy :) Here is the main class I use to perform actions based on user selection: package hibernate_tut_emp; import java.util.Iterator; import java.util.List; import java.util.Scanner; import org.hibernate.Query; imp

Operation timeout in Hibernate

NoobEditor Learning to hibernate.. take it easy :) Here is the main class I use to perform actions based on user selection: package hibernate_tut_emp; import java.util.Iterator; import java.util.List; import java.util.Scanner; import org.hibernate.Query; imp

NSpredicate "and" operation not working as expected

Santosh Kashyap When i get all the values from the core data entity and when i loop and print the values i have the following data i have this Type: Inviter: Mobile Staff: 1:1234123131 Staff: 1:2314213211 Client: 1:1214212312 Client: 1:2412142121 Client: 1:542

Compare operation === not working on Chrome?

Delphinium I don't know why, but the code below works in Firefox but not in Google Chrome, why? This should be standard JS. status = parseInt($('#input-status').val()); // status field is exactly equals to 0 if (status === 0) { // do something, in Firefox the

@Transient not working in hibernate mode

Romil Gaurav: I am using hibernate 4.1.9. my code is @Transient private String ldapIdTemp; package is import javax.persistence.Transient; Still in hibernate query, it doesn't work and puts the properties in the query. Part of the query fragment (assetasset0_

Hibernate cascade delete not working

Buffalo Buffalo: I am using hibernate 3 and trying to delete a record in the database and the delete is not working. Hibernate mode is targeting (in pseudocode): create table Employer( employer_id number(12) primary key, employer_name varchar2(50) );

Event listener not working on Hibernate?

Ethan I implemented an event listener for auditing actions on my entity. For example, I made a file called HibernatePreInsert for PreInsertEventListener. Also, by reading about stackoverflow, I configured an integrator called HibernateIntegrator. Since I am us

hibernate validator not working

Omar B. I'm using Hibernate Validator with Hibernate and Spring, but the validation doesn't seem to work. When I don't enter a string or enter a 1-character string (not between min=4and max=20), no error is displayed, so it is saved in the table. What am I mis

hibernate validator not working

Omar B. I'm using Hibernate Validator with Hibernate and Spring, but the validation doesn't seem to work. When I don't enter a string or enter a 1-character string (not between min=4and max=20), no error is displayed, so it is saved in the table. What am I mis

@Transient not working in hibernate mode

Romil Gaurav: I am using hibernate 4.1.9. my code is @Transient private String ldapIdTemp; package is import javax.persistence.Transient; Still in hibernate query, it doesn't work and puts the properties in the query. Part of the query fragment (assetasset0_

Hibernate Validator annotation not working

IAmYourFaja I just added hibernate-validator-5.1.3.Final.jarto my classpath and annotated the POJO: public class Person { @NotNull private String name; // etc... } Then I tested it with the driver: public class Driver { public static void mai

Hibernate @Generated annotation not working

open I want to use @org.hibernate.annotations.Generated to automatically update the "lastModified" field, here is my code: bill: @Entity public class Bill { @Id @GeneratedValue(strategy = GenerationType.SEQUENCE) protected Long id; @Temporal(T

Hibernate cascade delete not working

Buffalo Buffalo: I am using hibernate 3 and trying to delete a record in the database and the delete is not working. Hibernate mode is targeting (in pseudocode): create table Employer( employer_id number(12) primary key, employer_name varchar2(50) );

hibernate validator not working

Omar B. I'm using Hibernate Validator with Hibernate and Spring, but the validation doesn't seem to work. When I don't enter a string or enter a 1-character string (not between min=4and max=20), no error is displayed, so it is saved in the table. What am I mis

Event listener not working on Hibernate?

Ethan I implemented an event listener for auditing actions on my entity. For example, I made a file called HibernatePreInsert for PreInsertEventListener. Also, by reading about stackoverflow, I configured an integrator called HibernateIntegrator. Since I am us

Hibernate @Generated annotation not working

open I want to use @org.hibernate.annotations.Generated to automatically update the "lastModified" field, here is my code: bill: @Entity public class Bill { @Id @GeneratedValue(strategy = GenerationType.SEQUENCE) protected Long id; @Temporal(T

hibernate search index not working

Abdusamad Tim I am trying to integrate hibernate search into my existing application. According to the tutorial of hibernate search, I added the following properties in hibernate properties in applicationContext.xml <prop key="hibernate.search.default.director

UniqueConstrait on table not working in Hibernate

username Hibernate version is 3.x Below is the table structure, firstname | lastname | salary ----------------------------------- firstname | lastname | 100 firstname | lastname | 500 firstname | lastname | 1000 Below is the Java code, @Entity @Table(name="T

Hibernate Crtieria and throttling not working

username I have an object in an object and want to create a limit using one of the fields in the innermost object. Now I guess it can be achieved by doing the following: ... .add(Restrictions.eq(“ myObjectTwp.myObjectThree.myObjectsID”,mySearchId))。list(); How