E ai pessoal,
Seguinte quero fazer uma busca com hibernate porem não quero trazer um objeto só quero trazer um atributo,
no meu caso quero faze uma consulta com hibernate para ver o numero de registros de uma tabela.
quando executo o select na mão direto no banco funciona, foi quando percebi que da forma que estou fazendo não é possivel trazer so um atributo que gera uma exception…
Alguem tem uma luz, Obrigado…
Obs: a consulta tem que ser com hibernate nem que seja com nativeQuery
@Override
public Integer selectByNumberRecords() throws Exception {
EntityManager mananger = factory.createEntityManager();
try {
String classe = classEntity.getSimpleName();
String busca = "select count(*) from " + classe;
Query q = mananger.createNativeQuery(busca, classEntity);
return (Integer) q.getSingleResult();
} catch (Exception e) {
return null;
} finally {
if (mananger != null) {
mananger.close();
}
}
}
Valeu
qmx
Dezembro 2, 2009, 12:16am
#2
qual exceção que está sendo lançada?
[quote=andrejusti]run:
02/12/2009 01:16:52 org.hibernate.cfg.annotations.Version <clinit>
INFO: Hibernate Annotations 3.3.1.GA
02/12/2009 01:16:52 org.hibernate.cfg.Environment <clinit>
INFO: Hibernate 3.2.5
02/12/2009 01:16:52 org.hibernate.cfg.Environment <clinit>
INFO: hibernate.properties not found
02/12/2009 01:16:52 org.hibernate.cfg.Environment buildBytecodeProvider
INFO: Bytecode provider name : cglib
02/12/2009 01:16:52 org.hibernate.cfg.Environment <clinit>
INFO: using JDK 1.4 java.sql.Timestamp handling
02/12/2009 01:16:52 org.hibernate.ejb.Version <clinit>
INFO: Hibernate EntityManager 3.3.2.GA
02/12/2009 01:16:53 org.hibernate.cfg.AnnotationBinder bindClass
INFO: Binding entity from annotated class: br.com.dutatec.util.genericDAO.entity.Link
02/12/2009 01:16:53 org.hibernate.cfg.annotations.EntityBinder bindTable
INFO: Bind entity br.com.dutatec.util.genericDAO.entity.Link on table LINK
02/12/2009 01:16:53 org.hibernate.cfg.AnnotationBinder bindClass
INFO: Binding entity from annotated class: br.com.dutatec.util.genericDAO.entity.Usuario
02/12/2009 01:16:53 org.hibernate.cfg.annotations.EntityBinder bindTable
INFO: Bind entity br.com.dutatec.util.genericDAO.entity.Usuario on table USUARIO
02/12/2009 01:16:53 org.hibernate.cfg.AnnotationConfiguration secondPassCompile
INFO: Hibernate Validator not found: ignoring
02/12/2009 01:16:53 org.hibernate.connection.DriverManagerConnectionProvider configure
INFO: Using Hibernate built-in connection pool (not for production use!)
02/12/2009 01:16:53 org.hibernate.connection.DriverManagerConnectionProvider configure
INFO: Hibernate connection pool size: 20
02/12/2009 01:16:53 org.hibernate.connection.DriverManagerConnectionProvider configure
INFO: autocommit mode: true
02/12/2009 01:16:53 org.hibernate.connection.DriverManagerConnectionProvider configure
INFO: using driver: com.mysql.jdbc.Driver at URL: jdbc:mysql://localhost/daogenerico
02/12/2009 01:16:53 org.hibernate.connection.DriverManagerConnectionProvider configure
INFO: connection properties: {user=root, password=****, autocommit=true, release_mode=auto}
02/12/2009 01:16:53 org.hibernate.cfg.SettingsFactory buildSettings
INFO: RDBMS: MySQL, version: 5.1.35-community
02/12/2009 01:16:53 org.hibernate.cfg.SettingsFactory buildSettings
INFO: JDBC driver: MySQL-AB JDBC Driver, version: mysql-connector-java-5.1.6 ( Revision: ${svn.Revision} )
02/12/2009 01:16:53 org.hibernate.dialect.Dialect <init>
INFO: Using dialect: org.hibernate.dialect.MySQLDialect
02/12/2009 01:16:53 org.hibernate.transaction.TransactionFactoryFactory buildTransactionFactory
INFO: Transaction strategy: org.hibernate.transaction.JDBCTransactionFactory
02/12/2009 01:16:53 org.hibernate.transaction.TransactionManagerLookupFactory getTransactionManagerLookup
INFO: No TransactionManagerLookup configured (in JTA environment, use of read-write or transactional second-level cache is not recommended)
02/12/2009 01:16:53 org.hibernate.cfg.SettingsFactory buildSettings
INFO: Automatic flush during beforeCompletion(): disabled
02/12/2009 01:16:53 org.hibernate.cfg.SettingsFactory buildSettings
INFO: Automatic session close at end of transaction: disabled
02/12/2009 01:16:53 org.hibernate.cfg.SettingsFactory buildSettings
INFO: JDBC batch size: 15
02/12/2009 01:16:53 org.hibernate.cfg.SettingsFactory buildSettings
INFO: JDBC batch updates for versioned data: disabled
02/12/2009 01:16:53 org.hibernate.cfg.SettingsFactory buildSettings
INFO: Scrollable result sets: enabled
02/12/2009 01:16:53 org.hibernate.cfg.SettingsFactory buildSettings
INFO: JDBC3 getGeneratedKeys(): enabled
02/12/2009 01:16:53 org.hibernate.cfg.SettingsFactory buildSettings
INFO: Connection release mode: auto
02/12/2009 01:16:53 org.hibernate.cfg.SettingsFactory buildSettings
INFO: Maximum outer join fetch depth: 2
02/12/2009 01:16:53 org.hibernate.cfg.SettingsFactory buildSettings
INFO: Default batch fetch size: 1
02/12/2009 01:16:53 org.hibernate.cfg.SettingsFactory buildSettings
INFO: Generate SQL with comments: disabled
02/12/2009 01:16:53 org.hibernate.cfg.SettingsFactory buildSettings
INFO: Order SQL updates by primary key: disabled
02/12/2009 01:16:53 org.hibernate.cfg.SettingsFactory buildSettings
INFO: Order SQL inserts for batching: disabled
02/12/2009 01:16:53 org.hibernate.cfg.SettingsFactory createQueryTranslatorFactory
INFO: Query translator: org.hibernate.hql.ast.ASTQueryTranslatorFactory
02/12/2009 01:16:53 org.hibernate.hql.ast.ASTQueryTranslatorFactory <init>
INFO: Using ASTQueryTranslatorFactory
02/12/2009 01:16:53 org.hibernate.cfg.SettingsFactory buildSettings
INFO: Query language substitutions: {}
02/12/2009 01:16:53 org.hibernate.cfg.SettingsFactory buildSettings
INFO: JPA-QL strict compliance: enabled
02/12/2009 01:16:53 org.hibernate.cfg.SettingsFactory buildSettings
INFO: Second-level cache: enabled
02/12/2009 01:16:53 org.hibernate.cfg.SettingsFactory buildSettings
INFO: Query cache: disabled
02/12/2009 01:16:53 org.hibernate.cfg.SettingsFactory createCacheProvider
INFO: Cache provider: org.hibernate.cache.NoCacheProvider
02/12/2009 01:16:53 org.hibernate.cfg.SettingsFactory buildSettings
INFO: Optimize cache for minimal puts: disabled
02/12/2009 01:16:53 org.hibernate.cfg.SettingsFactory buildSettings
INFO: Structured second-level cache entries: disabled
02/12/2009 01:16:53 org.hibernate.cfg.SettingsFactory buildSettings
INFO: Statistics: disabled
02/12/2009 01:16:53 org.hibernate.cfg.SettingsFactory buildSettings
INFO: Deleted entity synthetic identifier rollback: disabled
02/12/2009 01:16:53 org.hibernate.cfg.SettingsFactory buildSettings
INFO: Default entity-mode: pojo
02/12/2009 01:16:53 org.hibernate.cfg.SettingsFactory buildSettings
INFO: Named query checking : enabled
02/12/2009 01:16:53 org.hibernate.impl.SessionFactoryImpl <init>
INFO: building session factory
02/12/2009 01:16:53 org.hibernate.impl.SessionFactoryObjectFactory addInstance
INFO: Not binding factory to JNDI, no JNDI name configured
02/12/2009 01:16:53 org.hibernate.tool.hbm2ddl.SchemaUpdate execute
INFO: Running hbm2ddl schema update
02/12/2009 01:16:53 org.hibernate.tool.hbm2ddl.SchemaUpdate execute
INFO: fetching database metadata
02/12/2009 01:16:53 org.hibernate.tool.hbm2ddl.SchemaUpdate execute
INFO: updating schema
02/12/2009 01:16:54 org.hibernate.tool.hbm2ddl.TableMetadata <init>
INFO: table found: daogenerico.link
02/12/2009 01:16:54 org.hibernate.tool.hbm2ddl.TableMetadata <init>
INFO: columns: [id, link, horacadastro, nome, datacadastro]
02/12/2009 01:16:54 org.hibernate.tool.hbm2ddl.TableMetadata <init>
INFO: foreign keys: []
02/12/2009 01:16:54 org.hibernate.tool.hbm2ddl.TableMetadata <init>
INFO: indexes: [primary]
02/12/2009 01:16:54 org.hibernate.tool.hbm2ddl.TableMetadata <init>
INFO: table found: daogenerico.usuario
02/12/2009 01:16:54 org.hibernate.tool.hbm2ddl.TableMetadata <init>
INFO: columns: [site, complemento, datanascimento, horacadastro, sexo, senha, id, cidade, estado, sobrenome, email, datacadastro, nome]
02/12/2009 01:16:54 org.hibernate.tool.hbm2ddl.TableMetadata <init>
INFO: foreign keys: []
02/12/2009 01:16:54 org.hibernate.tool.hbm2ddl.TableMetadata <init>
INFO: indexes: [primary]
02/12/2009 01:16:54 org.hibernate.tool.hbm2ddl.SchemaUpdate execute
INFO: schema update complete
02/12/2009 01:16:54 org.hibernate.type.NullableType nullSafeGet
INFO: could not read column value from result set: id; Column 'id' not found.
02/12/2009 01:16:54 org.hibernate.util.JDBCExceptionReporter logExceptions
WARNING: SQL Error: 0, SQLState: S0022
02/12/2009 01:16:54 org.hibernate.util.JDBCExceptionReporter logExceptions
SEVERE: Column 'id' not found.
null
CONSTRUÍDO COM SUCESSO (tempo total: 3 segundos)
VALEU ;D
É possivel não ter nenhum id em algum registro teu ?!
tem sim, tem uns 20 registros, problema está eu acho com a forma que estou pegando o retorno acredito que exista outro metodo ou algo do tipo que retorna so um parametro, e não o objeto inteiro
andrejusti:
tem sim, tem uns 20 registros, problema está eu acho com a forma que estou pegando o retorno acredito que exista outro metodo ou algo do tipo que retorna so um parametro, e não o objeto inteiro
Tenta criar uma Query que retorna só o teu id por exemplo! ou uma lista com todos teus registros…
e Dps tu faz num list o teu contador pra ver qtos registros tem… não é mais fácil?!