lcmetzger 29 de mar. de 2004
“alex.lopes”:
Estou apanhando pra fazer um select simples no hibernate…
dentro do meu DAO está assim
public List listagem ( String query ) {
Session sessao ;
try {
sessao = factory .openSession () ;
List lista = sessao .find ( query ) ;
sessao .flush () ;
sessao .close () ;
return lista ;
} catch ( HibernateException e ) {
e .printStackTrace () ;
}
return null ;
}
e na minha classe eu chamo assim
ArrayList lista = new ArrayList ( dao . listagem ( "Select Codigo from CLIENTES as clientes" ));
mas não funciona, gera uma exceção
net . sf . hibernate . QueryException : unexpected token : as [ Select Codigo from CLIENTES as clientes ]
at net . sf . hibernate . hql . FromParser . token ( FromParser . java : 94 )
at net . sf . hibernate . hql . ClauseParser . token ( ClauseParser . java : 87 )
at net . sf . hibernate . hql . PreprocessingParser . token ( PreprocessingParser . java : 123 )
at net . sf . hibernate . hql . ParserHelper . parse ( ParserHelper . java : 29 )
at net . sf . hibernate . hql . QueryTranslator . compile ( QueryTranslator . java : 149 )
at net . sf . hibernate . hql . QueryTranslator . compile ( QueryTranslator . java : 138 )
at net . sf . hibernate . impl . SessionFactoryImpl . getQuery ( SessionFactoryImpl . java : 293 )
at net . sf . hibernate . impl . SessionImpl . getQueries ( SessionImpl . java : 1530 )
at net . sf . hibernate . impl . SessionImpl . find ( SessionImpl . java : 1501 )
at net . sf . hibernate . impl . SessionImpl . find ( SessionImpl . java : 1491 )
at net . sf . hibernate . impl . SessionImpl . find ( SessionImpl . java : 1483 )
at br . com . junior . database . DAO . listagem ( DAO . java : 58 )
at br . com . junior . cadastro . Principal . listagem ( Principal . java : 26 )
at br . com . junior . cadastro . Principal .( Principal . java : 22 )
at br . com . junior . cadastro . Principal . main ( Principal . java : 46 )
java . lang . NullPointerException
at java . util . ArrayList .( ArrayList . java : 132 )
at br . com . junior . cadastro . Principal . listagem ( Principal . java : 26 )
at br . com . junior . cadastro . Principal .( Principal . java : 22 )
at br . com . junior . cadastro . Principal . main ( Principal . java : 46 )
Exception in thread “ main ”
Existem dados no Banco de Dados, as outras operações de gravar, excluir estão OK, só falta mesmo fazer uma simulação de uma Query Language no Hibernate
Tenta assim:
ArrayList lista = new ArrayList ( dao . listagem ( "from CLIENTES" ));
alex.lopes 29 de mar. de 2004
eu tentei isso, mas não funcionou
agora gerou outra exceção
< blockquote >
WARNING : SQL Error : 335544569 , SQLState : null
29 / 03 / 2004 14 : 02 : 38 net . sf . hibernate . util . JDBCExceptionReporter logExceptions
SEVERE : GDS Exception . Dynamic SQL Error
SQL error code = - 104
Token unknown - line 1 , char 9
from
29 / 03 / 2004 14 : 02 : 38 net . sf . hibernate . util . JDBCExceptionReporter logExceptions
WARNING : SQL Error : 335544569 , SQLState : null
29 / 03 / 2004 14 : 02 : 38 net . sf . hibernate . util . JDBCExceptionReporter logExceptions
SEVERE : GDS Exception . Dynamic SQL Error
SQL error code = - 104
Token unknown - line 1 , char 9
from
29 / 03 / 2004 14 : 02 : 38 net . sf . hibernate . JDBCException
SEVERE : Could not execute query
org . firebirdsql . jdbc . FBSQLException : GDS Exception . Dynamic SQL Error
SQL error code = - 104
Token unknown - line 1 , char 9
from
at org . firebirdsql . jdbc . FBPreparedStatement . ( FBPreparedStatement . java : 82 )
at org . firebirdsql . jdbc . FBConnection . prepareStatement ( FBConnection . java : 244 )
at net . sf . hibernate . impl . BatcherImpl . getPreparedStatement ( BatcherImpl . java : 249 )
at net . sf . hibernate . impl . BatcherImpl . getPreparedStatement ( BatcherImpl . java : 223 )
at net . sf . hibernate . impl . BatcherImpl . prepareQueryStatement ( BatcherImpl . java : 65 )
at net . sf . hibernate . loader . Loader . prepareQueryStatement ( Loader . java : 704 )
at net . sf . hibernate . loader . Loader . doQuery ( Loader . java : 185 )
at net . sf . hibernate . loader . Loader . doQueryAndInitializeNonLazyCollections ( Loader . java : 133 )
at net . sf . hibernate . loader . Loader . doList ( Loader . java : 950 )
at net . sf . hibernate . loader . Loader . list ( Loader . java : 941 )
at net . sf . hibernate . hql . QueryTranslator . list ( QueryTranslator . java : 834 )
at net . sf . hibernate . impl . SessionImpl . find ( SessionImpl . java : 1512 )
at net . sf . hibernate . impl . SessionImpl . find ( SessionImpl . java : 1491 )
at net . sf . hibernate . impl . SessionImpl . find ( SessionImpl . java : 1483 )
at br . com . junior . database . DAO . listagem ( DAO . java : 58 )
at br . com . junior . cadastro . Principal . listagem ( Principal . java : 26 )
at br . com . junior . cadastro . Principal . ( Principal . java : 22 )
at br . com . junior . cadastro . Principal . main ( Principal . java : 46 )
at org . firebirdsql . gds . GDSException : Dynamic SQL Error
SQL error code = - 104
Token unknown - line 1 , char 9
from
at org . firebirdsql . jgds . GDS_Impl . readStatusVector ( GDS_Impl . java : 1698 )
at org . firebirdsql . jgds . GDS_Impl . receiveResponse ( GDS_Impl . java : 1651 )
at org . firebirdsql . jgds . GDS_Impl . isc_dsql_prepare ( GDS_Impl . java : 1162 )
at org . firebirdsql . jca . FBManagedConnection . prepareSQL ( FBManagedConnection . java : 767 )
at org . firebirdsql . jdbc . FBConnection . prepareSQL ( FBConnection . java : 1110 )
at org . firebirdsql . jdbc . FBStatement . prepareFixedStatement ( FBStatement . java : 998 )
at org . firebirdsql . jdbc . FBPreparedStatement . prepareFixedStatement ( FBPreparedStatement . java : 700 )
at org . firebirdsql . jdbc . FBPreparedStatement . ( FBPreparedStatement . java : 79 )
at org . firebirdsql . jdbc . FBConnection . prepareStatement ( FBConnection . java : 244 )
at net . sf . hibernate . impl . BatcherImpl . getPreparedStatement ( BatcherImpl . java : 249 )
at net . sf . hibernate . impl . BatcherImpl . getPreparedStatement ( BatcherImpl . java : 223 )
at net . sf . hibernate . impl . BatcherImpl . prepareQueryStatement ( BatcherImpl . java : 65 )
at net . sf . hibernate . loader . Loader . prepareQueryStatement ( Loader . java : 704 )
at net . sf . hibernate . loader . Loader . doQuery ( Loader . java : 185 )
at net . sf . hibernate . loader . Loader . doQueryAndInitializeNonLazyCollections ( Loader . java : 133 )
at net . sf . hibernate . loader . Loader . doList ( Loader . java : 950 )
at net . sf . hibernate . loader . Loader . list ( Loader . java : 941 )
at net . sf . hibernate . hql . QueryTranslator . list ( QueryTranslator . java : 834 )
at net . sf . hibernate . impl . SessionImpl . find ( SessionImpl . java : 1512 )
at net . sf . hibernate . impl . SessionImpl . find ( SessionImpl . java : 1491 )
at net . sf . hibernate . impl . SessionImpl . find ( SessionImpl . java : 1483 )
at br . com . junior . database . DAO . listagem ( DAO . java : 58 )
at br . com . junior . cadastro . Principal . listagem ( Principal . java : 26 )
at br . com . junior . cadastro . Principal . ( Principal . java : 22 )
at br . com . junior . cadastro . Principal . main ( Principal . java : 46 )
net . sf . hibernate . JDBCException : Could not execute query
at net . sf . hibernate . impl . SessionImpl . find ( SessionImpl . java : 1515 )
at net . sf . hibernate . impl . SessionImpl . find ( SessionImpl . java : 1491 )
at net . sf . hibernate . impl . SessionImpl . find ( SessionImpl . java : 1483 )
at br . com . junior . database . DAO . listagem ( DAO . java : 58 )
at br . com . junior . cadastro . Principal . listagem ( Principal . java : 26 )
at br . com . junior . cadastro . Principal . ( Principal . java : 22 )
at br . com . junior . cadastro . Principal . main ( Principal . java : 46 )
Caused by : org . firebirdsql . jdbc . FBSQLException : GDS Exception . Dynamic SQL Error
SQL error code = - 104
Token unknown - line 1 , char 9
from
at org . firebirdsql . jdbc . FBPreparedStatement . ( FBPreparedStatement . java : 82 )
at org . firebirdsql . jdbc . FBConnection . prepareStatement ( FBConnection . java : 244 )
at net . sf . hibernate . impl . BatcherImpl . getPreparedStatement ( BatcherImpl . java : 249 )
at net . sf . hibernate . impl . BatcherImpl . getPreparedStatement ( BatcherImpl . java : 223 )
at net . sf . hibernate . impl . BatcherImpl . prepareQueryStatement ( BatcherImpl . java : 65 )
at net . sf . hibernate . loader . Loader . prepareQueryStatement ( Loader . java : 704 )
at net . sf . hibernate . loader . Loader . doQuery ( Loader . java : 185 )
at net . sf . hibernate . loader . Loader . doQueryAndInitializeNonLazyCollections ( Loader . java : 133 )
at net . sf . hibernate . loader . Loader . doList ( Loader . java : 950 )
at net . sf . hibernate . loader . Loader . list ( Loader . java : 941 )
at net . sf . hibernate . hql . QueryTranslator . list ( QueryTranslator . java : 834 )
at net . sf . hibernate . impl . SessionImpl . find ( SessionImpl . java : 1512 )
… 6 more
java . lang . NullPointerException
at java . util . ArrayList . ( ArrayList . java : 132 )
at br . com . junior . cadastro . Principal . listagem ( Principal . java : 26 )
at br . com . junior . cadastro . Principal . ( Principal . java : 22 )
at br . com . junior . cadastro . Principal . main ( Principal . java : 46 )
Exception in thread “ main ” </ blockquote >
lcmetzger 29 de mar. de 2004
Seria interessante você configurar o arquivo hibernate.properties para mostrar as sentenças SQL que o hibernate está gerando. Procure por hibernate.show_sql e mude para true.
Veja o que acontece…
alex.lopes 29 de mar. de 2004
Hibernate: select from
29/03/2004 15:29:16 net.sf.hibernate.util.JDBCExceptionReporter logExceptions
WARNING: SQL Error: 335544569, SQLState: null
29/03/2004 15:29:16 net.sf.hibernate.util.JDBCExceptionReporter logExceptions
SEVERE: GDS Exception. Dynamic SQL Error
SQL error code = -104
Token unknown - line 1, char 9
from
estranho que nao pegou o nome CLIENTES
ficou apenas select from…
lcmetzger 29 de mar. de 2004
“alex.lopes”:
Hibernate: select from
29/03/2004 15:29:16 net.sf.hibernate.util.JDBCExceptionReporter logExceptions
WARNING: SQL Error: 335544569, SQLState: null
29/03/2004 15:29:16 net.sf.hibernate.util.JDBCExceptionReporter logExceptions
SEVERE: GDS Exception. Dynamic SQL Error
SQL error code = -104
Token unknown - line 1, char 9
from
estranho que nao pegou o nome CLIENTES
ficou apenas select from…
Mostre o mapeamento, pode existir algo errado no mesmo.
alex.lopes 29 de mar. de 2004
<?xml version=“1.0”?>
<!DOCTYPE hibernate-mapping PUBLIC
“-//Hibernate/Hibernate Mapping DTD 1.1//EN”
“http://hibernate.sourceforge.net/hibernate-mapping-1.1.dtd ”>
< hibernate - mapping >
< class
name = “ br . com . junior . database . Clientes ”
table = “ CLIENTES ”
>
<id
name= "codigo"
column= "codigo"
type= "java.lang.Integer"
>
< generator class="assigned">
< /generator>
</id>
< property
name="endereco"
type="java.lang.String"
column="endereco"
/>
< property
name="nome"
type="java.lang.String"
column="nome"
/>
< !--
To add non XDoclet property mappings, create a file named
hibernate-properties-Clientes.xml
containing the additional properties and place it in your merge dir.
-->
< /class>
</hibernate-mapping>
para as outras operações funciona normalmente
lcmetzger 29 de mar. de 2004
Percebi que você está utilizando a versão 1.1 do hibernate, seria melhor utilizar a versão 2.0.
Vi também que não existe um mapeamento para ID, veja um exemplo meu:
<id
name= "id"
column= "ID"
type= "java.lang.Long"
unsaved-value= "null"
>
<generator class= "native" >
<param name= "sequence" > SQ_FAC</param>
</generator>
</id>
Veja se não é algo neste sentido.
Jair_Rillo_Junior 29 de mar. de 2004
Alex,
não sei se isso é o problema, mas quando você trabalha com Hibernate Query Language você deve pensar em Objetos invés de Tabelas quando se trabalha com SQL. Isso significa que o hibernate não conhece a tabela CLIENTES e sim o seu Objeto Clientes, então dentro da sua query, você tem que passar o caminho da onde está o objeto, olhando seu arquivo hbm.xml, eu vi que está dentro de br.com.junior.database.Clientes, então a sua query deveria ficar assim
ArrayList lista = new ArrayList(dao.listagem("from br.com.junior.database.Clientes"));
Acredito que seja isso, pois o hibernate não está encontrando seu objeto Clientes
alex.lopes 30 de mar. de 2004
O problema era esse mesmo ManchesteR, coloquei o caminho inteiro da minha classe Clientes e funcionou :lol: :lol: :lol: :lol:
Muito Obrigado