<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">org.postgresql.jdbc.Driver</property>
<property name="hibernate.connection.password">postgres</property>
<property name="hibernate.connection.url">jdbc:postgresql://localhost:5432/pesquisa</property>
<property name="hibernate.connection.username">postgres</property>
<property name="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</property>
<property name="hibernate.jdbc.batch_size">15</property>
<property name="hibernate.connection.driver_class">net.souceforge.jtds.jdbc.Driver</property>
<property name="hibernate.dialect">net.sf.hibernate.dialect.PostgreSQLDialect</property>
</session-factory>
</hibernate-configuration>
Hibernate properties
17 Respostas
De uma olhada no seu driver:
<property name="hibernate.connection.driver_class">net.souceforge.jtds.jdbc.Driver</property>
O correto não seria:
<property name="hibernate.connection.driver_class">net.souRceforge.jtds.jdbc.Driver</property>
Até mais
kra eu fiz um projetinho com hibernate e postgresql tb, posso te ajudar.
meu hibernate.properties. Está na src.
hibernate.dialect=org.hibernate.dialect.PostgreSQLDialect
hibernate.connection.driver_class=org.postgresql.Driver
hibernate.connection.url=jdbc:postgresql:farmacia
hibernate.connection.username= “seu usuario do banco”
hibernate.connection.password= “sua senha do banco”
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0dtd">
Assinatura do xml tá correta.
Estou tentando persistir via annotations
Mas está dando um erro ou um aviso?
Se vc setou o xml, ele passa o aviso de que não tem o properties.
Acho que o seu problema deve ser o drive jdbc que está setado errado, deveria ser esse: org.postgresql.Driver
Posta a exceção no log, fica mais fácil identifica o problema.
Ele lança essa INfo
76 [main] INFO org.hibernate.annotations.common.Version - Hibernate Commons Annotations 3.2.0.Final
81 [main] INFO org.hibernate.cfg.Environment - Hibernate 3.6.0.Final
82 [main] INFO org.hibernate.cfg.Environment - hibernate.properties not found
84 [main] INFO org.hibernate.cfg.Environment - Bytecode provider name : javassist
86 [main] INFO org.hibernate.cfg.Environment - using JDK 1.4 java.sql.Timestamp handling
129 [main] INFO org.hibernate.cfg.Configuration - configuring from resource: /hibernate.cfg.xml
129 [main] INFO org.hibernate.cfg.Configuration - Configuration resource: /hibernate.cfg.xml
E essa exceção.
Exception in thread “main” org.hibernate.HibernateException: Could not parse configuration: /hibernate.cfg.xml
at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:2216)
at org.hibernate.cfg.AnnotationConfiguration.doConfigure(AnnotationConfiguration.java:229)
at org.hibernate.cfg.AnnotationConfiguration.doConfigure(AnnotationConfiguration.java:70)
at org.hibernate.cfg.Configuration.configure(Configuration.java:2128)
at org.hibernate.cfg.AnnotationConfiguration.configure(AnnotationConfiguration.java:211)
at org.hibernate.cfg.AnnotationConfiguration.configure(AnnotationConfiguration.java:70)
at org.hibernate.cfg.Configuration.configure(Configuration.java:2107)
at org.hibernate.cfg.AnnotationConfiguration.configure(AnnotationConfiguration.java:205)
at tese.persistencia.RepositorioFuncionario.(RepositorioFuncionario.java:17)
at teste.TesteMain.main(TesteMain.java:9)
Caused by: org.dom4j.DocumentException: <a href="http://www.jboss.org/dtd/hibernate/hibernate-configuration-3.0dtd">http://www.jboss.org/dtd/hibernate/hibernate-configuration-3.0dtd</a> Nested exception: <a href="http://www.jboss.org/dtd/hibernate/hibernate-configuration-3.0dtd">http://www.jboss.org/dtd/hibernate/hibernate-configuration-3.0dtd</a>
at org.dom4j.io.SAXReader.read(SAXReader.java:484)
at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:2208)
… 9 more
Se o erro for no código meu repositório está dessa forma.
import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.cfg.Configuration;
import org.hibernate.classic.Session;
import teste.Funcionario;
public class RepositorioFuncionario {
SessionFactory sf;
public RepositorioFuncionario() {
sf = new AnnotationConfiguration().configure().buildSessionFactory();
}
public void inserirFuncionario(Funcionario f) {
Session session = sf.openSession();
session.save(f);
session.close();
}
}
O problema parece ser no xml, tenta assim:
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">org.postgresql.jdbc.Driver</property>
<property name="hibernate.connection.password">postgres</property>
<property name="hibernate.connection.url">jdbc:postgresql://localhost:5432/pesquisa</property>
<property name="hibernate.connection.username">postgres</property>
<property name="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</property>
<property name="hibernate.jdbc.batch_size">15</property>
</session-factory>
</hibernate-configuration>
Aparece isso agora
73 [main] INFO org.hibernate.annotations.common.Version - Hibernate Commons Annotations 3.2.0.Final
77 [main] INFO org.hibernate.cfg.Environment - Hibernate 3.6.0.Final
78 [main] INFO org.hibernate.cfg.Environment - hibernate.properties not found
80 [main] INFO org.hibernate.cfg.Environment - Bytecode provider name : javassist
82 [main] INFO org.hibernate.cfg.Environment - using JDK 1.4 java.sql.Timestamp handling
125 [main] INFO org.hibernate.cfg.Configuration - configuring from resource: /hibernate.cfg.xml
125 [main] INFO org.hibernate.cfg.Configuration - Configuration resource: /hibernate.cfg.xml
Exception in thread “main” org.hibernate.HibernateException: Could not parse configuration: /hibernate.cfg.xml
at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:2216)
at org.hibernate.cfg.AnnotationConfiguration.doConfigure(AnnotationConfiguration.java:229)
at org.hibernate.cfg.AnnotationConfiguration.doConfigure(AnnotationConfiguration.java:70)
at org.hibernate.cfg.Configuration.configure(Configuration.java:2128)
at org.hibernate.cfg.AnnotationConfiguration.configure(AnnotationConfiguration.java:211)
at org.hibernate.cfg.AnnotationConfiguration.configure(AnnotationConfiguration.java:70)
at org.hibernate.cfg.Configuration.configure(Configuration.java:2107)
at org.hibernate.cfg.AnnotationConfiguration.configure(AnnotationConfiguration.java:205)
at tese.persistencia.RepositorioFuncionario.(RepositorioFuncionario.java:17)
at teste.TesteMain.main(TesteMain.java:9)
Caused by: org.dom4j.DocumentException: <a href="http://hibernate.sourceforge.net/hibernate-configuration-3.0dtd">http://hibernate.sourceforge.net/hibernate-configuration-3.0dtd</a> Nested exception: <a href="http://hibernate.sourceforge.net/hibernate-configuration-3.0dtd">http://hibernate.sourceforge.net/hibernate-configuration-3.0dtd</a>
at org.dom4j.io.SAXReader.read(SAXReader.java:484)
at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:2208)
… 9 more
Tem que adicionar no xml onde estão suas classes com as anotações, coloca o caminho e o nome da classe como o exemplo a baixo. Coloca dentro das tags <session-factory>
<!-- mapping classes -->
<mapping class="br.com.jeebrasil.hibernate.anotacoes.dominio.Aluno"/>
E continua minha novela mesmo erros galera por favor agradeço ajuda mas continuem em ajundando.Quem tiver mais sugestões agradeço.
Altera seu arquivo por esse e seta as suas propriedades, mas usa esse arquivo.
Caso não funcione, exclua esse arquivo hibernate.cfg.xml da aplicação e uso em seu lugar o hibernate.properties.
Poste o resultado.
Agora tá aparecendo esse monte de coisa, eu mudei a versão do banco coloquei o postgresql 9.0 ao invés da 8.4 entretanto contiam os problemas
pq o driver que eu tava usando era o 9.0.
78 [main] INFO org.hibernate.cfg.Environment - Hibernate 3.6.0.Final
78 [main] INFO org.hibernate.cfg.Environment - hibernate.properties not found
78 [main] INFO org.hibernate.cfg.Environment - Bytecode provider name : javassist
78 [main] INFO org.hibernate.cfg.Environment - using JDK 1.4 java.sql.Timestamp handling
125 [main] INFO org.hibernate.cfg.Configuration - configuring from resource: /hibernate.cfg.xml
125 [main] INFO org.hibernate.cfg.Configuration - Configuration resource: /hibernate.cfg.xml
156 [main] WARN org.hibernate.util.DTDEntityResolver - recognized obsolete hibernate namespace <a href="http://hibernate.sourceforge.net/">http://hibernate.sourceforge.net/</a>. Use namespace <a href="http://www.hibernate.org/dtd/">http://www.hibernate.org/dtd/</a> instead. Refer to Hibernate 3.6 Migration Guide!
188 [main] INFO org.hibernate.cfg.Configuration - Configured SessionFactory: null
219 [main] INFO org.hibernate.cfg.Configuration - Hibernate Validator not found: ignoring
219 [main] INFO org.hibernate.cfg.search.HibernateSearchEventListenerRegister - Unable to find org.hibernate.search.event.FullTextIndexEventListener on the classpath. Hibernate Search is not enabled.
234 [main] INFO org.hibernate.connection.DriverManagerConnectionProvider - Using Hibernate built-in connection pool (not for production use!)
234 [main] INFO org.hibernate.connection.DriverManagerConnectionProvider - Hibernate connection pool size: 20
234 [main] INFO org.hibernate.connection.DriverManagerConnectionProvider - autocommit mode: false
234 [main] INFO org.hibernate.connection.DriverManagerConnectionProvider - using driver: org.postgresql.Driver at URL: jdbc:postgresql://localhost:5432/pesquisa
234 [main] INFO org.hibernate.connection.DriverManagerConnectionProvider - connection properties: {user=postgres, password=****}
297 [main] INFO org.hibernate.cfg.SettingsFactory - Database ->
name : PostgreSQL
version : 9.0.2
major : 9
minor : 0
297 [main] INFO org.hibernate.cfg.SettingsFactory - Driver ->
name : PostgreSQL Native Driver
version : PostgreSQL 9.0 JDBC4 (build 801)
major : 9
minor : 0
312 [main] INFO org.hibernate.dialect.Dialect - Using dialect: org.hibernate.dialect.PostgreSQLDialect
328 [main] INFO org.hibernate.engine.jdbc.JdbcSupportLoader - Disabling contextual LOB creation as createClob() method threw error : java.lang.reflect.InvocationTargetException
328 [main] INFO org.hibernate.transaction.TransactionFactoryFactory - Using default transaction strategy (direct JDBC transactions)
328 [main] INFO org.hibernate.transaction.TransactionManagerLookupFactory - No TransactionManagerLookup configured (in JTA environment, use of read-write or transactional second-level cache is not recommended)
328 [main] INFO org.hibernate.cfg.SettingsFactory - Automatic flush during beforeCompletion(): disabled
328 [main] INFO org.hibernate.cfg.SettingsFactory - Automatic session close at end of transaction: disabled
328 [main] INFO org.hibernate.cfg.SettingsFactory - JDBC batch size: 15
328 [main] INFO org.hibernate.cfg.SettingsFactory - JDBC batch updates for versioned data: disabled
328 [main] INFO org.hibernate.cfg.SettingsFactory - Scrollable result sets: enabled
328 [main] INFO org.hibernate.cfg.SettingsFactory - JDBC3 getGeneratedKeys(): enabled
328 [main] INFO org.hibernate.cfg.SettingsFactory - Connection release mode: auto
328 [main] INFO org.hibernate.cfg.SettingsFactory - Default batch fetch size: 1
328 [main] INFO org.hibernate.cfg.SettingsFactory - Generate SQL with comments: disabled
328 [main] INFO org.hibernate.cfg.SettingsFactory - Order SQL updates by primary key: disabled
328 [main] INFO org.hibernate.cfg.SettingsFactory - Order SQL inserts for batching: disabled
328 [main] INFO org.hibernate.cfg.SettingsFactory - Query translator: org.hibernate.hql.ast.ASTQueryTranslatorFactory
328 [main] INFO org.hibernate.hql.ast.ASTQueryTranslatorFactory - Using ASTQueryTranslatorFactory
328 [main] INFO org.hibernate.cfg.SettingsFactory - Query language substitutions: {}
328 [main] INFO org.hibernate.cfg.SettingsFactory - JPA-QL strict compliance: disabled
328 [main] INFO org.hibernate.cfg.SettingsFactory - Second-level cache: enabled
328 [main] INFO org.hibernate.cfg.SettingsFactory - Query cache: disabled
328 [main] INFO org.hibernate.cfg.SettingsFactory - Cache region factory : org.hibernate.cache.impl.NoCachingRegionFactory
328 [main] INFO org.hibernate.cfg.SettingsFactory - Optimize cache for minimal puts: disabled
328 [main] INFO org.hibernate.cfg.SettingsFactory - Structured second-level cache entries: disabled
328 [main] INFO org.hibernate.cfg.SettingsFactory - Echoing all SQL to stdout
328 [main] INFO org.hibernate.cfg.SettingsFactory - Statistics: disabled
328 [main] INFO org.hibernate.cfg.SettingsFactory - Deleted entity synthetic identifier rollback: disabled
328 [main] INFO org.hibernate.cfg.SettingsFactory - Default entity-mode: pojo
328 [main] INFO org.hibernate.cfg.SettingsFactory - Named query checking : enabled
328 [main] INFO org.hibernate.cfg.SettingsFactory - Check Nullability in Core (should be disabled when Bean Validation is on): enabled
344 [main] INFO org.hibernate.impl.SessionFactoryImpl - building session factory
375 [main] INFO org.hibernate.impl.SessionFactoryObjectFactory - Not binding factory to JNDI, no JNDI name configured
Exception in thread “main” org.hibernate.MappingException: Unknown entity: teste.Funcionario
at org.hibernate.impl.SessionFactoryImpl.getEntityPersister(SessionFactoryImpl.java:691)
at org.hibernate.impl.SessionImpl.getEntityPersister(SessionImpl.java:1485)
at org.hibernate.event.def.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:120)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:210)
at org.hibernate.event.def.DefaultSaveEventListener.saveWithGeneratedOrRequestedId(DefaultSaveEventListener.java:56)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:195)
at org.hibernate.event.def.DefaultSaveEventListener.performSaveOrUpdate(DefaultSaveEventListener.java:50)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:93)
at org.hibernate.impl.SessionImpl.fireSave(SessionImpl.java:713)
at org.hibernate.impl.SessionImpl.save(SessionImpl.java:701)
at org.hibernate.impl.SessionImpl.save(SessionImpl.java:697)
at teste.persistencia.RepositorioFuncionario.inserirFuncionario(RepositorioFuncionario.java:21)
at teste.TesteMain.main(TesteMain.java:11)
Exception in thread "main" org.hibernate.MappingException: Unknown entity: teste.Funcionario
Cheque esse problema e tente compilar novamente.
ele nao está encontrando seu banco pq nao encontra a configuracao que está no arquivo .properties. veja la no log. tenta com xml pra ver se nao via dar certo.