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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T14:25:29+00:00 2026-05-20T14:25:29+00:00

Here are the sequences leading to the question : I have a Team record,

  • 0

Here are the sequences leading to the question :

  1. I have a Team record, and 3 Player records in the database. Team entity has a List that is using FetchType.LAZY, CascadeType.ALL
  2. The search button on the webui is clicked
  3. Query in the server side using JPA query is invoked, finding all the Team records, in this case, only 1 record of the team entity returned from the query (which has a proxy of the list of player entities)
  4. Map this teamEntity to a DTO, and return this DTO to the webui, skipping the mapping of the list of player entities
  5. Webui renders the DTO in a html form, ready to receive modifications from the user
  6. User modifies the team’s properties, like the date of when it’s founded
  7. The save button on the webui is clicked
  8. Converting the DTO to the team entity, to be used to update the already existing team record
  9. But in this case, if i were to use the em.merge(teamEntity), the team record will be updated, but what will happen to the list of players ? Because when converting from DTO to the team entity, the teamEntity has an empty list of players entities. And after merging, i notice that the teamEntity has 0 size of the details. But after refreshing that entity, em.refresh(teamEntity), it will return 3 of the detail size.

Im confused on :

  1. Why is it the size is 0 after merged ? It’s like not representing the record anymore
  2. Before doing the test, i was thinking that the details will be removed since im merging a teamEntity with an empty detail.

Please enlighten me 🙂

Thank you !

  • 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-20T14:25:30+00:00Added an answer on May 20, 2026 at 2:25 pm

    JPA Specification says:

    The semantics of the merge operation
    applied to an entity X are as follows:

    • If X is a detached entity, the state
      of X is copied onto a pre-existing
      managed entity instance X’ of the same
      identity or a new managed copy X’ of X
      is created.

    • If X is a new entity
      instance, a new managed entity
      instance X’ is created and the state
      of X is copied into the new managed
      entity instance X’.

    • If X is a
      removed entity instance, an
      IllegalArgumentException will be
      thrown by the merge operation (or the
      transaction commit will fail).

    • If X
      is a managed entity, it is ignored by
      the merge operation, however, the
      merge operation is cascaded to
      entities referenced by relationships
      from X if these relationships have
      been annotated with the cascade
      element value cascade=MERGE or
      cascade=ALL annotation.

    • For all
      entities Y referenced by relationships
      from X having the cascade element
      value cascade=MERGE or cascade=ALL, Y
      is merged recursively as Y’. For all
      such Y referenced by X, X’ is set to
      reference Y’. (Note that if X is
      managed then X is the same object as
      X’.)

    • If X is an entity merged to X’,
      with a reference to another entity Y,
      where cascade=MERGE or cascade=ALL is
      not specified, then navigation of the
      same association from X’ yields a
      reference to a managed object Y’ with
      the same persistent identity as Y.

    As you can see, there is no magic here. The state of detached instance is copied into the newly created managed instance. Since your detached instance has an empty list, managed instance would have it too.

    Further behaviour depends on ownership of relationship, since representation in the database reflects the owning side of relationship:

    • If Team is the owning side, relationships between Team and Players will be destroyed during flush (but Player itself would survive unless you have orphanRemoval = true on your relationship).
    • Otherwise having the empty list in Team doesn’t affect the database.

    If you refresh the Team before flushing the context, all properties of Team are rewritten by values from the database, therefore list of Players is restored (since the empty list of players wasn’t flushed yet).

    If you call flush() before calling refresh(), and Team is the owning side, list will be empty, since destruction of relationships was propagated to the database during flush().

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

Sidebar

Related Questions

No related questions found

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.