com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException:'field list' 中的未知列“____”

com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Unknown column '____' in 'field list'

我做了简单的 spring 看起来像博客的网络应用程序(有注释和用户)。首先,当我使用保存在我的 ***inMemoryRepository.java 文件中的数据时,前端和后端工作完美。 现在我正在尝试将此员工更改为使用 mysql 数据库,但我被卡住了。 通常我想从这两个数据库加载所有数据。

控制台错误:

com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Unknown column 'note0_.author_id' in 'field list'

控制台休眠日志:

Hibernate: 
    select
        note0_.id as id1_0_,
        note0_.author_id as author_i6_0_,
        note0_.body as body2_0_,
        note0_.date as date3_0_,
        note0_.is_done as is_done4_0_,
        note0_.title as title5_0_ 
    from
        notes note0_

浏览器错误:

There was an unexpected error (type=Internal Server Error, status=500).
could not extract ResultSet; SQL [n/a]; nested exception is org.hibernate.exception.SQLGrammarException: could not extract ResultSet

application.properties -> 我正在尝试使用自动模式但没有这条线(#)

#spring.jpa.properties.hibernate.hbm2ddl.auto = create-drop

Sql 脚本 'users' table:

USE stickyNotes_db;
DROP TABLE users;
CREATE TABLE users 
(
id BIGINT(20),
username VARCHAR(50),
firstName VARCHAR(50),
lastName VARCHAR(50),
password VARCHAR(50),
role VARCHAR(50)
);

INSERT INTO `users` (`id`, `username`, `firstName`, `lastName`, `password`, `role`) VALUES
    (1, 'user1',    'Adam1',    'Mock1',    'pass1',    'USER'),
    ...... ;

Sql 脚本 'notes' table:

USE stickyNotes_db;
DROP TABLE notes;
CREATE TABLE notes 
(
id BIGINT(20),
author VARCHAR(50),
title VARCHAR(100),
body VARCHAR(500),
date DATETIME(0),
isDone BOOLEAN not null default 0
);

INSERT INTO `notes` (`id`, `author`, `title`, `body`, `date`, `isDone`) VALUES
    (1, 'user1',    'Title1',   'Lorem ipsum.', '2017-02-08 00:00:00',  0),
    ...... ;

User.java

@Entity
@Table(name = "users")
public class User {

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

    @Column(name = "username")
    private String userName;

    @Column(name = "firstName")
    private String firstName;

    @Column(name = "lastName")
    private String lastName;

    @Column(name = "password")
    private String password;

    @OneToMany(mappedBy = "author")
    private Set<Note> notes = new HashSet<>();

    ...constructors, getters/setters

Note.java

@Entity
@Table(name = "notes")
public class Note {

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

    @ManyToOne(optional = false, fetch = FetchType.LAZY)
    private User author;

    @Column(name = "title")
    private String title;

    @Column(name = "body")
    private String body;

    @Column(name = "date")
    private Date date = new Date();

    @Column(name = "isDone")
    private boolean isDone;

    ...constructors, getters/setters

NoteDAO.java

@Repository
public interface NoteDAO extends JpaRepository<Note, Long> {

    @Query("FROM Note")
    Page<Note> findAll(Pageable pageable);

}

UserDAO.java

@Repository
public interface UserDAO extends JpaRepository<User, Long> {

}

您的 Hibernate 查询表明

note0_.author_id as author_i6_0_,

这意味着它正在尝试设置 author_id 并且您的数据库脚本没有定义任何外键约束来定义它们之间的关系。

此外,您的数据库架构不正确,与您的休眠配置不匹配。

INSERT INTO `notes` (`id`, `author`, `title`, `body`, `date`, `isDone`) VALUES
    (1, 'user1',    'Title1',   'Lorem ipsum.', '2017-02-08 00:00:00',  0),
    ...... ;
INSERT INTO `users` (`id`, `username`, `firstName`, `lastName`, `password`, `role`) VALUES
    (1, 'user1',    'Adam1',    'Mock1',    'pass1',    'USER'),
    ...... ;

在上面,您的 author 应该是 author_id 并且应该包含用户 table.

id