简体   繁体   中英

Spring boot JPA error in gradle build from docker

I have a Gradle project, i added jpa to it with:

implementation group: 'org.springframework.boot', name: 'spring-boot-starter-data-jpa', version: '2.4.3'
implementation group: 'mysql', name: 'mysql-connector-java', version: '8.0.23'

And in application.properties i have added the following lines:

spring.datasource.url=jdbc:mysql://user:pass@localhost/dbname
spring.datasource.username=null
spring.datasource.password=null
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
### Hibernate Properties
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5InnoDBDialect
spring.jpa.hibernate.ddl-auto = update

It's working fine when the mysql server is hosted on the machine. But when i am building my spring boot application inside the docker with ./gradlew build command, without hosting the mysql its giving me the exception:

oreApplicationTests > contextLoads() FAILED
    java.lang.IllegalStateException at DefaultCacheAwareContextLoaderDelegate.java:132
        Caused by: org.springframework.beans.factory.BeanCreationException at AbstractAutowireCapableBeanFactory.java:1796
            Caused by: javax.persistence.PersistenceException at AbstractEntityManagerFactoryBean.java:403
                Caused by: org.hibernate.exception.JDBCConnectionException at SQLStateConversionDelegate.java:112
                    Caused by: com.mysql.cj.jdbc.exceptions.CommunicationsException at SQLError.java:174
                        Caused by: com.mysql.cj.exceptions.CJCommunicationsException at NativeConstructorAccessorImpl.java:-2
                            Caused by: java.net.UnknownHostException at InetAddress.java:797

I don't want these errors during the build, I only want to turn on the mysql server when i will be using the jars. So i don't want these errors during the compile time.

I ran into a similar error when using MySQL. The solution for me was to add in the dialect to the.properties folder. I figured out the error was coming from JPA not knowing which dialect to use based on the database.

spring.jpa.database-platform=org.hibernate.dialect.{your dialect here}

Also keep in mind tests want to run with an in memory database by default so you need to wire up your tests to run as expected.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM