Note that there are some explanatory texts on larger screens.

plurals
  1. POJPA Best Practice to update an Entity with Collections
    primarykey
    data
    text
    <p>I am using JPA in a Glassfish Container. I have the following Modell (not complete)</p> <pre><code>@Entity public class Node { @Id private String serial; @Version @Column(updatable=false) protected Integer version; private String name; @ManyToMany(cascade = {CascadeType.PERSIST,CascadeType.MERGE}) private Set&lt;LUN&gt; luns = new HashSet&lt;LUN&gt;(); @Entity public class LUN { @Id private String wwid; @Version @Column(updatable=false) protected Integer version; private String vendor; private String model; private Long capacity; @ManyToMany(mappedBy = "luns") private Set&lt;Node&gt; nodes = new HashSet&lt;Node&gt;(); </code></pre> <p>This information will be updated daily. Now my question is, what is the best practice to do this.</p> <p>My fist approach was, I generate the Node Objects on the client (with LUNs) every day new, and merge it to the Database (I wanted to let JPA do the work) via service.</p> <p>Now I did some tests without LUNs yet. I have the following service in a stateless EJB:</p> <pre><code>public void updateNode(Node node) { if (!nodeInDB(node)) { LOGGER.log(Level.INFO, "persisting node {0} the first time", node.toString()); em.persist(node); } else { LOGGER.log(Level.INFO, "merging node {0}", node.toString()); node = em.merge(node); } } </code></pre> <p>The test:</p> <pre><code>@Test public void addTest() throws Exception { Node node = new Node(); node.setName("hostname"); node.setSerial("serial"); nodeManager.updateNode(node); nodeManager.updateNode(node); node.setName("newhostname"); nodeManager.updateNode(node); } </code></pre> <p>This works without the @Version Field. With the @Version field I get an OptimisticLockException.</p> <p>Is that the wrong approach? Do I have to always perform an em.find(...) and then modify the managed entity via getter and setter?</p> <p>Any help is appreciated.</p> <p>BR Rene</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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