I have two entities working in Master-Detail pattern like this:
The pattern works as expected with LOCAL RESOURCE, but when i try to move the example to a web environment GlassFish4.1 (JSF) with JTA, i get the following error:
Warning: DTX5014: Caught exception in beforeCompletion() callback: java.lang.NullPointerException at entities.OrderItemPK._persistence_set(OrderItemPK.java)
this source code works
public static void main(String[] args) {
factory = Persistence.createEntityManagerFactory(PERSISTENCE_UNIT_NAME);
EntityManager em = factory.createEntityManager();
em.getTransaction().begin();
// Fill the items filds
OrderItem item = new OrderItem();
item.setItemDesc("Item Text");
// Fill the orders fields
CustomerOrder order = new CustomerOrder();
order.setOrderText("Order text");
// Fill the relationship fields
order.getOrderItemCollection().add(item);
item.setCustomerOrder(order);
em.persist(order);
em.getTransaction().commit();
}
this source code doesn't work
public void CreateOrder(){
// Fill the items filds
OrderItem item = new OrderItem();
item.setItemDesc("Item Text");
// Fill the orders fields
CustomerOrder order = new CustomerOrder();
order.setOrderText("Order text");
// Fill the relationship fields
order.getOrderItemCollection().add(item);
item.setCustomerOrder(order);
// Save the changes
this.create(order) ;
}
The source code for persistence class of web enviroment:
import java.util.List;
import javax.persistence.Query;
import javax.persistence.EntityManager;
import java.util.List;
import javax.persistence.Query;
import javax.persistence.EntityManager;
public abstract class AbstractFacade<T> {
private Class<T> entityClass;
protected abstract EntityManager getEntityManager();
public AbstractFacade(Class<T> entityClass) {
this.entityClass = entityClass;
}
public void create(T entity) {
getEntityManager().persist(entity);
}
public T edit(T entity) {
return getEntityManager().merge(entity);
}
public void remove(T entity) {
getEntityManager().remove(getEntityManager().merge(entity));
}
... more code
I tried with both methods:
getEntityManager().persist(entity);
and
getEntityManager().merge(entity);
I think the problem has to be the type of datasource, because in a java application works and in glassfish does not.
Any idea what I'm doing wrong, suggestions are welcome.