What I'm doing is just creating one Object and trying to save it to the database.
public O create(O o) {
em.getTransaction().begin();
em.persist(o);
em.getTransaction().commit();
em.refresh(o);
return o;
}
this is the entity class:
@Entity
@Table(name = "o")
public class O implements Serializable {
private static final long serialVersionUID = 1L;
private int id;
@ManyToOne(cascade=CascadeType.REFRESH, fetch = FetchType.LAZY)
@JoinColumn(name = "b_id")
private B b;
@OneToMany(fetch = FetchType.EAGER , cascade = CascadeType.ALL)
@JoinTable(name="o_p",
joinColumns=@JoinColumn(name="o_id", referencedColumnName="id"),
inverseJoinColumns=@JoinColumn(name="p_id", referencedColumnName="id"))
private List<P> p;
What happens... :
DataService<O> das = new ODataService();
O o = new O();
das.create(O);
The object is created in the database and the transaction ends but on refresh(o) the Entity manager is querying O where id=0 but the O id is auto incremented to some value that is not 0. So query by 0 is returning nothing and EntityNotFoundException is thrown. Why refresh is searching for object with id 0? May be the object o is not updated in the cache and the EntityManager still thinks that this object is with id 0;