Question

I don't know if the tittle is clear, but I am going to try to make myself as clear as possible. This is my scenario, I have a class called User:

@Entity(name="User_table")
public class User {
    @Id @GeneratedValue
    int userId;

    String userName;

    String userMessage;


//    public User(String userName, String userMessage)
//      {
//          this.userName= userName;
//          this.userMessage = userMessage;
//      }
//  public User(){}

    public int getUserId() {
        return userId;
    }
    public void setUserId(int userId) {
        this.userId = userId;
    }
    public String getUserName() {
        return userName;
    }
    public void setUserName(String userName) {
        this.userName = userName;
    }
    public String getUserMessage() {
        return userMessage;
    }
    public void setUserMessage(String userMessage) {
        this.userMessage = userMessage;
    }

}

Related to that table:

  mysql> select * from User_table;
+--------+-------------+
| userId |  | userName |
+--------+-------------
|     71 | | Dani     |
|     72 | | Dani     |
+--------++------------
2 rows in set (0.00 sec)

And what I want is to select all the objects from User_table, where userName = Dani. So, what I do is this:

public List<Object> HotelesNombre(String name) {

        int i = 0;
        User user = null;
        SessionFactory sf = open();
        Session ss = sf.openSession();
        ss.beginTransaction();
        Query query = ss.createQuery("select userId, userName from User_table where userName = :id ");
        query.setParameter("id", name);
        List<Object> list = query.list();
        if (list.size() != 0) {
            ss.getTransaction().commit();
            ss.close();
            return list;
        } else {
            ss.getTransaction().commit();
            ss.close();
            return null;
        }
    }

As you can see, I use List instead of List because I am selecting a part of the object User (not the whole) and List wouldn't work.

So, for testing, in the main class I have:

public class HibernateMain {

    /**
     * @param args
     */
    public static void main(String[] args) {

        int status;

        int i=0;

        Manager manager = new Manager();

        status = manager.creaUsuario("Dani", "pollo");

        //User user = manager.buscaHotel(71);

        List<Object> list = manager.HotelesNombre("Dani");

        System.out.println(list.get(0).getUserName());

        if (list.size() >0){

            for (Object v : list){


                System.out.println(v.getUserName());
            i++;

            }



        }


//      if (user !=null){
//      
//      System.out.println(user.getUserName());
//      }

    }

}

I have problem in the lines: System.out.println(v.getUserName()); and System.out.println(v.getUserName()); I can't use the methods in the User Class as v is not a User, so.... how could I do what I want¿? Thank you very much.

Was it helpful?

Solution

What you get back for such a query is a List<Object[]>, i.e. a list which contains arrays of objects. Each array is a row returned by the query. And since the query selects userId and userName, each array contains two elements, the user ID (an Integer instance), and the user name (a String instance).

Note that, unless you have a huge number of columns, representing a massive amount of information (i.e. blobs), in this table, what you're doing is a premature optimization. You'd better simply use select u from User u where ..., and get back a List<User>.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top