无法在Google Cloud中构建Hibernate SessionFactory

时间:2018-03-02 09:44:58

标签: hibernate jpa spring-boot google-cloud-platform

我正在使用我在Google Appengine上部署的Spring Boot应用程序,我想连接到Google Cloud SQL。我已成功激活Google Cloud SQL API,创建了一个实例,并在Google Cloud SQL端创建了一个数据库。我也可以成功地将我的应用程序部署到Appengine,但是一旦我尝试填充我的数据库,我就会收到错误:

java.lang.RuntimeException: 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 java.lang.NoSuchMethodError: 
org.hibernate.boot.registry.BootstrapServiceRegistryBuilder.applyIntegrator(Lorg/hibernate/integrator/spi/Integrator;)Lorg/hibernate/boot/registry/BootstrapServiceRegistryBuilder;
    at org.eclipse.jetty.annotations.ServletContainerInitializersStarter.doStart (ServletContainerInitializersStarter.java:68)
    at org.eclipse.jetty.util.component.AbstractLifeCycle.start (AbstractLifeCycle.java:68)
    at org.eclipse.jetty.servlet.ServletContextHandler.startContext (ServletContextHandler.java:330)
    at org.eclipse.jetty.webapp.WebAppContext.startWebapp (WebAppContext.java:1406)
    at org.eclipse.jetty.webapp.WebAppContext.startContext (WebAppContext.java:1368)
    at org.eclipse.jetty.server.handler.ContextHandler.doStart (ContextHandler.java:778)
    at org.eclipse.jetty.servlet.ServletContextHandler.doStart (ServletContextHandler.java:262)
    at org.eclipse.jetty.webapp.WebAppContext.doStart (WebAppContext.java:522)
    at org.eclipse.jetty.util.component.AbstractLifeCycle.start (AbstractLifeCycle.java:68)
    at com.google.apphosting.runtime.jetty9.AppVersionHandlerMap.createHandler (AppVersionHandlerMap.java:244)
    at com.google.apphosting.runtime.jetty9.AppVersionHandlerMap.getHandler (AppVersionHandlerMap.java:182)
    at com.google.apphosting.runtime.jetty9.JettyServletEngineAdapter.serviceRequest (JettyServletEngineAdapter.java:97)
    at com.google.apphosting.runtime.JavaRuntime$RequestRunnable.dispatchServletRequest (JavaRuntime.java:686)
    at com.google.apphosting.runtime.JavaRuntime$RequestRunnable.dispatchRequest (JavaRuntime.java:648)
    at com.google.apphosting.runtime.JavaRuntime$RequestRunnable.run (JavaRuntime.java:618)
    at com.google.apphosting.runtime.JavaRuntime$NullSandboxRequestRunnable.run (JavaRuntime.java:812)
    at com.google.apphosting.runtime.ThreadGroupPool$PoolEntry.run (ThreadGroupPool.java:274)
    at java.lang.Thread.run (Thread.java:745)

我的pom文件如下:

<?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>com.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>

<name>demo</name>
<description>Demo project for Spring Boot</description>

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.5.9.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
</parent>

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    <java.version>1.8</java.version>
</properties>

<dependencies>
    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <version>1.16.20</version>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
        <exclusions>
            <exclusion>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-tomcat</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <scope>runtime</scope>
    </dependency>
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>javax.servlet-api</artifactId>
        <version>3.1.0</version>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>jul-to-slf4j</artifactId>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
</dependencies>

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
        <plugin>
            <groupId>com.google.cloud.tools</groupId>
            <artifactId>appengine-maven-plugin</artifactId>
            <version>1.3.1</version>
        </plugin>
    </plugins>
</build>

我的application.properties文件如下:

spring.jpa.database=MYSQL
spring.datasource.url=jdbc:mysql://google/DB_NAME?cloudSqlInstance=PROJECT_ID:REGION:INSTANCE_NAME&socketFactory=com.google.cloud.sql.mysql.SocketFactory
spring.datasource.username=root
spring.datasource.password=password
spring.jpa.show-sql=true
spring.jpa.hibernate.ddl-auto=update

我有一个实体,如下:

package com.example.demo.model;
import lombok.Getter;
import lombok.Setter;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
@Entity
@Gettter
@Setter
public class Medico {

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
private String nome;
private String cognome;

protected Medico() {}

public Medico(String nome, String cognome) {
    this.nome = nome;
    this.cognome = cognome;
}

@Override
public String toString() {
    return "Medico{" +
            "id=" + id +
            ", nome='" + nome + '\'' +
            ", cognome='" + cognome + '\'' +
            '}';
}
}

我有以下存储库:

package com.example.demo.repository;
import com.example.demo.model.Medico;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;
import java.util.List;
@Repository
public interface MedicoRepository extends CrudRepository<Medico, Long> {
    List<Medico> findMedicoByCognome(String cognome);
}

最后,我有以下控制器:

package com.example.demo;
import com.example.demo.model.Medico;
import com.example.demo.repository.MedicoRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class MainController {

@Autowired
MedicoRepository repository;

@GetMapping("/medici")
public String hello() {
    String results = "";
    repository.save(new Medico("Jack", "Bauer"));
    repository.save(new Medico("Chloe", "O'Brian"));
    repository.save(new Medico("Kim", "Bauer"));
    repository.save(new Medico("David", "Palmer"));
    repository.save(new Medico("Michelle", "Dessler"));
    results += "Medici found with findAll():";
    results += "-------------------------------";
    for (Medico medico : repository.findAll()) {
        results += medico.toString();
    }
    return results;
}
}

我不确定出了什么问题。任何建议都非常感谢!

0 个答案:

没有答案