Problemas com hibernate

2 respostas
T

Olá a todos!
Eu estou a quase 1 mês tentando fazer uma pequena aplicação em
java com hibernate usando o eclipse 3.0 e até agora só bati na trave.
Eu preciso muito de ajuda.

Para ficar mais fácil eu vou detalhar o que eu fiz.

Eu estou usando o banco de dados FireBird na raiz da máquina (C:\FSCONTROL.GDB).
No banco de dados eu tenho uma tabela chamada Contas com os seguintes campos:

ID_CONTAS INTEGER (Chave primária)
DESCRICAO VARCHAR(30)

Eu criei um projeto chamado hibernate no diretório C:\esclipse\worksapce\hibernate.

  • Criei uma classe Contas no diretório scr do meu projeto:
public class Contas {

private long id;

private String descricao;
public Contas() {}

public String getDescricao() {
	return descricao;
}
public void setDescricao(String descricao) {
	this.descricao = descricao;
}
public long getId() {
	return id;
}
public void setId(long id) {
	this.id = id;
}

}

  • Criei um xml Contas.hbm.xml no diretório scr do meu projeto:
<?xml version=“1.0”?>

<!DOCTYPE hibernate-mapping PUBLIC

“-//Hibernate/Hibernate Mapping DTD//EN”

“<a href="http://hibernate.sourceforge.net/hibernate-mapping.dtd">http://hibernate.sourceforge.net/hibernate-mapping.dtd</a>”>

<hibernate-mapping>

<class name=“Contas” table=“Contas”>

<id column=“ID_CONTAS” name=“id” type=“long”>

<generator class=“assigned”/>

</id>

<property column “DESCRICAO” name=“descricao” type=“string”/>

</class>

</hibernate-mapping>
  • Alterei e coloquei no diretório scr do meu projeto o xml hiberbate.cfg.xml:

<?xml version=“1.0”?>
<!DOCTYPE hibernate-configuration PUBLIC
“-//Hibernate/Hibernate Configuration DTD 3.0//EN”
http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd”>

<hibernate-configuration>

<session-factory name="/Fabrica01">

<property name=“hibernate.connection.url”>jdbc:firebirdsql:localhost:C:/FSCONTROL.GDB</property>

<property name=“hibernate.connection.username”>SYSDBA</property>

<property name=“hibernate.connection.password”>masterkey</property>

<property name=“hibernate.connection.driver_class”>org.firebirdsql.jdbc.FBDriver</property>

<property name=“hibernate.dialect”>net.sf.hibernate.dialect.InterbaseDialect</property>

<property name=“hibernate.show_sql”>true</property>

<mapping resource=“Contas.hbm.xml” />

<class-cache

class=“org.hibernate.test.Simple”

region=“Simple”

usage=“read-write”/>

</session-factory>

</hibernate-configuration>
  • Para fazer o teste criei uma classe teste ContasDao onde eu configuro e faço uma pequena consulta.
import net.sf.hibernate.HibernateException;

import net.sf.hibernate.Session;

import net.sf.hibernate.SessionFactory;

import net.sf.hibernate.cfg.Configuration;

public class ContasDao {

public static void main(String[] args) {
	try {
		// Cria uma configuração para a classe Produto
		Configuration cfg = new Configuration().addClass(Contas.class);
		
		SessionFactory factory = cfg.buildSessionFactory();
		Session session = factory.openSession();
		
		Contas encontrado = (Contas) session.load(Contas.class, new Long(1));
		
		System.out.println(encontrado.getDescricao());
		
		session.close();
		
		factory.close();
	} catch (HibernateException e) {
			e.printStackTrace();
	}
}

}

  • Copiei todos os aquivos da pasta lib do hibernate e o driver jdbc do firebird para a pasta lib do
    meu projeto.

  • Quando eu rodo a aplicação como Run/Java Application/Schema Export da o seuinte erro no console:

(cfg.Environment                     462 ) Hibernate 2.1.4

(cfg.Environment                     491 ) hibernate.properties not found

(cfg.Environment                     522 ) using CGLIB reflection optimizer

(cfg.Configuration                   347 ) Mapping resource: Contas.hbm.xml

(cfg.Binder                          229 ) Mapping class: Contas -> Contas

(cfg.Configuration                   613 ) processing one-to-many association mappings

(cfg.Configuration                   622 ) processing one-to-one association property references

(cfg.Configuration                   647 ) processing foreign key constraints

(cfg.SettingsFactory                 50  ) No dialect set - using GenericDialect: The dialect was not set. Set the property hibernate.dialect.

(dialect.Dialect                     82  ) Using dialect: net.sf.hibernate.dialect.GenericDialect

(cfg.SettingsFactory                 62  ) Use outer join fetching: false

(connection.UserSuppliedConnectionProvider 25  ) No connection properties specified - the user must supply JDBC connections

(transaction.TransactionManagerLookupFactory 33  ) No TransactionManagerLookup configured (in JTA environment, use of process level read-write cache is not recommended)

(cfg.SettingsFactory                 102 ) Use scrollable result sets: false

(cfg.SettingsFactory                 105 ) Use JDBC3 getGeneratedKeys(): false

(cfg.SettingsFactory                 108 ) Optimize cache for minimal puts: false

(cfg.SettingsFactory                 117 ) Query language substitutions: {}

(cfg.SettingsFactory                 128 ) cache provider: net.sf.ehcache.hibernate.Provider

(cfg.Configuration                   1093) instantiating and configuring caches

(impl.SessionFactoryImpl             119 ) building session factory

(impl.SessionFactoryObjectFactory    82  ) no JNDI name configured

Exception in thread main java.lang.UnsupportedOperationException: The user must supply a JDBC connection

at net.sf.hibernate.connection.UserSuppliedConnectionProvider.getConnection(UserSuppliedConnectionProvider.java:32)

at net.sf.hibernate.impl.BatcherImpl.openConnection(BatcherImpl.java:278)

at net.sf.hibernate.impl.SessionImpl.connect(SessionImpl.java:3302)

at net.sf.hibernate.impl.SessionImpl.connection(SessionImpl.java:3282)

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.loadEntity(Loader.java:836)

at net.sf.hibernate.loader.Loader.loadEntity(Loader.java:856)

at net.sf.hibernate.loader.EntityLoader.load(EntityLoader.java:59)

at net.sf.hibernate.loader.EntityLoader.load(EntityLoader.java:51)

at net.sf.hibernate.persister.EntityPersister.load(EntityPersister.java:419)

at net.sf.hibernate.impl.SessionImpl.doLoad(SessionImpl.java:2113)

at net.sf.hibernate.impl.SessionImpl.doLoadByClass(SessionImpl.java:1987)

at net.sf.hibernate.impl.SessionImpl.load(SessionImpl.java:1916)

at ContasDao.main(ContasDao.java:28)

Eu também tentei rodar a aplicação com as outras opções mas também dão erros.

Eu gostria que alguém me orientasse onde que eu errei no código ou se eu fiz um procedimento errado. Em fim me ajudem…

2 Respostas

W

Parece que ele não achou o hibernate.properties, aonde vc colocou ele.

T

Me disseram que eu poderia utilizar o hibernate.cfg.xml que também daria.
Mas de qualquer forma eu criei o hibernate.properties e o coloquei no diretório scr do meu projeto.
Parece que carregou mas o erro mudou. Acho que agora vai ficar mais fácil de me ajudar.

  • Conteúdo do hibernate.properties:
    hibernate.dialect net.sf.hibernate.dialect.InterbaseDialect
    hibernate.connection.username SYSDBA
    hibernate.connection.password masterkey
    hibernate.connection.driver_class org.firebirdsql.jdbc.FBDriver
    hibernate.connection.url jdbc:firebirdsql:localhost/3050:C:/FSCONTROL.GDB

  • Erro:
    
    (cfg.Environment                     462 ) Hibernate 2.1.4
    
    (cfg.Environment                     496 ) loaded properties from resource hibernate.properties: {hibernate.connection.username=SYSDBA, hibernate.connection.password=masterkey, hibernate.cglib.use_reflection_optimizer=true, hibernate.dialect=net.sf.hibernate.dialect.InterbaseDialect, hibernate.connection.url=jdbc:firebirdsql:localhost/3050:C:/FSCONTROL.GDB, hibernate.connection.driver_class=org.firebirdsql.jdbc.FBDriver}
    
    (cfg.Environment                     522 ) using CGLIB reflection optimizer
    
    (cfg.Configuration                   347 ) Mapping resource: Contas.hbm.xml
    
    (cfg.Binder                          229 ) Mapping class: Contas -> Contas
    
    (cfg.Configuration                   613 ) processing one-to-many association mappings
    
    (cfg.Configuration                   622 ) processing one-to-one association property references
    
    (cfg.Configuration                   647 ) processing foreign key constraints
    
    (dialect.Dialect                     82  ) Using dialect: net.sf.hibernate.dialect.InterbaseDialect
    
    (cfg.SettingsFactory                 62  ) Use outer join fetching: true
    
    (connection.DriverManagerConnectionProvider 42  ) Using Hibernate built-in connection pool (not for production use!)
    
    (connection.DriverManagerConnectionProvider 43  ) Hibernate connection pool size: 20
    
    (connection.DriverManagerConnectionProvider 77  ) using driver: org.firebirdsql.jdbc.FBDriver at URL: jdbc:firebirdsql:localhost/3050:C:/FSCONTROL.GDB
    
    (connection.DriverManagerConnectionProvider 78  ) connection properties: {user=SYSDBA, password=masterkey}
    
    (transaction.TransactionManagerLookupFactory 33  ) No TransactionManagerLookup configured (in JTA environment, use of process level read-write cache is not recommended)
    
    (cfg.SettingsFactory                 95  ) Could not obtain connection metadata
    
    java.sql.SQLException: Not yet implemented
    
    at org.firebirdsql.jdbc.FBDatabaseMetaData.supportsResultSetType(FBDatabaseMetaData.java:3678)
    
    at net.sf.hibernate.cfg.SettingsFactory.buildSettings(SettingsFactory.java:75)
    
    at net.sf.hibernate.cfg.Configuration.buildSettings(Configuration.java:1132)
    
    at net.sf.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:766)
    
    at ContasDao.main(ContasDao.java:25)
    
    (cfg.SettingsFactory                 102 ) Use scrollable result sets: false
    
    (cfg.SettingsFactory                 105 ) Use JDBC3 getGeneratedKeys(): false
    
    (cfg.SettingsFactory                 108 ) Optimize cache for minimal puts: false
    
    (cfg.SettingsFactory                 117 ) Query language substitutions: {}
    
    (cfg.SettingsFactory                 128 ) cache provider: net.sf.ehcache.hibernate.Provider
    
    (cfg.Configuration                   1093) instantiating and configuring caches
    
    (impl.SessionFactoryImpl             119 ) building session factory
    
    (impl.SessionFactoryObjectFactory    82  ) no JNDI name configured
    
    (util.JDBCExceptionReporter          38  ) SQL Error: 0, SQLState: null
    
    (util.JDBCExceptionReporter          46  ) fetch problem: org.firebirdsql.gds.GDSException: arithmetic exception, numeric overflow, or string truncation
    
    Cannot transliterate character between character sets
    
    (impl.BatcherImpl                    147 ) exception clearing maxRows/queryTimeout
    
    java.sql.SQLException: Not yet implemented
    
    at org.firebirdsql.jdbc.FBStatement.getMaxRows(FBStatement.java:277)
    
    at net.sf.hibernate.impl.BatcherImpl.closeQueryStatement(BatcherImpl.java:143)
    
    at net.sf.hibernate.impl.BatcherImpl.closeQueryStatement(BatcherImpl.java:103)
    
    at net.sf.hibernate.loader.Loader.doQuery(Loader.java:234)
    
    at net.sf.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:133)
    
    at net.sf.hibernate.loader.Loader.loadEntity(Loader.java:836)
    
    at net.sf.hibernate.loader.Loader.loadEntity(Loader.java:856)
    
    at net.sf.hibernate.loader.EntityLoader.load(EntityLoader.java:59)
    
    at net.sf.hibernate.loader.EntityLoader.load(EntityLoader.java:51)
    
    at net.sf.hibernate.persister.EntityPersister.load(EntityPersister.java:419)
    
    at net.sf.hibernate.impl.SessionImpl.doLoad(SessionImpl.java:2113)
    
    at net.sf.hibernate.impl.SessionImpl.doLoadByClass(SessionImpl.java:1987)
    
    at net.sf.hibernate.impl.SessionImpl.load(SessionImpl.java:1916)
    
    at ContasDao.main(ContasDao.java:28)
    
    (util.JDBCExceptionReporter          38  ) SQL Error: 0, SQLState: null
    
    (util.JDBCExceptionReporter          46  ) fetch problem: org.firebirdsql.gds.GDSException: arithmetic exception, numeric overflow, or string truncation
    
    Cannot transliterate character between character sets
    
    (util.JDBCExceptionReporter          38  ) could not load: [Contas#1]
    
    java.sql.SQLException: fetch problem: org.firebirdsql.gds.GDSException: arithmetic exception, numeric overflow, or string truncation
    
    Cannot transliterate character between character sets
    
    at org.firebirdsql.jdbc.FBResultSet$FBStatementFetcher.next(FBResultSet.java:2535)
    
    at org.firebirdsql.jdbc.FBResultSet.next(FBResultSet.java:157)
    
    at net.sf.hibernate.loader.Loader.doQuery(Loader.java:200)
    
    at net.sf.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:133)
    
    at net.sf.hibernate.loader.Loader.loadEntity(Loader.java:836)
    
    at net.sf.hibernate.loader.Loader.loadEntity(Loader.java:856)
    
    at net.sf.hibernate.loader.EntityLoader.load(EntityLoader.java:59)
    
    at net.sf.hibernate.loader.EntityLoader.load(EntityLoader.java:51)
    
    at net.sf.hibernate.persister.EntityPersister.load(EntityPersister.java:419)
    
    at net.sf.hibernate.impl.SessionImpl.doLoad(SessionImpl.java:2113)
    
    at net.sf.hibernate.impl.SessionImpl.doLoadByClass(SessionImpl.java:1987)
    
    at net.sf.hibernate.impl.SessionImpl.load(SessionImpl.java:1916)
    
    at ContasDao.main(ContasDao.java:28)
    
    net.sf.hibernate.JDBCException: could not load: [Contas#1]
    
    at net.sf.hibernate.persister.EntityPersister.load(EntityPersister.java:422)
    
    at net.sf.hibernate.impl.SessionImpl.doLoad(SessionImpl.java:2113)
    
    at net.sf.hibernate.impl.SessionImpl.doLoadByClass(SessionImpl.java:1987)
    
    at net.sf.hibernate.impl.SessionImpl.load(SessionImpl.java:1916)
    
    at ContasDao.main(ContasDao.java:28)
    
    Caused by: java.sql.SQLException: fetch problem: org.firebirdsql.gds.GDSException: arithmetic exception, numeric overflow, or string truncation
    
    Cannot transliterate character between character sets
    
    at org.firebirdsql.jdbc.FBResultSet$FBStatementFetcher.next(FBResultSet.java:2535)
    
    at org.firebirdsql.jdbc.FBResultSet.next(FBResultSet.java:157)
    
    at net.sf.hibernate.loader.Loader.doQuery(Loader.java:200)
    
    at net.sf.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:133)
    
    at net.sf.hibernate.loader.Loader.loadEntity(Loader.java:836)
    
    at net.sf.hibernate.loader.Loader.loadEntity(Loader.java:856)
    
    at net.sf.hibernate.loader.EntityLoader.load(EntityLoader.java:59)
    
    at net.sf.hibernate.loader.EntityLoader.load(EntityLoader.java:51)
    
    at net.sf.hibernate.persister.EntityPersister.load(EntityPersister.java:419)
    
     4 more
    
Criado 17 de março de 2005
Ultima resposta 17 de mar. de 2005
Respostas 2
Participantes 2