【问题标题】:Schema 'SA' does not exist and dropping table架构“SA”不存在并删除表
【发布时间】:2018-01-01 12:56:47
【问题描述】:

我正在使用 Spring Boot 创建课程和主题数据库。一旦我对课程课程进行了更改,就会出现一堆错误,我遇到了麻烦。我不确定出了什么问题。以下是错误消息:

 ERROR 1136 --- [           main] org.hibernate.tool.hbm2ddl.SchemaExport  : HHH000389: Unsuccessful: alter table course drop constraint FKokaxyfpv8p583w8yspapfb2ar

    ERROR 1136 --- [           main] org.hibernate.tool.hbm2ddl.SchemaExport  : Schema 'SA' does not exist

    ERROR 1136 --- [           main] org.hibernate.tool.hbm2ddl.SchemaExport  : HHH000389: Unsuccessful: drop table course

    ERROR 1136 --- [           main] org.hibernate.tool.hbm2ddl.SchemaExport  : Schema 'SA' does not exist

    ERROR 1136 --- [           main] org.hibernate.tool.hbm2ddl.SchemaExport  : HHH000389: Unsuccessful: drop table topic

    ERROR 1136 --- [           main] org.hibernate.tool.hbm2ddl.SchemaExport  : Schema 'SA' does not exist

    WARN 1136 --- [           main] o.h.engine.jdbc.spi.SqlExceptionHelper   : SQL Warning Code: 10000, SQLState: 01J01

    WARN 1136 --- [           main] o.h.engine.jdbc.spi.SqlExceptionHelper   :

未创建数据库“memory:testdb”,而是连接到现有数据库。

另外,这是我的课程代码,我认为主题代码没问题,但我遇到了麻烦。我正在使用内部数据库。

包装...

@Entity

public class Course {
    // here generate constructors and getters/setters
    @Id
    private String id;
    private String Name;
    private String description;

    @ManyToOne
    private Topic topic; //use it to tie this class to the Topic class, to make it easier for the user

    public Topic getTopic() {
        return topic;
    }

    public void setTopic(Topic topic) {
        this.topic = topic;
    }

    public Course() {
    }

    public Course(String id, String name, String description, String topicId) {
        super();
        this.id = id;
        Name = name;
        this.description = description;
        this.topic = new Topic(topicId,"","");
    }

    public String getId() {
        return id;
    }
    public void setId(String id) {
        this.id = id;
    }
    public String getName() {
        return Name;
    }
    public void setName(String name) {
        Name = name;
    }
    public String getDescription() {
        return description;
    }
    public void setDescription(String description) {
        this.description = description;
    }
}

也是我的控制器:

@RestController() //makes anything a rest controller, every time you build a class and add this on top of it
public class CourseController {

    @Autowired // it marks the courseService as something that needs dependency inj.
    private CourseService courseService;// To create a service you need a private courseService variable

    //GETALL
    @RequestMapping("/topics/{id}/courses")
    public List<Course> getAllcourses(@PathVariable String id){
        return courseService.getAllCourses(id);  //getAllCourses for the topic ID
    }
    //GET
    @RequestMapping("/topics/{topicId}/courses/{id}") 

    public Course getCourse(@PathVariable String id) {
        return courseService.getCourse(id);
    }

    //POST
    @RequestMapping(method = RequestMethod.POST, value = "/topics/{topicId}/courses")
    public void addCourse(@RequestBody Course course, @PathVariable String topicId) { 
        course.setTopic(new Topic(topicId, "", ""));
        courseService.addCourse(course);
    }

    //PUT
    @RequestMapping(method = RequestMethod.PUT, value = "/topics/{topicId}/courses/{id}")
    public void updateCourse(@RequestBody Course course,  @PathVariable String id,  @PathVariable String topicId) { 
        course.setTopic(new Topic(topicId, "", ""));
        courseService.updateCourse(course);

    }
    //DELETE
    @RequestMapping(method = RequestMethod.DELETE, value = "/topics/{topicId}/courses/{id}")
    public void deletecourse(@PathVariable String id, @PathVariable String topicId) {
        courseService.deleteCourse(id);

    }
}

最后是我的服务类:

package io.msela.springbootstarter.course;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class CourseService {

    @Autowired // it injects the courseRepository as it's initialized
    private CourseRepository courseRepository;

    public List<Course> getAllCourses(String topicId){ //getting all is not good!
        List<Course> courses = new ArrayList<>() ;
        courseRepository.findByTopicId(topicId).forEach(courses::add);
        return courses;
    }

    public Course getCourse(String id) {
        return courseRepository.findOne(id);
    }

    public void addCourse(Course course) {
        courseRepository.save(course); //save a course to the database
            }

    public void updateCourse(Course course) {
        courseRepository.save(course);
        //save does both add and update
        }


    public void deleteCourse(String id) {
        courseRepository.delete(id);
    }
}

编辑:这也是我的 pom.xml 文件

http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4.0.0

<groupId>io.msela</groupId>
<artifactId>course-api-data</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>

<name>course-api-data</name>
<description>Course API with Spring Data</description>

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.5.4.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.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

    <dependency>
        <groupId>org.apache.derby</groupId>
        <artifactId>derby</artifactId>
        <scope>runtime</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>
    </plugins>
</build>

编辑 2

实际错误似乎如下:

上下文初始化期间遇到异常 - 取消 刷新尝试: org.springframework.beans.factory.UnsatisfiedDependencyException: 创建名为“courseController”的 bean 时出错:不满意 通过字段“courseService”表达的依赖关系;嵌套异常 是 org.springframework.beans.factory.UnsatisfiedDependencyException: 创建名为“courseService”的 bean 时出错:不满足的依赖关系 通过字段“courseRepository”表示;嵌套异常是 org.springframework.beans.factory.BeanCreationException:错误 创建名为“courseRepository”的 bean:调用 init 方法 失败的;嵌套异常是 java.lang.IllegalArgumentException: could 不为方法 public abstract java.util.List 创建查询元模型 io.msela.springbootstarter.course.CourseRepository.findByName(java.lang.String)!

【问题讨论】:

  • Word 'sa' .... 嗯 ... 一种可能是错误的连接字符串、休眠方言等。显示 persistence.xml
  • 你声明spring.datasource.url了吗?如果是,那是什么?连接字符串在哪里声明?
  • 我不确定 datasource.url 是什么?

标签: java spring spring-boot spring-data


【解决方案1】:

此错误仅出现在 Derby 中。这是因为您的属性默认设置为:

spring.jpa.hibernate.ddl-auto=create-drop

您必须将其设置为:

spring.jpa.hibernate.ddl-auto=update

更多信息可以在这里找到:https://github.com/spring-projects/spring-boot/issues/7706

【讨论】:

  • 这个答案应该被接受为正确的答案,因为它有效地解决了带有 Derby 的 spring-data-jpa 的问题。谢谢多米尼克。它通过编辑我的 application.properties 文件并添加这一行来解决我的问题。
  • 这也解决了我的问题,但我不明白它是如何工作的,因为更新假设(如果我错了,请纠正我)有一个数据库方案,但事实并非如此,因此我认为需要先创建它
  • 文件路径:src/main/resources/application.properties
【解决方案2】:

为spring boot添加以下属性

**#JPA**
      spring.jpa.generate-ddl=true
      spring.jpa.hibernate.ddl-auto=update
      spring.jpa.database=default
      spring.jpa.show-sql=true
**#DATASOURCE**
     spring.datasource.continue-on-error=false
     spring.datasource.generate-unique-name=false
     spring.datasource.username=app

【讨论】:

    【解决方案3】:

    你需要像这样定义表名和列名

    @Table(name="Topic")
    public class Topic {
        @Id
        @Column(name="topicId")
        private String id;
    

    【讨论】:

      【解决方案4】:

      是的,我在观看his 视频时也遇到了同样的问题。

      我刚刚在application.properties中做了如下修改:

      #JPA
        spring.jpa.generate-ddl=true
        spring.jpa.hibernate.ddl-auto=update
        spring.jpa.database=default
        spring.jpa.show-sql=true
      

      谢谢。

      【讨论】:

        猜你喜欢
        • 2014-04-05
        • 2014-12-29
        • 2015-08-08
        • 2020-04-17
        • 2019-08-16
        • 2017-07-27
        • 2012-03-16
        • 2014-08-23
        • 1970-01-01
        相关资源
        最近更新 更多