2011-08-17 15 views
10

sono di fronte eccezione: org.hibernate.hql.ast.QuerySyntaxException: Student6 non è mappato [da Stud Stud6] il mio nome tabella è Student6 in sql server il nome della classe di database e pojo è Studente.org.hibernate.hql.ast.QuerySyntaxException: tablename non mappato

public static void main(String[] args) { 
    Configuration configuration = new Configuration(); 
    SessionFactory sessionFactory = configuration.configure().buildSessionFactory(); 
    Session session = sessionFactory.openSession(); 
    try { 
     String SQL_QUERY ="from Student6 stud"; 
      Query query = session.createQuery(SQL_QUERY); 
      for(Iterator it=query.iterate();it.hasNext();)    { 
      Object[] row = (Object[]) it.next(); 
      System.out.println("STUDENT_ID: " + row[0]); 
      System.out.println("STUDENT_NAME: " + row[1]); 
      System.out.println("ADDRESS_STREET: " + row[2]); 
      System.out.println("ADDRESS_CITY: " + row[3]); 
      System.out.println("ADDRESS_STATE: " + row[4]); 
      System.out.println("ADDRESS_ZIPCODE: " + row[5]);        } 

    } catch (HibernateException e) { 
     transaction.rollback(); 
     e.printStackTrace(); 
    } finally { 
     session.close(); 
    } 
} 
+0

È necessario aggiungere i mapping – ssedano

risposta

32

La tua query non è una query SQL. È una query HQL. Pertanto, non dovrebbe utilizzare nomi di tabelle, ma nomi di classi di entità (from Student anziché from Student6). E non restituirà righe sotto forma di istanze Object[], ma restituirà istanze di entità.

Hibernate è un ORM: un Object Relational Mapper. L'idea è di usare oggetti piuttosto che dati relazionali. Dovresti rileggere lo Hibernate reference manual.

+0

Grazie JB Nizet. – user783160

+0

@JB Nizet - grazie per la buona risposta Sono stato troppo veloce per rispondere Non ho notato la differenza tra "Studente" e "Studente6" :) –

+0

Grazie, ho cercato e cercato e questa è stata la risposta. – Fabii

Problemi correlati