Security type: an expression of type List requires raw conversion to match List <Object []>

I always get a type safety warning when I want to run a Hibernate application. Is there any way to get rid of this without using @SuppressWarnings("unchecked") ?

here is my code:

 Configuration config = new Configuration(); config.addAnnotatedClass(Employee.class); config.configure("hibernate.cfg.xml"); new SchemaExport(config).create(false, false); ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder() .applySettings(config.getProperties()).build(); SessionFactory factory = config.buildSessionFactory(serviceRegistry); Session session = factory.getCurrentSession(); session.beginTransaction(); Query q = session .createQuery("SELECT e.empId,e.empName FROM Employee e"); @SuppressWarnings("unchecked") List<Object[]> list = q.list(); <-- here is the problem! 
+7
java types
source share
3 answers

Hibernate Session.list() returns a simple, raw List .

This is a completely legal Java syntax to pass to a parameterized collection ( List<Object[]> here). But because universal type information is destroyed at runtime, the compiler will issue a warning to let you know that it cannot guarantee that this cast will actually be valid. So this is just his way of telling you: "Hey, you play with fire here, I hope you know what you are doing, because I do not."

In this particular case, you cannot do anything to eliminate this warning, but you can take responsibility for ignoring it using the @SuppressWarnings annotation.

+11
source share

No, there is no way to remove it unless you make q.list () exactly a List<Object[]>

0
source share

You can force the cast to make a warning, but like suppressing the warning, it hides a potential problem, since q.list () does not guarantee the return of this exact type.

 List<Object[]> list = (List<Object[]>)q.list(); 
0
source share

All Articles