简体   繁体   中英

How to check type of List of Users returned from Query object in Hibernate?

To retrieve data from database using Hibernate, I use the following syntax :

Query query = session.createQuery("from User where name=? and password=?");
query.setString(0,user.getName());
query.setString(1,user.getPassword());

List list = query.list();           // Line 1

Line 1 shows warning: "List is a raw type. References to generic type List should be parameterized."

Now, if I add generics using:

List<User> list = (List<User>) query.list();

This again gives warning: "Unchecked type conversion."

How do I check the type of List of Users?

I am not sure but seems okay to me .

You might want to use

@SuppressWarnings (value="unchecked")

The information about template parameter type is erased in runtime. This is called type erasure . So, you just have to know in advance the type you are casting to. To avoid compiler warning, you may use @SuppressWarnings("unchecked") .

Also, you may try the Hibernate Typesafe Criteria , have a look here .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM