0

I am having problem trying to execute a SQL/HQL statement using the WHERE clause.

The application connects and executes this method when called without errors:

public void listPlayers( ){
      Session session = sessionFactory.openSession();
      Transaction tx = null;
      try{
         tx = session.beginTransaction();
         List players = session.createQuery("FROM Player").list(); 
         for (Iterator iterator = 
                           players.iterator(); iterator.hasNext();){
            Player player = (Player) iterator.next(); 


           String tmpMessage = player.getFirstName().toString();
           System.out.println(tmpMessage);
              writer.write("First Name: "+player.getPlayerName()+", Wealth: "+player.getWealth()+"\r\n");
             writer.flush();

         }
         tx.commit();
      }catch (HibernateException e) {
         if (tx!=null) tx.rollback();
         e.printStackTrace(); 
      }finally {
         session.close(); 
      }
   }

BUT, when I try to do this; I get an error: player_name field not found:

      public void loadPlayer(String usr){
      Session session = sessionFactory.openSession();


      Transaction tx = null;
      try{
         tx = session.beginTransaction();
         String hql = "SELECT * FROM Player P WHERE P.player_name = "+usr;
         Query query = session.createQuery(hql); 
         List players = query.list();
         for (Iterator iterator = 
                           players.iterator(); iterator.hasNext();){
            Player player = (Player) iterator.next(); 

            System.out.print("  Location: " + player.getLocation()); 
            System.out.println("  Wealth: " + player.getWealth()); 
         }
         tx.commit();
      }catch (HibernateException e) {
         if (tx!=null) tx.rollback();
         e.printStackTrace(); 
      }finally {
         session.close(); 
      }
   }

My mySQL database has a Table named Player, with pID, player_name, location, wealth as column values

I am attempting to pass playername to the loadPlayer(String username) method so it can look up all information associated with the player and write back to socket. What am I doing wrong here?

1
  • also the issue is i think your trying to map the table column name "player_name" whereas you need to put the actual java class instance variable name when writing your HQL. Also you need to pass in parameters as highlighted by quartzde. You cannot simply concatenate strings as writing RAW sql. Commented Jul 28, 2013 at 13:33

1 Answer 1

1

i think your query is not correct. try something like this

String hql = "SELECT * FROM Player P WHERE P.playerName = :playerName";         
Query query = session.createQuery(hql); 
query.setParameter("playerName", usr);
Sign up to request clarification or add additional context in comments.

3 Comments

I trust your correct too. You need to define either as named parameters or placeholders(i.e : the question marks) to pass in your external parameters. You cant just append them like normal SQL.
sorry i responded to wrong person saying it was wrong, what you mentioned did work, but I am getting a ClassCastException
try SELECT P FROM Player P WHERE P.playerName = :playerName

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.