gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 05:46:41 25 4
gpt4 key购买 nike

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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com