我做了简单的春季网络应用程序,看起来像博客(有笔记和用户)。首先,当我使用保存在我的*** inMemoryRepository.java文件中的数据时,前端和后端工作正常。 现在我正在尝试改变这个工作人员使用mysql数据库,我卡住了。 通常我想从这两个数据库加载所有数据。
控制台出错:
com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Unknown column 'note0_.author_id' in 'field list'
Console Hibernate日志:
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),
...... ;
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> {
}
答案 0 :(得分:1)
您的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
,并且应该包含用户表的id
。