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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T04:14:21+00:00 2026-06-13T04:14:21+00:00

I’m trying to Update an Entity (EntityObjectContainigList) that points to a list. I fetch

  • 0

I’m trying to Update an Entity (EntityObjectContainigList) that points to a list. I fetch the Entity from the DB, detach it, remove some elements from the list that the Entity points to and then try to merge the updated Entity with the one residing in the DB.

Now the problem is that when I run em.merge(EntityObjectContainigList) the Objects/Entitys that where referenced by the list (the ones that I removed) are still stay in DB.

@Entity
public class EntityObjectContainigList {

    @Id
//    @GeneratedValue(strategy=GenerationType.IDENTITY)
    private Long id;

    @OneToMany(mappedBy = "eocl", cascade = CascadeType.ALL, fetch = FetchType.EAGER, orphanRemoval = true)
    List<ContainerObject> list = new ArrayList<ContainerObject>();

    private String name;


    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }


    public List<ContainerObject> getList() {
        return list;
    }

    public void setList(List<ContainerObject> list) {
        this.list = new ArrayList<ContainerObject>(list);
    }


}

the ContainerObject looks like that:

@Entity
public class ContainerObject {

     @Id
     @GeneratedValue(strategy=GenerationType.IDENTITY)
     @JsonIgnore
     private Key id;

    @ManyToOne(fetch = FetchType.LAZY)
    EntityObjectContainigList eocl;


    private String s;

    private int i;


    public String getS() {
            return s;
    }

    public void setS(String s) {
        this.s = s;
    }

    public int getI() {
        return i;
    }

    public void setI(int i) {
        this.i = i;
    }


}

the servlet looks like that:

@Path("/db")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public class MyResourcse {

    private static final EntityManagerFactory emf = Persistence.createEntityManagerFactory("transactions-optional");

    @PUT
    public Response putInDb(EntityObjectContainigList d) {
        EntityManager em = null;
        try {
            em = emf.createEntityManager();
/*          Gson g = new Gson();
            System.out.println(g.toJson(d));*/
            em.persist(d);
            return Response.ok().build();
        } finally {
            if (em != null && em.isOpen()) em.close();
        }
    }

    @GET
    public Response getFromDb(@QueryParam("id")Long id) {
        EntityManager em = null;
        try {
            em = emf.createEntityManager();
/*          Gson g = new Gson();*/
            EntityObjectContainigList o = em.find(EntityObjectContainigList.class, id);
/*          System.out.println(g.toJson(o));*/
            return Response.ok(o).build();
        } finally {
            if (em != null && em.isOpen()) em.close();
        }
    }

    @POST
    public Response updateDb(EntityObjectContainigList d) {
        EntityManager em = null;
        try {
            em = emf.createEntityManager();
            Gson g = new Gson();
            d =  em.merge(d);
            System.out.println(g.toJson(d));
            return Response.ok().build();
        } finally {
            if (em != null && em.isOpen()) em.close();
        }
    }
}

Client side looks like that:

public class UpdateClient {

    public static void main(String[] args) {
        ClientConfig cc = new DefaultClientConfig();
        cc.getFeatures().put(JSONConfiguration.FEATURE_POJO_MAPPING, Boolean.TRUE);

        Client client = Client.create(cc);

        WebResource res = client.resource("http://localhost:8888/rest/db");


        EntityObjectContainigList o = new EntityObjectContainigList();
        o.setId(12L);
        o.setName("hello");

        for (int i = 0; i < 5; i++) {
            ContainerObject c = new ContainerObject();
            c.setI(i);
            c.setS("foo" + i );
            o.getList().add(c);
        }


        res.type(MediaType.APPLICATION_JSON).put(ClientResponse.class, o);

        System.out.println("Object inserted");

        o = res.queryParam("id", "12")
                .type(MediaType.APPLICATION_JSON)
                .accept(MediaType.APPLICATION_JSON)
                .get(ClientResponse.class)
                .getEntity(EntityObjectContainigList.class);

        System.out.println("Object fetched");


        o.getList().remove(1);
        o.getList().remove(0);
        o.setName("world");


        res.type(MediaType.APPLICATION_JSON).accept(MediaType.APPLICATION_JSON).post(ClientResponse.class, o);
        System.out.println("Object updated");

        System.out.println("Done.");
    }

}

I’m using GAE 1.7.2 with JPA 2.0.

Any help is much appreciated.
Vitaly

  • 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-13T04:14:22+00:00Added an answer on June 13, 2026 at 4:14 am

    How to fix:

    Set eocl to null for each element to be removed from list

    What is actually wrong:

    Two sides of bidirectional relationship,

    1. owning side (side pointed by mappedBy) and
    2. inverse side (one with mappedBy).

    are not equal, because database is modified based to the values in owning side only. In your case Field eocl in ContainerObject owns relationship. Because you modify only inverse side (list), there is nothing to persist. To persist changes, eocl should be modified. It is recommended to modify both sides, because otherwise object graph in memory is inconsistent.

    In JPA 2.0 specification this is told with following words:

    Relationships may be bidirectional or unidirectional. A bidirectional
    relationship has both an owning side and an inverse (non-owning) side.
    A unidirectional relationship has only an owning side. The owning side
    of a relationship determines the updates to the relationship in the
    database, as described in section 3.2.4.
    …
    Bidirectional relationships between managed entities will be persisted based on
    references held by the owning side of the relationship. It is the
    developer’s responsibility to keep the in-memory references held on
    the owning side and those held on the inverse side consistent with
    each other when they change.

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

Sidebar

Related Questions

For some reason, after submitting a string like this Jack’s Spindle from a text
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I'm trying to create an if statement in PHP that prevents a single post
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function

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.