JPA and NullPointerException when accessing to related entity, but randomly

93 Views Asked by At

I'm using Wildfly16 + Eclipselink2.6.0 and an EAR project. The database is Postgresql. I've some entities:

  • Group : a group for shops
  • Shop : a shop with n:1 relation with Group.
  • VAT : VAT information, n:1 with Group.

Theese are the entities (extract) :

@Entity
@Cacheable(value = false)
public class ShoppingCart {
    ...
    @JoinColumn(name = "idpv",referencedColumnName = "ID",nullable = false)
    @ManyToOne(fetch = FetchType.EAGER)
    @JsonSerialize(using = StandardJsonSerializer.class)
    private Shop shop;
    ...
}


@Entity
public class Shop implements Serializable {
    @ManyToOne(targetEntity = Group.class, fetch = FetchType.EAGER )
    @JoinColumn(name = "idgroup", referencedColumnName = "ID", nullable = false)
    private Group group;
}

@Entity
@Cacheable(false)
public class Group implements Serializable {
    ...
}

@Entity
public class VAT implements Serializable {

   @ManyToOne(fetch = FetchType.EAGER)
   @JoinColumn(name = "idgroup",referencedColumnName = "ID")
   private Group group;
}

This is the code:

@PersistenceContext(unitName = "MyPU")
private EntityManager em;

...

public void createInvoice(Long idcart){

ShoppingCart c = this.entitymanager.find(ShoppingCart.class, idcart);
if(c == null){
     throw new BssException("Cart not found");
}

Shop shop = c.getShop();
if(shop != null){
    LOG.log(Level.INFO, "Invoice for shop {0}", new Object[]{shop.getCode()});
    RowInvoice rowTrasp = new RowInvoice();
        ...
        
    List<VAT> lVat = this.em.createNamedQuery("VAT.findVATGroup", VAT.class)
                .setParameter("idgroup", shop.getGroup().getId()) // <- row 265
                .setParameter("vat", 22.00)
                .getResultList();
    ...
}
}

And this is the JPQL queries.

SELECT i FROM VAT i
WHERE i.group.id = :idgroup AND i.value = :vat

So : i want to get the VAT infos from a Group with a percentage value of 22.00%. I use the function when producing invoices.

I can make a lot of invoice but, starting from a random moment, I can't produce invoices anymore. If I restart Wildfly, without changing any of my data on database, the invoices starts to work.

The exception stacktrace is:

Caused by: java.lang.NullPointerException
    at com.bsssrl.bssspesaweb_ejb.GeneratoreFatture.creaTestataDocumentoStd(GeneratoreFatture.java:265)
    at sun.reflect.GeneratedMethodAccessor790.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)

I don't change group, shop or vat, they are static info (not updated since 2018). Each day I make a lot of carts and invoices with theese data.

Why the same records with the same method throw that error and after restarting works? I suppose that may be cache problem. How can I verify that?

It's not a database connection problem because the other operations work fine. The same data are used for making shopping-cart. All the other queries work.

0

There are 0 best solutions below