Note that there are some explanatory texts on larger screens.

plurals
  1. POJPA managed entities vs JavaFX properties
    text
    copied!<p>My current project is done using JavaFX. I use properties to bind (bidirectionnal) view fields to bean (with BeanPathAdapter of JFXtras).</p> <p>I choose to use JPA with ObjectDB as model.</p> <p>This is the first time I use JPA in a standalone project and here I'm facing the problem of managed entities.</p> <p>Actually, I bind managed entities to view fields and when the value of a view field changes, the entities is updated... and the database also.</p> <p>I'm trying to find a way to manually persist/merge an entity so I can ask the user if he wants to save or not.</p> <p>Here's the code i use to get list :</p> <pre><code>EntityManagerFactory emf = Persistence.createEntityManagerFactory("$objectdb/data/db.odb"); EntityManager em = emf.createEntityManager(); List&lt;XXX&gt; entities = em.createQuery("SELECT x FROM XXX x").getResultList(); </code></pre> <p>So when i do</p> <pre><code>entity.setName("test"); </code></pre> <p>the entity is updated in the database.</p> <p>What i'm looking for is that the entity doesn't update automatically.</p> <p>I tried (just after the getResultList)</p> <pre><code>em.clear(); </code></pre> <p>or</p> <pre><code>em.detach(entity); </code></pre> <p>but it looses the relations instances even with CascadeType.DETACH.</p> <p>I also tried</p> <pre><code>em.setFlushMode(FlushModeType.COMMIT); </code></pre> <p>but it still updates automatically...</p> <p>I also tried to clone the object. But when i want to merge it, it gives me an exception :</p> <pre><code>Attempt to reuse an existing primary key value </code></pre> <p>I thought an alternative solution : use a variable as 'buffer' and fill the managed bean with buffer if the user saves. But BeanPathAdapter looses its sense. It's the same as filling view fields manually and filling bean fields manually after saving.</p> <p>Could you help me to find a solution ?</p> <p>Thanks,</p> <p>Smoky</p> <p><strong>EDIT:</strong> I answer to my own question :p</p> <p>After 3 hours of research, I found a solution.</p> <p>The 'cloning' solution was the 'best' of each I quoted but I don't think it's the best one.</p> <p>The cause of the exception was the code I used to persist/merge my entity. I was persisting an entity non-managed with an already existing id. I thought I was merging...</p> <p>I did a generic method not to fail again</p> <pre><code>public &lt;T extends IEntity&gt; T persist(T object) { em.getTransaction().begin(); if (object.getId() == null) { em.persist(object); em.flush(); em.getTransaction().commit(); em.refresh(object); } else { object = em.merge(object); em.getTransaction().commit(); } return object; } </code></pre> <p>So the solution : When I have to bind the entity to the view, I use entity.clone() so I can use the entity as non-managed and merge when I want.</p> <p>But if you have a proper solution, i'm interested :)</p> <p>Thanks again</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