Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I belive the behaviour you observe is correct. When you use GenerationType.IDENTITY or GenerationType.SEQUENCE strategy, the id value is not known before the row is inserted. </p> <p>If you need that value right after persisting entity, you have to call EntityManager.flush() to force synchronization with database and then retrive your object - it will have the id set at that time. </p> <p>You may also try to use HQL query, not find method</p> <pre><code>Query q = em.createQuery("select e from Employee where e.id = :id"); q.setId(employeeNumber); return q.getSingleResult() </code></pre> <p>Another problem might arise if you use EJB with remote interface. Assuming the following flow</p> <ol> <li><p>On the client side you call EmployeeManagementBean.create(), then you get Employee object on the client side, lets call it e1.</p></li> <li><p>On the client side you call EmployeeManagementBean.store(e1)</p></li> <li><p>On the client side you call e1.getEmployeeNumber() and you get 0</p></li> </ol> <p>the problem is the following. When you call EmployeeManagementBean.store(e1) the copy of the e1 object is passed to EJB method, the object is modified on the server side, but the original object on the client side does not. This is because during remote EJBs calls arguments are passed to server as a serialized copies of the original objects. </p> <p>So remote changes on the server side (e.g. changing id) are not seen on client side.</p> <p>In order to overcome this you can change store method as follows:</p> <pre><code>public Employee store(Employee employee) { entityManager.persist(employee); entityManager.flush(); return employee; //employee should have id set to the DB value } </code></pre>
    singulars
    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. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      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