【发布时间】:2017-06-27 17:51:55
【问题描述】:
我做了一个简单的 Spring Web 应用程序,它看起来像博客(有注释和用户)。首先,当我使用保存在我的 ***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 脚本“用户”表:
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'表:
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),
...... ;
用户.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> {
}
【问题讨论】:
标签: mysql hibernate jpa spring-boot hql