尝试学习hibernate,给出错误的sql语法错误



我正在练习JPA/Hibernate,当运行以下代码:

package com.newjpa;
import javax.persistence.*;
public class App 
{
public static void main( String[] args ) {
EntityManagerFactory emf = Persistence.createEntityManagerFactory("book");
EntityManager em = emf.createEntityManager();
EntityTransaction et = em.getTransaction();
et.begin();
Book book = new Book();
book.setAuthor("1423414");
book.setTitle("#143441");
book.setRating(4);
book.setYear(2093);
book.setDescr("13471863146486348626424372");
em.merge(book);
et.commit();
em.close();
emf.close();
}
}

和Book类(不含getter和setter):

package com.newjpa;
import javax.persistence.*;
@Entity
@Table(name = "Books")
public class Book {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private long bookId;
private String author;
private String title;
private int year;
private int rating;
private String descr;

public Book(String author, String title, int year, int rating, String descr) {
this.author = author;
this.title = title;
this.year = year;
this.rating = rating;
this.descr = descr;
}
public Book() {
}
}

我得到以下错误:

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'value for hibernate_sequence' at line 1

我真的不知道这是什么意思,也不知道怎么解决,我花了几个小时搜索,没有找到任何类似的东西。persistence . xml代码:

<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence" version="1.0">
<persistence-unit name="book" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.jpa.HibernatePersistenceProvider
</provider>
<class>com.newjpa.Book</class>
<properties>
<property name="javax.persistence.jdbc.url  " 
value="jdbc:mysql://www.remotemysql.com:3306/placeholder" />
<property name="javax.persistence.jdbc.driver" 
value="com.mysql.jdbc.Driver" />
<property name="javax.persistence.jdbc.user" 
value="placeholder" />
<property name="javax.persistence.jdbc.password" 
value="placeholder" />
<property name="hibernate.dialect" 
value="org.hibernate.dialect.H2Dialect" />
<property name="hibernate.show_sql" value="true" />
<property name="hibernate.format_sql" value="true" />
<property name="hibernate.hbm2ddl.auto" value="create" />
</properties>
</persistence-unit>
</persistence>

我在这里找不到任何错误——我在互联网上的许多地方看到了非常相似的代码。正如另一个用户指出的,下面是hibernate生成的sql语句:创建表Books ()bookId不为空,作者varchar (255),备注说明varchar (255),评级整数不为空,标题varchar (255),年份整数不为空,主键(bookId)(值得指出的是,表是创建的,问题是异常,我创建的实体没有插入到表中。重要的更新:我发现hibernate也在这一行之前生成这一行:

Hibernate: create sequence hibernate_sequence start with 1 increment by 1

和表创建后的

Hibernate: 
call next value for hibernate_sequence

这似乎与问题有关

我发现我有两个问题:我有错误的方言集(org.hibernate.dialect)。事实证明,当使用MySQL 8时,你必须从"mysqldialect"更改方言。";MySQL8Dialect。

相关内容

  • 没有找到相关文章

最新更新