使用hibernate的时候会用到hql语句查询数据库, 那就一定会用到query.list();这个方法, 那就一定会出现一个长长的黄色的警告线, 不管你想尽什么办法, 总是存在, 虽然说这个黄色的警告并无碍于程序的运行, 但是对于我这样有着强烈强迫症的人来说我看不下去了.
查了一些资料, 找到办法了
原来的
public List<Object> list(String className) {
Configuration conf = new Configuration().configure("conf/hibernate.cfg.xml");
ServiceRegistry serviceReg = new StandardServiceRegistryBuilder().applySettings(conf.getProperties()).build();
sessionFactory = conf.buildSessionFactory(serviceReg);
session = sessionFactory.openSession(); String hql = "from "+className+" where yxbz='1'";
System.out.println(hql);
Query query = session.createQuery(hql);
List<Object> list = query.list(); session.close();
sessionFactory.close();
return list;
}
改成这样的:
public List<?> list(String className) {
Configuration conf = new Configuration().configure("conf/hibernate.cfg.xml");
ServiceRegistry serviceReg = new StandardServiceRegistryBuilder().applySettings(conf.getProperties()).build();
sessionFactory = conf.buildSessionFactory(serviceReg);
session = sessionFactory.openSession(); String hql = "from "+className+" where yxbz='1'";
System.out.println(hql);
Query query = session.createQuery(hql);
List<?> list = query.list(); session.close();
sessionFactory.close();
return list;
}
原因是query.list();方法返回的是一个List<?>泛型, 而原来每次这样写的时候都写成List<Object>, 所以出现这黄色的警告线, 实际上List<Object>和List<?>是没啥毛线区别的