Spring Boot-derby数据库-GenerationTarget遇到异常接受命令:执行DDL“放置表连线”时出错

问题描述

我是春季靴子世界的新手。我不知道Maven Pom.xml文件中是否存在版本冲突。

GenerationTarget encountered exception accepting command : Error executing DDL "drop table wire" via JDBC Statement

org.hibernate.tool.schema.spi.CommandAcceptanceException: Error executing DDL "drop table wire" via JDBC Statement
at org.hibernate.tool.schema.internal.exec.GenerationTargetToDatabase.accept(GenerationTargetToDatabase.java:67) ~[hibernate-core-5.4.12.Final.jar:5.4.12.Final]
at org.hibernate.tool.schema.internal.SchemaDropperImpl.applysqlString(SchemaDropperImpl.java:375) [hibernate-core-5.4.12.Final.jar:5.4.12.Final]
at org.hibernate.tool.schema.internal.SchemaDropperImpl.applysqlStrings(SchemaDropperImpl.java:359) [hibernate-core-5.4.12.Final.jar:5.4.12.Final]
在运行独立的spring工具套件4.4.7.2时出现

异常

Pom.xml文件

<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 https://maven.apache.org/xsd/maven- 
4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>io.wires.springbootquickstart</groupId>
<artifactId>wires-api</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>Wires API Sample </name>

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

 <dependencies>
      <dependency>
           <groupId>org.springframework.boot</groupId>
           <artifactId>spring-boot-starter-web</artifactId>
      </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>          
     </dependency>
    <dependency>
         <groupId>org.apache.derby</groupId>
         <artifactId>derby</artifactId>         
      </dependency>

   </dependencies>
</project>

实体文件-电线类是模型层,也称为实体层 //在MVC模式中,Wire类是Model层,也称为实体层//////我们需要将Wire类的对象(即Wire类的实例)保存到

package io.wiresapi.springbootstarter.Wires.SendWires;
import javax.persistence.Entity;
import javax.persistence.Id;

@Entity
public class Wire {

//In a MVC pattern,the Wire class is the Model layer also called the entity layer
//We need to save the objects of the Wire class in other words instances of the wire class into the database

public Wire() {
    
}


public Wire(int wireId,String lPID,float wireAmount) {
    super();
    this.WireId = wireId;
    this.LPID = lPID;
    this.WireAmount = wireAmount;
}

@Id
private  int WireId;
private String LPID;
private float WireAmount;

public int getWireId() {
    return WireId;
}
public void setWireId(int wireId) {
    WireId = wireId;
}
public String getLPID() {
    return LPID;
}
public void setLPID(String lPID) {
    LPID = lPID;
}
public float getWireAmount() {
    return WireAmount;
}
public void setWireAmount(float wireAmount) {
    WireAmount = wireAmount;
}


 }

主要方法

package io.wiresapi.springbootstarter;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class WiresAPI {


public static void main(String[] args) {
    // Todo Auto-generated method stub
    
    //SpringApplication is a static class
    SpringApplication.run(WiresAPI.class,args);

  }

 }

存储库文件

package io.wiresapi.springbootstarter.Wires.SendWires;

import org.springframework.data.repository.CrudRepository;

//Spring data JPA has already an interface for all CRUM operations

 public interface WireRepository extends CrudRepository<Wire,String>{


}

控制器文件

package io.wiresapi.springbootstarter.Wires.SendWires;


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

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
 import org.springframework.web.bind.annotation.RequestBody;
 import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@RestController 
public class WiresSendController {

@Autowired
private WiresService aWireService;

//We map an incoming request from the client to an appropriate method.

//Get all wires URI
@RequestMapping("/Wires")
public List<Wire> TotalWiresSent() {
    
    return aWireService.getAllWires();
}

}

控制台

:: Spring Boot ::        (v2.2.6.RELEASE)

  2020-09-26 14:50:32.465  INFO 19332 --- [           main] io.wiresapi.springbootstarter.WiresAPI   
  : Starting WiresAPI on WN-CA14R9Y6H224 with PID 19332 (C:\Users\myname\Documents\workspace-spring- 
  tool-suite-4-4.7.2.RELEASE\wires-api\target\classes started by bxnath1 in 
  C:\Users\myname\Documents\workspace-spring-tool-suite-4-4.7.2.RELEASE\wires-api)
  2020-09-26 14:50:32.474  INFO 19332 --- [           main] io.wiresapi.springbootstarter.WiresAPI   
  : No active profile set,falling back to default profiles: default
 2020-09-26 14:50:33.961  INFO 19332 --- [           main] .s.d.r.c.RepositoryConfigurationDelegate : 
 Bootstrapping Spring Data JPA repositories in DEFAULT mode.
 2020-09-26 14:50:34.119  INFO 19332 --- [           main] .s.d.r.c.RepositoryConfigurationDelegate : 
  Finished Spring Data repository scanning in 140ms. Found 1 JPA repository interfaces.
 2020-09-26 14:50:35.301  INFO 19332 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : 
 Tomcat initialized with port(s): 3000 (http)
  2020-09-26 14:50:35.317  INFO 19332 --- [           main] o.apache.catalina.core.StandardService   
    : Starting service [Tomcat]
  2020-09-26 14:50:35.318  INFO 19332 --- [           main] org.apache.catalina.core.StandardEngine  
 : Starting Servlet engine: [Apache Tomcat/9.0.33]
 2020-09-26 14:50:35.635  INFO 19332 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : 
  Initializing Spring embedded WebApplicationContext
   2020-09-26 14:50:35.636  INFO 19332 --- [           main] o.s.web.context.ContextLoader            
   : Root WebApplicationContext: initialization completed in 3041 ms
  2020-09-26 14:50:36.051  INFO 19332 --- [           main] com.zaxxer.hikari.HikariDataSource       
 : HikariPool-1 - Starting...
 2020-09-26 14:50:36.053  WARN 19332 --- [           main] com.zaxxer.hikari.util.DriverDataSource  : 
  Registered driver with driverClassName=org.apache.derby.jdbc.EmbeddedDriver was not found,trying 
  direct instantiation.
    2020-09-26 14:50:36.914  INFO 19332 --- [           main] com.zaxxer.hikari.pool.PoolBase          
  : HikariPool-1 - Driver does not support get/set network timeout for connections. (Feature not 
   implemented: No details.)
  2020-09-26 14:50:36.920  INFO 19332 --- [           main] com.zaxxer.hikari.HikariDataSource       
 : HikariPool-1 - Start completed.
  2020-09-26 14:50:37.110  INFO 19332 --- [           main] o.hibernate.jpa.internal.util.LogHelper  
  : HHH000204: Processing PersistenceUnitInfo [name: default]
  2020-09-26 14:50:37.333  INFO 19332 --- [           main] org.hibernate.Version                    
  : HHH000412: Hibernate ORM core version 5.4.12.Final
  2020-09-26 14:50:37.772  INFO 19332 --- [           main] o.hibernate.annotations.common.Version   
  : HCANN000001: Hibernate Commons Annotations {5.1.0.Final}
  2020-09-26 14:50:38.088  INFO 19332 --- [           main] org.hibernate.dialect.Dialect            
  : HHH000400: Using dialect: org.hibernate.dialect.DerbyTenSevendialect
     2020-09-26 14:50:39.795  WARN 19332 --- [           main] o.h.t.s.i.ExceptionHandlerLoggedImpl     
   : GenerationTarget encountered exception accepting command : Error executing DDL "drop table wire" 
   via JDBC Statement

  org.hibernate.tool.schema.spi.CommandAcceptanceException: Error executing DDL "drop table wire" via 
  JDBC Statement
   at  

解决方法

由于默认值设置为spring.jpa.hibernate.ddl-auto=create-drop,因此Derby中出现此错误。 您必须在application.properties文件中将其设置为:spring.jpa.hibernate.ddl-auto=update