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

  • Home
  • SEARCH
  • 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 7708429
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T00:36:36+00:00 2026-06-01T00:36:36+00:00

I have a parent object Compound with a one-to-many relationship to an object Submission.

  • 0

I have a parent object Compound with a one-to-many relationship to an object Submission. I have set up the relationship as follows:

@Entity
public class Compound implements Serializable {

    @Id
    private long compoundId;

    ...

    @OneToMany(mappedBy="compound", fetch=FetchType.LAZY,
        cascade={ CascadeType.PERSIST, CascadeType.REFRESH, CascadeType.REMOVE })
    @PrivateOwned
    private List<Submission> submissions;

    ...
}

@Entity
public class Submission implements Serializable {

    @Id
    private long submissionId;

    ...

    @ManyToOne(fetch=FetchType.LAZY,
        cascade={ CascadeType.PERSIST, CascadeType.REFRESH, CascadeType.REMOVE })
    @JoinColumn(name="compoundId")
    private Compound compound;

    ...
}

I have set-up a form (in Flex) to modify a selected Compound. Given the form output (a BlazeDS class called ObjectProxy, which extends HashMap) I map the values from the form onto a new Compound object. The submissions data is not stored in the form hence the submissions list on the new Compound object remains null.

public Compound decodeWebForm(final ObjectProxy proxy) {
    // create a new compound object
    final Compound c = new Compound();

    // map the proxy values onto the new compound
    c.setCompoundId(proxy.get("compoundId"));
    ...

    return updateCompound(c);
}

Next I merge the Compound object I have constructed into the persistence context.

public Compound updateCompound(final Compound c) {
    // retrieve the entity manager
    final EntityManager em = getEntityManager();

    // begin a transaction
    em.getTransaction().begin();

    // merge changes to the managed compound
    final Compound managed = em.merge(c);

    // commit changes to the database
    em.getTransaction().commit();

    return managed;
}

I have specifically omitted the CascadeType.MERGE directive from the relationship definitions. As such, when I call the EntityManager.merge(Compound) method I was expecting the lack of submissions data to be ignored. However, this is not the case and any submissions relating to the Compound I am modifying are deleted.

I must be doing something wrong or have misunderstood the meaning of CascadeType.MERGE? Can someone please help?

Thanks
James

  • 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-06-01T00:36:37+00:00Added an answer on June 1, 2026 at 12:36 am

    Two problems:
    1) You marked the relationship with @PrivateOwned which should cause all dereferenced entities to also be deleted.
    2) you are merging an empty collection. Cascade merge (or the lack there of) only means that the merge will (or will not be) cascaded to the Submissions entities. The state of the collection itself though is considered part of the Compound and so needs to be updated in the database. Coupled with #1, this means that Submissions will be deleted. IF not for #1, there would be no change in the database (since Submissions own the relationhip and haven’t changed), but when reading back the Compound entity, it will show a null or empty collection unless it gets refreshed.

    Creating a new instance of the entity is a bad idea. Instead, you might want to read it in and only change what is needed from the form – then merge this entity into the transactional context if required.

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

Sidebar

Related Questions

I have a parent object which has a one to many relationship with an
I have a parent object which has a one-to-many relationship with an ISet of
I have one-to-many relationship between parent and child Java objects. The parent object uses
I have a Parent/Child object/mapping as follows: class Parent { int Id; string name;
I have a parent class which contains a child object. I am using set
I have an object where I maintain a relationship to another object: public class
If I have abstract class Parent { static object staticLock = new object(); public
I have a one-to-many relation between Parent and Child table. In the parent object
I have a parent-child view model object structure set up and need to update
I have a parent object with a version locking policy defined as follows: VersionLockingPolicy

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.