Bom dia Amigos, já programo em java puro a 3 anos e estou iniciando os estudos com Frameworks, Spring e Hibernate, me deparei com um erro que ocorre somente com SQL Server, provavelmente algum erro de configuração que esta passando despercebido pelos meus olhos.
Segue meus códigos:
Clase Controller
import br.com.hjsystems.syswebchamados.model.repositorios.FornecedoresRepositorios;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
/**
*
* @author Daniel
*/
@Controller
@RequestMapping("/fornecedor")
public class FornecedorController {
@Autowired private FornecedoresRepositorios fornecedorRepositorio;
@RequestMapping("/total")
@ResponseBody
public String listaFornecedores(){
return "Existem " + fornecedorRepositorio.count() + "cadastrado(s)";
}
}
Entidade Fornecedor
import java.io.Serializable;
import java.util.Objects;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import org.hibernate.validator.constraints.NotEmpty;
/**
*
* @author Daniel
*/
@Entity
@Table(name = "FORNECEDORES", catalog = "SysWeb", schema = "dbo")
public class Fornecedores implements Serializable {
private static final long serialVersionUID = 1619917121015558131L;
@Id
@NotNull
@Column(name = "FORN_ID")
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer fornId;
@NotNull
@Size(max = 160)
@NotEmpty
@Column(name = "FORN_NOME")
private String fornNome;
@NotNull
@Size(max = 30)
@NotEmpty
@Column(name = "FORN_TELEFONE")
private String fornTelefone;
@NotNull
@Size(max = 30)
@NotEmpty
@Column(name = "FORN_CELULAR")
private String fornCelular;
@NotNull
@NotEmpty
@Size(max = 30)
@Column(name = "EMAI_ID")
private Integer emaiId;
public Integer getFornId() {
return fornId;
}
public void setFornId(Integer fornId) {
this.fornId = fornId;
}
public String getFornNome() {
return fornNome;
}
public void setFornNome(String fornNome) {
this.fornNome = fornNome;
}
public String getFornTelefone() {
return fornTelefone;
}
public void setFornTelefone(String fornTelefone) {
this.fornTelefone = fornTelefone;
}
public String getFornCelular() {
return fornCelular;
}
public void setFornCelular(String fornCelular) {
this.fornCelular = fornCelular;
}
public Integer getEmaiId() {
return emaiId;
}
public void setEmaiId(Integer emaiId) {
this.emaiId = emaiId;
}
@Override
public int hashCode() {
int hash = 7;
hash = 97 * hash + Objects.hashCode(this.fornId);
hash = 97 * hash + Objects.hashCode(this.fornNome);
hash = 97 * hash + Objects.hashCode(this.fornTelefone);
hash = 97 * hash + Objects.hashCode(this.fornCelular);
hash = 97 * hash + Objects.hashCode(this.emaiId);
return hash;
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
final Fornecedores other = (Fornecedores) obj;
if (!Objects.equals(this.fornNome, other.fornNome)) {
return false;
}
if (!Objects.equals(this.fornTelefone, other.fornTelefone)) {
return false;
}
if (!Objects.equals(this.fornCelular, other.fornCelular)) {
return false;
}
if (!Objects.equals(this.fornId, other.fornId)) {
return false;
}
if (!Objects.equals(this.emaiId, other.emaiId)) {
return false;
}
return true;
}
}
Repositorio
import br.com.hjsystems.syswebchamados.model.entity.Fornecedores;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;
/**
*
* @author Daniel
*/
@Repository
public interface FornecedoresRepositorios extends CrudRepository<Fornecedores, Integer>{
}
Configuração do Banco
import com.mchange.v2.c3p0.ComboPooledDataSource;
import java.beans.PropertyVetoException;
import java.util.Properties;
import javax.sql.DataSource;
import org.hibernate.jpa.HibernatePersistenceProvider;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.orm.jpa.vendor.HibernateJpaDialect;
import org.springframework.transaction.annotation.EnableTransactionManagement;
/**
*
* @author Daniel
*/
@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(basePackages = "br.com.hjsystems.syswebchamados.model.repositorios")
public class DataBaseConfig {
@Bean
public DataSource dataSource() throws PropertyVetoException{
ComboPooledDataSource dataSource = new ComboPooledDataSource();
dataSource.setDriverClass("com.microsoft.sqlserver.jdbc.SQLServerDriver");
dataSource.setJdbcUrl("jdbc:sqlserver://hjsystems.dynns.com:3389;databaseName=SysWeb;");
dataSource.setUser("sa");
dataSource.setPassword("Hj11032011");
return dataSource;
}
@Bean
public LocalContainerEntityManagerFactoryBean entityManagerFactory() throws PropertyVetoException {
LocalContainerEntityManagerFactoryBean entityManagerFactoryBean = new LocalContainerEntityManagerFactoryBean();
entityManagerFactoryBean.setDataSource(dataSource());
entityManagerFactoryBean.setPackagesToScan("br.com.hjsystems.syswebchamados.model.entity");
entityManagerFactoryBean.setPersistenceProviderClass(HibernatePersistenceProvider.class);
entityManagerFactoryBean.setJpaDialect(new HibernateJpaDialect());
Properties jpaProperties = new Properties();
jpaProperties.put("hibernate.dialect","org.hibernate.dialect.SQLServer2008Dialect");
jpaProperties.put("hbm2ddl.auto","update");
jpaProperties.put("show_sql","true");
jpaProperties.put("format_sql", "false");
entityManagerFactoryBean.setJpaProperties(jpaProperties);
return entityManagerFactoryBean;
}
@Bean
public JpaTransactionManager transactionManager() throws PropertyVetoException{
JpaTransactionManager transactionManager = new JpaTransactionManager();
transactionManager.setEntityManagerFactory(entityManagerFactory().getObject());
return transactionManager;
}
}
Arquivo POM.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>br.com.hjsystems</groupId>
<artifactId>SysWebChamados</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
<name>SysWebChamados</name>
<properties>
<endorsed.dir>${project.build.directory}/endorsed</endorsed.dir>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<repositories>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>http://repo.spring.io/milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
<repository>
<id>unknown-jars-temp-repo</id>
<name>A temporary repository created by NetBeans for libraries and jars it could not identify. Please replace the dependencies in this repository with correct ones and delete this repository.</name>
<url>file:${project.basedir}/lib</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>unknown.binary</groupId>
<artifactId>hibernate-jpamodelgen-4.3.1.Final</artifactId>
<version>SNAPSHOT</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.mchange</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.2.1</version>
<type>jar</type>
</dependency>
<dependency>
<groupId>com.microsoft.sqlserver</groupId>
<artifactId>sqljdbc4</artifactId>
<version>4.0</version>
</dependency>
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-web-api</artifactId>
<version>7.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.persistence</groupId>
<artifactId>persistence-api</artifactId>
<version>1.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/javax/javaee-api -->
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-api</artifactId>
<version>7.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.1.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>4.1.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>4.1.2.RELEASE</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>4.1.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-jpa</artifactId>
<version>1.7.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>4.3.7.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>4.3.1.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>5.1.3.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-envers</artifactId>
<version>4.3.7.Final</version>
</dependency>
<dependency>
<groupId>cglib</groupId>
<artifactId>cglib</artifactId>
<version>2.2.2</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.0.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>2.2.1-b03</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.3.1</version>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-core</artifactId>
<version>3.0.7.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-web</artifactId>
<version>3.0.7.RELEASE</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.5</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>1.6.4</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
<compilerArguments>
<endorseddirs>${endorsed.dir}</endorseddirs>
</compilerArguments>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.3</version>
<configuration>
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.6</version>
<executions>
<execution>
<phase>validate</phase>
<goals>
<goal>copy</goal>
</goals>
<configuration>
<outputDirectory>${endorsed.dir}</outputDirectory>
<silent>true</silent>
<artifactItems>
<artifactItem>
<groupId>javax</groupId>
<artifactId>javaee-endorsed-api</artifactId>
<version>7.0</version>
<type>jar</type>
</artifactItem>
</artifactItems>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.mortbay.jetty</groupId>
<artifactId>jetty-maven-plugin</artifactId>
<version>8.1.16.v20140903</version>
<configuration>
<scanIntervalSeconds>3</scanIntervalSeconds>
<webApp>
<contextPath>/onboard</contextPath>
</webApp>
</configuration>
</plugin>
</plugins>
</build>
</project>
Ocorre o erro de que a tabela não existe, e realmente não existe, mas a ideia era que ele criasse a tabela automaticamente. segue o erro:
02-Jul-2017 10:24:04.745 SEVERE [http-nio-8084-exec-60] org.apache.catalina.core.StandardWrapperValve.invoke Servlet.service() for servlet [appServlet] in context with path [/chamados] threw exception [Request processing failed; nested exception is org.springframework.dao.InvalidDataAccessResourceUsageException: could not extract ResultSet; SQL [n/a]; nested exception is org.hibernate.exception.SQLGrammarException: could not extract ResultSet] with root cause
com.microsoft.sqlserver.jdbc.SQLServerException: Invalid object name 'SysWeb.dbo.FORNECEDORES'.
02-Jul-2017 09:37:27.513 INFO [http-nio-8084-exec-30] org.apache.catalina.core.StandardContext.reload Reloading Context with name [/chamados] is completed
02-Jul-2017 10:24:04.610 WARN [http-nio-8084-exec-60] org.hibernate.engine.jdbc.spi.SqlExceptionHelper.logExceptions SQL Error: 208, SQLState: S0002
02-Jul-2017 10:24:04.614 ERROR [http-nio-8084-exec-60] org.hibernate.engine.jdbc.spi.SqlExceptionHelper.logExceptions Invalid object name 'SysWeb.dbo.FORNECEDORES'.
Se eu trocar o banco para MySQL, trocando a conexão e o driver ele já funciona de imediato, cria as tabelas e etc… porem com SQLServer não funciona…
Alguma dica ou sugestão ??