有没有办法迭代Stateful Bean中所有列出的EntityManager?
@Stateful
@Local(SomeInterface.class)
public class ContextBean implements SomeInterface{
@PersistenceContext(unitName="00")
private EntityManager em00;
@PersistenceContext(unitName="01")
private EntityManager em01;
...
@PersistenceContext(unitName="0n")
private EntityManager em0n;
public ContextBean() {}
public void test(){
for(EntityManager em : ???){
// Do something
}
}
}
我可以使用循环吗?请
答案 0 :(得分:2)
当然,您可以迭代每个字段,如下所示:
Field[] fields = getClass().getDeclaredFields();
for (Field field : fields) {
if (field.getType().equals(EntityManager.class)) {
EntityManager em = (EntityManager) field.get(this);
// do something with em
}
}