FindBugs:如何避免& quot;书面公共领域& quot;使用JPA元模型时出现警告? [英] FindBugs: How to avoid "Unwritten public field" warning when using JPA meta model?
问题描述
我已经编写了很多DAO类,并在其中使用了JPA criteria API
及其元模型,例如以下示例:
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);
}
在运行静态代码分析时,FindBugs
会引发以下警告:
While running static code analysis, FindBugs
throws the following warning:
UWF_UNWRITTEN_PUBLIC_OR_PROTECTED_FIELD,优先级:正常
UWF_UNWRITTEN_PUBLIC_OR_PROTECTED_FIELD, Priorität: Normal
未写的公共或受保护字段:EntityA_.entityB
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.
我不想从FindBugs sca中排除DAO类,因为我想检查它们以发现其他可能的错误!
I don't want to exclude the DAO classes from FindBugs sca as I want to check these to maybe find other possible bugs!
推荐答案
以下是一些想法:
- 修改生成器以添加(冗余)setter.
- 实施FindBugs过滤器(请参见 http://findbugs.sourceforge.net/manual/filter .html )以排除特定类或程序包中的特定错误.或一般而言.
- Modify the generator to add a (redundant) setter.
- Implement a FindBugs filter (see http://findbugs.sourceforge.net/manual/filter.html) to exclude that specific bug in specific classes or packages. Or generally.
这篇关于FindBugs:如何避免& quot;书面公共领域& quot;使用JPA元模型时出现警告?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!