Hibernate: lazy collections and session.merge

VB_

What I have:

I need to update my entity. I use Hibernatee conversation, that means that we already have entity in the session cache.

public void handle(Request request, Session session) {
  MyEntity updatedEntity = request.getEntity();
  session.merge(updatedEntity); //rewrite all lazy collections
}

So I send object to client, client full up the object and send it back for update.

What the problem:

Lazy collections aren't sent to the client. As a result, if lazy collection hasn't been empty, than it will be overriden in session.merge(updatedEntity) string

It happens because client knows nothing about element in those collections

Question:

How two merge entity in correct way? Means without rewriting lazy collections.

EDIT:(how I work with my collections)

public class MyEntity {
  @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER, orphanRemoval = true)
  @JoinColumn(name = "entity_id")
  private List<AnotherEntity> anotherEntities;


  public void setAnotherEntities(List<AnotherEntity> anotherEntities) {
    // we must work with the same instance of list (again, because of orphanRemoval)
    this.anotherEntities.clear();
    this.anotherEntities.addAll(anotherEntities);
  }
}
mvb13

I think that CascadeType.ALL is the problem

You may use instead

 cascade = {CascadeType.PERSIST, CascadeType.REFRESH, CascadeType.DELETE})

You may also add to this set any other cascade option you need.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

Hibernate: best practice to pull all lazy collections

From Dev

Enable no-proxy behaviour for all FetchType.LAZY non-collections by default in Hibernate

From Dev

Should Hibernate Session#merge do an insert when receiving an entity with an ID?

From Dev

Using lazy for properties in Hibernate

From Dev

How to do a hibernate session.close() after showing JSP to avoid lazy=false

From Dev

Spring Hibernate Lazy Fetch collections transactions not working

From Dev

Lazy mapping with hibernate tools?

From Dev

Spring @Async: null hibernate session on LAZY collection

From Dev

Jackson JSON and lazy loaded collections

From Dev

Merge collections in oracle?

From Dev

Hibernate criteria with collections

From Dev

Hibernate OneToMany Lazy Fetching

From Dev

Hibernate FetchType.LAZY without session

From Dev

Initialize lazy collections

From Dev

Merge collections using update

From Dev

Hibernate and Jackson lazy serialization

From Dev

Spring Data JPA, Hibernate, @ManyToOne(fetch=FetchType.LAZY) and org.hibernate.LazyInitializationException: could not initialize proxy - no Session

From Dev

Merge and lazy loading

From Dev

Is there a way to verify that a Lazy loaded object is not in Hibernate session?

From Dev

Merge map of collections

From Dev

Using lazy for properties in Hibernate

From Dev

Should Hibernate Session#merge do an insert when receiving an entity with an ID?

From Dev

Lazy mapping with hibernate tools?

From Dev

Hibernate criteria with collections

From Dev

Session and session factory in hibernate

From Dev

Hibernate FetchType.LAZY without session

From Dev

Spring Hibernate Lazy loading - no session

From Dev

Avoiding loading of Lazy Collections in QueryDSL, Hibernate and Spring Data JPA

From Dev

Merge two collections in MongoDB

Related Related

  1. 1

    Hibernate: best practice to pull all lazy collections

  2. 2

    Enable no-proxy behaviour for all FetchType.LAZY non-collections by default in Hibernate

  3. 3

    Should Hibernate Session#merge do an insert when receiving an entity with an ID?

  4. 4

    Using lazy for properties in Hibernate

  5. 5

    How to do a hibernate session.close() after showing JSP to avoid lazy=false

  6. 6

    Spring Hibernate Lazy Fetch collections transactions not working

  7. 7

    Lazy mapping with hibernate tools?

  8. 8

    Spring @Async: null hibernate session on LAZY collection

  9. 9

    Jackson JSON and lazy loaded collections

  10. 10

    Merge collections in oracle?

  11. 11

    Hibernate criteria with collections

  12. 12

    Hibernate OneToMany Lazy Fetching

  13. 13

    Hibernate FetchType.LAZY without session

  14. 14

    Initialize lazy collections

  15. 15

    Merge collections using update

  16. 16

    Hibernate and Jackson lazy serialization

  17. 17

    Spring Data JPA, Hibernate, @ManyToOne(fetch=FetchType.LAZY) and org.hibernate.LazyInitializationException: could not initialize proxy - no Session

  18. 18

    Merge and lazy loading

  19. 19

    Is there a way to verify that a Lazy loaded object is not in Hibernate session?

  20. 20

    Merge map of collections

  21. 21

    Using lazy for properties in Hibernate

  22. 22

    Should Hibernate Session#merge do an insert when receiving an entity with an ID?

  23. 23

    Lazy mapping with hibernate tools?

  24. 24

    Hibernate criteria with collections

  25. 25

    Session and session factory in hibernate

  26. 26

    Hibernate FetchType.LAZY without session

  27. 27

    Spring Hibernate Lazy loading - no session

  28. 28

    Avoiding loading of Lazy Collections in QueryDSL, Hibernate and Spring Data JPA

  29. 29

    Merge two collections in MongoDB

HotTag

Archive