javajpacriteria-apifindbugs

FindBugs: How to avoid "Unwritten public field" warning when using JPA meta model?


I've written quite a lot of DAO class and using the JPA criteria API and its meta model in them, like in this example:

@Override
public EntityA findByEntityB(EntityB entityB) {
  CriteriaBuilder builder = this.getCriteriaBuilder();
  CriteriaQuery<EntityA> criteriaQuery = builder.createQuery(EntityA.class);
  Root<EntityA> root = criteriaQuery.from(EntityA.class);
  criteriaQuery.select(root);
  criteriaQuery.where(builder.and(builder.equal(root.get(EntityA_.entityB), entityB)));
  return this.findByCriteriaQuery(criteriaQuery);
}

While running static code analysis, FindBugs throws the following warning:

UWF_UNWRITTEN_PUBLIC_OR_PROTECTED_FIELD, Priorität: Normal

Unwritten public or protected field: EntityA_.entityB

No writes were seen to this public/protected field. All reads of it will return the default value. Check for errors (should it have been initialized?), or remove it if it is useless.

As I use the meta model classes in nearly all of my queries this warning is thrown very often.

Is there any useful way to avoid these warnings? As we all know the meta model classes are just generated and their attributs are never written.

I don't want to exclude the DAO classes from FindBugs sca as I want to check these to maybe find other possible bugs!


Solution

  • Here are some ideas:


    1 - The setter could potentially be declared as private. There is a good chance that FindBugs doesn't check that the setter is called.