Re: Unable to read after commit () with JPA
Thank you for your reply Robert.
I try to describe the whole picture:
I have 2 JFrame
- one is for CRUD operations on Offer (and for transforming master/
details offer into master/details order), lets call it frmOfferEditor
- one is for searching Order created by frmOfferEditor, lets call it
frmOrderSearch
/* frmOfferEditor */
//transform
The code I posted before it's the code I'm using on frmEditor when I
need transforming master/details offer into master/details order.
//CRUD
The code I use for CRUD operations uses an EM called entityManager.
Here is the code:
//CRUD - Create
try {
entityManager.getTransaction().rollback();
entityManager.getTransaction().begin();
offerta = new offerta.Offerte();
numOfferta = 0;
Object res = entityManager.createQuery("SELECT MAX(o.numOfferta) FROM
Offerte o WHERE o.anno = :anno").setParameter("anno",
anno).getSingleResult();
if (res != null) {
numOfferta = (Integer) res;
}
numOfferta++;
offerta.setNumOfferta(numOfferta);
entityManager.persist(offerta);
} catch (Exception ex) {
ex.printStackTrace();
}
// CRUD - Save
try {
entityManager.getTransaction().commit();
entityManager.getTransaction().begin();
} catch (RollbackException rex) {
rex.printStackTrace();
entityManager.getTransaction().begin();
List<offerta.Offerte> merged = new
ArrayList<offerta.Offerte>(list.size());
for (offerta.Offerte o : list) {
merged.add(entityManager.merge(o));
}
list.clear();
list.addAll(merged);
}
/* frmOrderSearch */
try {
entityManager.getTransaction().rollback();
entityManager.getTransaction().begin();
String query = ....
Query q = entityManager.createQuery(query).setParameter(.....)
java.util.Collection data = q.getResultList();
list.clear();
list.addAll(data);
} catch (Exception ex) {
ex.printStackTrace();
}
I hope that now it's clear