Hibernate doesn't remove object from collection with children in specific case

whatswrong

Please, see method testCollections(). Two objects are created and inserted to collection. Then one of them is removed from collection. Everything works perfect, single row is added in DB. But if we execute any sql query before removing object from collection, hibernated assigns PK for new objects and they are stored in DB both, regardless that in collection is single object... Any idea how to explain it to hibernate?

    @Entity
    @Table(name = "animalowner")
    public class Owner {
        @Id
        @Column
        private Long id;
        @Column(name = "owner_name")
        private String name;
        @OneToMany(fetch = FetchType.EAGER, targetEntity = Animal.class, cascade = { CascadeType.ALL }, orphanRemoval = true)
        @JoinColumn(name = "owner_fk")
        private Set<Animal> animals;
        ......


@Entity
@Table(name = "animals")
public class Animal {
    public Animal(String name, Owner owner) {
        this.name = name;
        this.owner = owner;
    }
    public Animal() {
    }
    @Id
    @GeneratedValue(generator = "animal_generator")
    @SequenceGenerator(name = "animal_generator", sequenceName = "animal_sequence")
    private Long id;
    @Column(name = "cat_name")
    private String name;
    @ManyToOne(targetEntity = Owner.class, fetch = FetchType.EAGER)
    @JoinColumn(name = "owner_fk")
    private Owner owner;
    .........



@Component
public class SomeService {
.....
 @Override
    @Transactional
    public void testCollections(){
        Owner owner = repository.loadOwnerById(1L);
        Set<Animal> animals = owner.getAnimals();
        Animal animal1 = new Animal("animal1", owner);
        Animal animal2 = new Animal("animal2", owner);
        animals.add(animal1);
        animals.add(animal2);

        //IF any SQL called here, hibernate assigns primary keys for animal1 and animal2,
        //and 2 objects are saved to database. 

        animals.remove(animal1);
    }
.....
Vlad Mihalcea

That's because Owner is attached to the current running PersistenceContext:

Owner owner = repository.loadOwnerById(1L);

and the moment you add any Animal to the animals collection:

@OneToMany(fetch = FetchType.EAGER, targetEntity = Animal.class, cascade = { CascadeType.ALL }, orphanRemoval = true)
@JoinColumn(name = "owner_fk")
private Set<Animal> animals;

Upon executing a query the flush mechanism kicks in. Hibernate dirty checking mechanism detects the owner has changes and it synchronizes any change with the database.

The transitive persistence mechanism is going to save all new Children entities:

If a transient or detached child becomes referenced by a persistent parent, it is passed to saveOrUpdate()

That's why the Animals get saved without any explicit flush.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to remove a specific object from a List<T>

From Dev

How to remove a specific object from a List<T>

From Dev

Remove Children from Json object

From Dev

Allow_Remove doesn't remove the collection

From Dev

How to remove children objects recursively from object?

From Dev

How to remove children objects recursively from object?

From Dev

simplexmlelement object doesn't list all children

From Dev

Remove item from Object set that doesn't exist in query set

From Dev

destroying object doesn't remove delegate from invocation list

From Dev

Mongoose Model.remove(callback) doesn't remove anything from my collection

From Dev

HashSet doesn't remove an object

From Dev

Hibernate: Join table with extra columns, remove children from one side

From Dev

garbage collection doesn't remove unreachable object after being turned off for a while

From Dev

RestHeart Deleting Specific JSON object from Collection

From Dev

Doesn't remove from list

From Dev

Entity Framework: after SaveChanges returned object doesn't have children

From Dev

Collection property doesn't load from parent object, but loads when separately queried

From Dev

How to populate documents from other collection. Specific case

From Dev

Remove a collection from a collection

From Dev

remove specific href children jquery

From Dev

Object doesn't support property or method 'remove'

From Dev

Why doesn't NSMutableSet remove an object contained in it?

From Dev

remove specific item from a json object

From Dev

How to remove object with specific name from ArrayList

From Dev

How to remove a specific object from an array

From Dev

Remove specific element from array of specifc object

From Dev

PHP/SQL doesn't fetch all children from parent

From Dev

Removing element by EQ filtering doesn't remove the element from jQuery Object

From Dev

c# remove object from list if ID value doesn't exist on web link

Related Related

  1. 1

    How to remove a specific object from a List<T>

  2. 2

    How to remove a specific object from a List<T>

  3. 3

    Remove Children from Json object

  4. 4

    Allow_Remove doesn't remove the collection

  5. 5

    How to remove children objects recursively from object?

  6. 6

    How to remove children objects recursively from object?

  7. 7

    simplexmlelement object doesn't list all children

  8. 8

    Remove item from Object set that doesn't exist in query set

  9. 9

    destroying object doesn't remove delegate from invocation list

  10. 10

    Mongoose Model.remove(callback) doesn't remove anything from my collection

  11. 11

    HashSet doesn't remove an object

  12. 12

    Hibernate: Join table with extra columns, remove children from one side

  13. 13

    garbage collection doesn't remove unreachable object after being turned off for a while

  14. 14

    RestHeart Deleting Specific JSON object from Collection

  15. 15

    Doesn't remove from list

  16. 16

    Entity Framework: after SaveChanges returned object doesn't have children

  17. 17

    Collection property doesn't load from parent object, but loads when separately queried

  18. 18

    How to populate documents from other collection. Specific case

  19. 19

    Remove a collection from a collection

  20. 20

    remove specific href children jquery

  21. 21

    Object doesn't support property or method 'remove'

  22. 22

    Why doesn't NSMutableSet remove an object contained in it?

  23. 23

    remove specific item from a json object

  24. 24

    How to remove object with specific name from ArrayList

  25. 25

    How to remove a specific object from an array

  26. 26

    Remove specific element from array of specifc object

  27. 27

    PHP/SQL doesn't fetch all children from parent

  28. 28

    Removing element by EQ filtering doesn't remove the element from jQuery Object

  29. 29

    c# remove object from list if ID value doesn't exist on web link

HotTag

Archive