简体   繁体   English

Hibernate 使用 MySQL 生成无效的 SQL 查询

[英]Hibernate generates invalid SQL query with MySQL

I have the following JPA entity classes (example case).我有以下 JPA 实体类(示例案例)。 A House belongs on a single Street.房子属于一条街。 A Street has many Houses.一条街有很多房子。

@Entity
public class House {
    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    public Integer id;

    public String name    

    @ManyToOne
    public Street street;
}

@Entity
public class Street {
    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    public Integer id;

    @OneToMany(mappedBy="street")
    public Set<House> houses;
}

I have the generation type set to identity, which is supposed to auto assign a new ID.我将生成类型设置为身份,它应该自动分配一个新的 ID。

When creating a new House with a new Street, I have to first create and persist Street, followed by House.用新的 Street 创建新的 House 时,我必须先创建并坚持 Street,然后才是 House。 This is because I do not have CascadeType set to PERSIST, so it has to be done manually [1].这是因为我没有将 CascadeType 设置为 PERSIST,所以必须手动完成 [1]。 However, while inserting a newly created Street:但是,在插入新创建的 Street 时:

Street street = new Street();
entityManager.persist(street);

Hibernate/JPA generates the following SQL query: Hibernate/JPA 生成以下 SQL 查询:

insert into Street default values

which MySQL doesn't like. MySQL不喜欢哪个。

Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: 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 'default values' at line 1 

Any ideas why?任何想法为什么? I'm using Java 6, MySQL 5.0.67 with Hibernate's implementation of JPA (version 3.2.1.ga).我正在使用 Java 6、MySQL 5.0.67 和 Hibernate 的 JPA 实现(版本 3.2.1.ga)。

[1] EJB 3 in Action pages 318-319 [1] EJB 3 操作页 318-319

If you don't have an error in your SQL statement, then you might want to check your table column name as it can contain a predefined name that Mysql uses;如果您的 SQL 语句中没有错误,那么您可能需要检查您的表列名,因为它可以包含 Mysql 使用的预定义名称; for example 'column' which can't be used as a table column name例如不能用作表列名的“列”

Standard SQL specifies this optional syntax for INSERT :标准 SQL 为INSERT指定了以下可选语法:

INSERT INTO <Table Name> DEFAULT VALUES

This is legal SQL syntax, supported, for example, by Microsoft SQL Server , PostgreSQL , and SQLite , but not by Oracle, IBM DB2, or MySQL.这是合法的 SQL 语法,例如受Microsoft SQL ServerPostgreSQLSQLite 支持,但不受 Oracle、IBM DB2 或 MySQL 支持。

MySQL supports other syntax that achieve the same result: MySQL 支持实现相同结果的其他语法:

INSERT INTO <Table Name> () VALUES ()

INSERT INTO <Table Name> (col1, col2, col3) VALUES (DEFAULT, DEFAULT, DEFAULT)

In Hibernate, you should configure the SQL dialect properly, and it's up to Hibernate to generate valid SQL for the target RDBMS brand.在 Hibernate 中,您应该正确配置SQL 方言,并且由 Hibernate 为目标 RDBMS 品牌生成有效的 SQL。

import org.hibernate.cfg.Configuration;

Configuration cfg = new Configuration();
cfg.setProperty("hibernate.dialect", "org.hibernate.dialect.MySQLInnoDBDialect");

You can also specify properties by placing a file named hibernate.properties in a root directory of the classpath.您还可以通过在类路径的根目录中放置一个名为hibernate.properties的文件来指定属性。

Another situation when you might have this exception is when you have reserved words as columns for the table.另一种可能出现此异常的情况是将保留字用作表的列。 For example 'to' and 'from' are not suitable clumn names for MySQL.例如,'to' 和 'from' 不是 MySQL 的合适名称。 It is obviously bug in Hibernate, that it doesn't check such columns and moreover continues to work with no error even if table is not created.这显然是 Hibernate 中的错误,它不检查这些列,而且即使没有创建表,它也会继续工作而不会出错。

确保 spring.jpa.database-platform=org.hibernate.dialect.MySQL5Dialect 具有正确的 MySQL 版本

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM