Note that there are some explanatory texts on larger screens.

plurals
  1. POJPA behaviour
    text
    copied!<p>I have some trouble understanding a JPA behaviour. Mabye someone could give me a hint. Situation:</p> <p>Product entity:</p> <pre><code>@Entity public class Product implements Serializable { ... @OneToMany(mappedBy="product", fetch=FetchType.EAGER) private List&lt;ProductResource&gt; productResources = new ArrayList&lt;ProductResource&gt;(); .... public List&lt;ProductResource&gt; getProductResources() { return productResources; } public boolean equals(Object obj) { if (obj == this) return true; if (obj == null) return false; if (!(obj instanceof Product)) return false; Product p = (Product) obj; return p.productId == productId; } } </code></pre> <p>Resource entity:</p> <pre><code>@Entity public class Resource implements Serializable { ... @OneToMany(mappedBy="resource", fetch=FetchType.EAGER) private List&lt;ProductResource&gt; productResources = new ArrayList&lt;ProductResource&gt;(); ... public void setProductResource(List&lt;ProductResource&gt; productResource) { this.productResources = productResource; } public List&lt;ProductResource&gt; getProductResources() { return productResources; } public boolean equals(Object obj) { if (obj == this) return true; if (obj == null) return false; if (!(obj instanceof Resource)) return false; Resource r = (Resource) obj; return (long)resourceId==(long)r.resourceId; } } </code></pre> <p>ProductResource Entity: This is a JoinTable (association class) with additional properties (amount). It maps Product and Resources.</p> <pre><code>@Entity public class ProductResource implements Serializable { ... @JoinColumn(nullable=false, updatable=false) @ManyToOne(fetch=FetchType.EAGER, cascade=CascadeType.PERSIST) private Product product; @JoinColumn(nullable=false, updatable=false) @ManyToOne(fetch=FetchType.EAGER, cascade=CascadeType.PERSIST) private Resource resource; private int amount; public void setProduct(Product product) { this.product = product; if(!product.getProductResources().contains((this))){ product.getProductResources().add(this); } } public Product getProduct() { return product; } public void setResource(Resource resource) { this.resource = resource; if(!resource.getProductResources().contains((this))){ resource.getProductResources().add(this); } } public Resource getResource() { return resource; } ... public boolean equals(Object obj) { if (obj == this) return true; if (obj == null) return false; if (!(obj instanceof ProductResource)) return false; ProductResource pr = (ProductResource) obj; return (long)pr.productResourceId == (long)productResourceId; } } </code></pre> <p>This is the Session Bean (running on glassfish).</p> <pre><code>@Stateless(mappedName="PersistenceManager") public class PersistenceManagerBean implements PersistenceManager { @PersistenceContext(unitName = "local_mysql") private EntityManager em; public Object create(Object entity) { em.persist(entity); return entity; } public void delete(Object entity) { em.remove(em.merge(entity)); } public Object retrieve(Class entityClass, Long id) { Object entity = em.find(entityClass, id); return entity; } public void update(Object entity) { em.merge(entity); } } </code></pre> <p>I call the session Bean from a java client:</p> <pre><code>public class Start { public static void main(String[] args) throws NamingException { PersistenceManager pm = (PersistenceManager) new InitialContext().lookup("java:global/BackITServer/PersistenceManagerBean"); ProductResource pr = new ProductResource(); Product p = new Product(); Resource r = new Resource(); pr.setProduct(p); pr.setResource(r); ProductResource pr_stored = (ProductResource) pm.create(pr); pm.delete(pr_stored); Product p_ret = (Product) pm.retrieve(Product.class, pr_stored.getProduct().getProductId()); // prints out true ???????????????????????????????????? System.out.println(p_ret.getProductResources().contains(pr_stored)); } } </code></pre> <p>So here comes my problem. Why is the ProductResource entity still in the List productResources(see code above). The productResource tuple in the db is gone after the deletion and I do newly retrieve the Product entity. If I understood right every method call of the client happens in a new persistence context, but here i obviously get back the non-refreshed product object!?</p> <p>Any help is appreciated</p> <p>Thanks</p> <p>Marcel</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload