0

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.

Daniele Licitra
  • 1,520
  • 21
  • 45
  • 2
    NPE in the mentioned line indicates that either shop or shop.group is null. Can you provide more details how the Shop and Group entities are defined and loaded? – Nowhere Man Jun 22 '20 at 16:48
  • I've added more code. The chart is read from the database with an entity manger – Daniele Licitra Jun 23 '20 at 11:17
  • Is this ShoppingCart a new entity you created in the code above? details on it, its mapping to shop, shop's mapping to group etc are more important than your vat query. How long are you keeping this EntityManager open for, what is loaded in it etc. that doesn't have a group into the graph, and this is getting into the cache. Once there, this is used as 'the' instance for all references to that shop until it is refreshed. You should fix how that is happening to prevent it from occuring, but could corect the issue it with a simple em.refresh call on it. – Chris Jun 25 '20 at 00:53
  • @Chris I added an em.refresh(shop) after get it from cart, i'm testing it. You can convert your comment to an answer ;) – Daniele Licitra Jul 09 '20 at 09:16

0 Answers0