java - 无法提取 ResultSet,与 String 相关为

标签 java spring hibernate spring-boot jpa

Spring 的错误消息:

: GenerationTarget encountered exception accepting command : Error executing DDL "create table category_class (category_name varchar(255) not null, primary key (category_name)) engine=MyISAM" via JDBC Statement



: GenerationTarget encountered exception accepting command : Error executing DDL "alter table book_class add constraint FKqov24n7bmx5f0s8d74tdxbgbk foreign key (book_category) references category_class (category_name)" via JDBC Statement

无法使用 Spring Boot 和 Hibernate 来精确结果集。

 Caused by: java.sql.SQLSyntaxErrorException: Table 'myDatabase.category_class' doesn't exist

我的所有其他表都显示正确,但是我的类别类确实有一个字符串作为 PK,这可能是问题的原因。 当我尝试通过我的categoryService 和扩展了JPARepository 的CategoryRepository 使用findAll 访问mysql 数据库时,会发生错误。

--Category.java--

package myPackage;
import lombok.Data;
import lombok.NoArgsConstructor;

import javax.persistence.*;
import java.util.List;

@Data
@NoArgsConstructor
@Entity
@Table(name="category_class")
public class Category {
        @Id
        @Column(name = "categoryName")
        private String name;

        @OneToMany(mappedBy = "category")
        private List<Book> books;
}

--application.properties--

spring.datasource.url= jdbc:mysql://localhost:3306/oblig2_v3
spring.datasource.username=
spring.datasource.password =
spring.datasource.testWhileIdle=true
spring.datasource.validationQuery=SELECT 1
spring.jpa.generate-ddl=true
spring.jpa.show-sql=false
spring.jpa.hibernate.ddl-auto=create
spring.jpa.hibernate.naming-strategy=org.hibernate.cfg.ImprovedNamingStrategy
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
server.port=8080

有谁知道为什么我的表没有在 MySQL 中创建?

编辑:添加了书籍类&&清理。

---Book.java---

import lombok.Data;
import lombok.NoArgsConstructor;

import javax.persistence.*;
import java.util.List;

@Data
@NoArgsConstructor
@Entity
@Table(name="book_class")
public class Book {
        @Id
        @GeneratedValue(strategy = GenerationType.AUTO)
        private long ISBN; //PK
        private String title;
        private String releaseYear;
        private int quantity;

        @ManyToOne
        @JoinColumn(name ="book_category")
        private Category category;

        @ManyToOne
        @JoinColumn(name ="author")
        private Author author;

        @ManyToMany(mappedBy = "books")
        private List<Order> orders;

}

最佳答案

您需要使用该属性

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

您的情况的问题是您已经创建了数据库,并且它没有尝试更新它。

您有两个选择:

  1. 使用更新,这将使 Spring 浏览您架构的元数据并尝试应用必要的更改。
  2. 每次删除并重新创建架构 - 这在开发阶段或单元/集成测试场景中可能是需要的。

根据 spring 文档,该属性的以下值是有效的:

You can set spring.jpa.hibernate.ddl-auto explicitly and the standard Hibernate property values are none, validate, update, create, and create-drop. Spring Boot chooses a default value for you based on whether it thinks your database is embedded. It defaults to create-drop if no schema manager has been detected or none in all other cases. An embedded database is detected by looking at the Connection type. hsqldb, h2, and derby are embedded, and others are not. Be careful when switching from in-memory to a ‘real’ database that you do not make assumptions about the existence of the tables and data in the new platform. You either have to set ddl-auto explicitly or use one of the other mechanisms to initialize the database.

关于java - 无法提取 ResultSet,与 String 相关为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54709972/

相关文章:

java - Spring 4 Jackson 数组映射

java - 为什么我的代码不更新我的实体对象? Spring Hibernate MVC 应用程序

java - Android 有什么更好的?定时器还是闹钟?

java - data[i++] = data[i++] * 2 结果令我惊讶

java - 使用 ServerSocket 类对方法进行单元测试的最佳方法是什么?

java - hibernate 注释

java - 如何限制hibernate criteria API获得的结果?

java - Android 应用程序上的 Google +1 按钮错误是暂时的

java - @Autowired 处理器为空

java - Spring Boot 使用 ConfigurationProperties 从 .properties 和 .yaml 注入(inject)映射值