Olá,
Fiz os projetos do curso utilizando o MySQL conforme os vídeos, porem utilizo muito o SQLServer e estou com problemas na utilização do JPA.
Estou usando o Hibernate 4.3.7 Final; Microsoft JDBC 4.0 - SQLServer Driver
Primeiramente Criei um projeto similar ao do curso: FinancasSQLServer Adicionei o jpa e os jars do hibernate junto com os sqljdbc. Tudo no buildPath.
Criei o Persistence:
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence
http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
version="2.0">
<persistence-unit name="gilmar">
<!-- Hibernate -->
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<!-- Entity -->
<class>modelo.Conta</class>
<properties>
<!-- Propriedades JDBC -->
<property name="javax.persistence.jdbc.driver" value="com.microsoft.sqlserver.jdbc.SQLServerDriver" />
<property name="javax.persistence.jdbc.url" value="jdbc:sqlserver://server:1433;databaseName=gilmar" />
<property name="javax.persistence.jdbc.user" value="sa" />
<property name="javax.persistence.jdbc.password" value="mister+1" />
<!-- Configurações específicas do Hibernate -->
<property name="hibernate.dialect" value="org.hibernate.dialect.SQLServerDialect" />
<property name="hibernate.hbm2ddl.auto" value="update" />
<property name="hibernate.show_sql" value="true" />
<property name="hibernate.format_sql" value="true" />
</properties>
</persistence-unit>
</persistence>
em seguida a classe Conta:
package modelo;
import javax.persistence.Entity;
import javax.persistence.Id;
@Entity
public class Conta {
@Id
private Integer id;
private String titular;
private String banco;
private String agencia;
private String numero;
//..get's e set's
Em seguida o JPAUtil:
package util;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
public class JPAUtil {
private static EntityManagerFactory entityManagerFactory = Persistence
.createEntityManagerFactory("gilmar");
public EntityManager getEntityManager(){
return entityManagerFactory.createEntityManager();
}
}
Finalmente o teste:
package teste;
import javax.persistence.EntityManager;
import util.JPAUtil;
import modelo.Conta;
public class testaJPA {
public static void main(String[] args) {
Conta conta = new Conta();
conta.setTitular("Gilmar");
conta.setAgencia("12345-6");
conta.setBanco("XPTO");
conta.setNumero("123456789");
EntityManager entityManager = new JPAUtil().getEntityManager();
entityManager.getTransaction().begin();
entityManager.persist(conta);
entityManager.getTransaction().commit();
entityManager.close();
}
}
ao executar me traz o seguinte erro:
GRAVE: O Java Runtime Environment (JRE) versão 1.8 não tem suporte nesse driver. Use a biblioteca de classes sqljdbc4.jar, que dá suporte para JDBC 4.0.
Exception in thread "main" java.lang.ExceptionInInitializerError
at teste.testaJPA.main(testaJPA.java:18)
Caused by: javax.persistence.PersistenceException: Unable to build entity manager factory
at org.hibernate.jpa.HibernatePersistenceProvider.createEntityManagerFactory(HibernatePersistenceProvider.java:83)
at org.hibernate.ejb.HibernatePersistence.createEntityManagerFactory(HibernatePersistence.java:54)
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:55)
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:39)
at util.JPAUtil.<clinit>(JPAUtil.java:10)
... 1 more
Caused by: java.lang.UnsupportedOperationException: O Java Runtime Environment (JRE) versão 1.8 não tem suporte nesse driver. Use a biblioteca de classes sqljdbc4.jar, que dá suporte para JDBC 4.0.
at com.microsoft.sqlserver.jdbc.SQLServerConnection.<init>(SQLServerConnection.java:304)
at com.microsoft.sqlserver.jdbc.SQLServerDriver.connect(SQLServerDriver.java:1011)
at org.hibernate.engine.jdbc.connections.internal.DriverConnectionCreator.makeConnection(DriverConnectionCreator.java:55)
at org.hibernate.engine.jdbc.connections.internal.BasicConnectionCreator.createConnection(BasicConnectionCreator.java:75)
at org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl.configure(DriverManagerConnectionProviderImpl.java:106)
at org.hibernate.boot.registry.internal.StandardServiceRegistryImpl.configureService(StandardServiceRegistryImpl.java:111)
at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:234)
at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:206)
at org.hibernate.engine.jdbc.internal.JdbcServicesImpl.buildJdbcConnectionAccess(JdbcServicesImpl.java:260)
at org.hibernate.engine.jdbc.internal.JdbcServicesImpl.configure(JdbcServicesImpl.java:94)
at org.hibernate.boot.registry.internal.StandardServiceRegistryImpl.configureService(StandardServiceRegistryImpl.java:111)
at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:234)
at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:206)
at org.hibernate.cfg.Configuration.buildTypeRegistrations(Configuration.java:1887)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1845)
at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl$4.perform(EntityManagerFactoryBuilderImpl.java:852)
at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl$4.perform(EntityManagerFactoryBuilderImpl.java:845)
at org.hibernate.boot.registry.classloading.internal.ClassLoaderServiceImpl.withTccl(ClassLoaderServiceImpl.java:398)
at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:844)
at org.hibernate.jpa.HibernatePersistenceProvider.createEntityManagerFactory(HibernatePersistenceProvider.java:75)
... 5 more
PS. Ao criar a classe CONTA, deu erro ao adicionar a anotação @Entity, foi solicitado a alteração do JRE para 1.5
Esses jars estão funcionando normalmente com statement, apenas com o JPA não esta indo, possivelmente a causa é o persistence, porem não estou encontrando...
Desde já agradeço a ajuda...