0

I cannot force hibernate to use proper database.

Consider we have two databases with same table names on one MySQL instance, and spring boot config:

spring.datasource.url=jdbc:mysql://localhost:3306/old_indeeddb?serverTimezone=UTC
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.jpa.properties.hibernate.default_schema=old_indeeddb
spring.jpa.hibernate.ddl-auto=validate

But when I run application it connects to wrong database indeeddb,instead of old_indeeddb . Here from log:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaAutoConfiguration.class]: Invocation of init method failed; nested exception is org.hibernate.HibernateException: Missing column: scraper_task_code in indeeddb.job_chron_stg

As you see I tried solution flagged as it works:

Change database schema used by Spring Boot

but it fails for me.

Not know why. Here my pom:

<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>healthjobs</groupId>
    <artifactId>scraper</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>HealthJobsScraper</name>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.3.6.RELEASE</version>
    </parent>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <!-- <version>1.3.3.RELEASE</version> can safely omit -->
        </dependency>


        <dependency>
            <groupId>org.jsoup</groupId>
            <artifactId>jsoup</artifactId>
            <version>1.8.3</version>
        </dependency>
        <!-- 
        <dependency>
            <groupId>com.google.code.gson</groupId>
            <artifactId>gson</artifactId>
            <version>2.4</version>
        </dependency>
         -->
        <dependency>
            <groupId>org.postgresql</groupId>
            <artifactId>postgresql</artifactId>

            <!--
            <version>9.4.1208</version>
            <version>9.4.1207.jre7</version> 
             -->

        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>6.0.5</version>
        </dependency>

        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-io</artifactId>
            <version>1.3.2</version>
        </dependency>       

        <dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
        </dependency>
        <!-- 
        <dependency>
            <groupId>org.scala-lang</groupId>
            <artifactId>scala-library</artifactId>
            <version>2.11.0</version>
        </dependency>
         -->

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.codehaus.jackson</groupId>
            <artifactId>jackson-mapper-asl</artifactId>
            <version>1.9.13</version>
        </dependency>

        <dependency>
            <groupId>xalan</groupId>
            <artifactId>xalan</artifactId>
            <version>2.7.1</version>
        </dependency>

        <dependency>
            <groupId>org.scala-lang</groupId>
            <artifactId>scala-library</artifactId>
            <version>2.11.0</version>
        </dependency>
        <dependency>
            <groupId>org.neo4j</groupId>
            <artifactId>neo4j-cypher-compiler-2.2</artifactId>
            <version>2.2.5</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

    <repositories>
        <repository>
            <id>spring-releases</id>
            <name>Spring Releases</name>
            <url>https://repo.spring.io/libs-release</url>
        </repository>
        <repository>
            <id>org.jboss.repository.releases</id>
            <name>JBoss Maven Release Repository</name>
            <url>https://repository.jboss.org/nexus/content/repositories/releases</url>
        </repository>
    </repositories>

    <pluginRepositories>
        <pluginRepository>
            <id>spring-releases</id>
            <name>Spring Releases</name>
            <url>https://repo.spring.io/libs-release</url>
        </pluginRepository>
    </pluginRepositories>

</project>
P_M
  • 2,723
  • 4
  • 29
  • 62
  • Your error says "scraper_task_code" column is not preset in your table. Can you share more info on your jpa models,? – Tanmay Delhikar Jun 08 '17 at 10:10
  • You are providing 2 datasources in config file? How are you creating the beans for data source, entity manager or hibernate etc. Please show the code and config file – Chaitanya Jun 08 '17 at 10:56
  • @Tanmay scraper_task_code column is not present in indeeddb.job_chron_stg table and this is true. But as you can see the database name is indeeddb, but it should be ld_indeeddb! – P_M Jun 08 '17 at 12:15
  • @Chaitanya I provide only one datasource in config file, and you see in my post exactly content of it: config.proprties file. I not did any other configs with datasource description or bean instantiation as I use Spring Boot. The Spring Boot takes config.proprties file and creates necessary beans. The Entity classes are mapped with annotations, and this means no xml configs in my project at all – P_M Jun 08 '17 at 12:22
  • As I see from comments, it is not clear that this is Hibernate behavior, I will add link to my previous research of this issue shortly. – P_M Jun 08 '17 at 12:24
  • Here the link to initial issue description https://stackoverflow.com/questions/42437407/spring-boot-crash-on-hibernate-validate-but-database-is-wrong – P_M Jun 08 '17 at 12:27

0 Answers0