实体类是:
@Entity
@Table(name = "movieDetail")
public class MovieDetailImpl implements MovieDetail {
@Id
// primary key
@Column(name = "idmovieDetail")
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;
@Column(name = "cast")
private String cast;
@Column(name = "producer")
private String producer;
@Column(name = "director")
private String director;
@Column(name = "trailer")
private URL trailer;
@Column(name = "photo")
private URL photo;
@Column(name = "plot")
private URL plot;
@Column(name = "desc")
private String desc;
@Column(name = "moreDetails")
private URL moreDetails;
// Getters/Setters
}
我正在尝试保留仅设置 cast
的 MovieDetail
实体。其余字段为 null
。
Hibernate 抛出以下异常:
014-08-17 21:47:35 INFO TransactionFactoryInitiator:62 - HHH000399: Using default transaction strategy (direct JDBC transactions)
2014-08-17 21:47:35 INFO ASTQueryTranslatorFactory:47 - HHH000397: Using ASTQueryTranslatorFactory
2014-08-17 21:47:36 INFO HibernateTransactionManager:341 - Using DataSource [org.springframework.jdbc.datasource.DriverManagerDataSource@1fba434a] of Hibernate SessionFactory for HibernateTransactionManager
Hibernate: insert into movieDetail (cast, desc, director, moreDetails, photo, plot, producer, trailer) values (?, ?, ?, ?, ?, ?, ?, ?)
2014-08-17 21:47:36 WARN SqlExceptionHelper:144 - SQL Error: 1064, SQLState: 42000
2014-08-17 21:47:36 ERROR SqlExceptionHelper:146 - 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 'desc, director, moreDetails, photo, plot, producer, trailer) values ('testGetAll' at line 1
2014-08-17 21:47:36 INFO GenericApplicationContext:873 - Closing org.springframework.context.support.GenericApplicationContext@40266966: startup date [Sun Aug 17 21:47:34 PDT 2014]; root of context hierarchy
在我的“database.properties”中,我有:
jdbc.dialect=org.hibernate.dialect.MySQL5Dialect
我使用的是 MySQL 社区服务器 5.6.20。而maven的pom.xml中定义的mysql java驱动版本依赖是
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.28</version>
</dependency>
我在这里遗漏了什么吗?
最佳答案
您有一个名为 desc
的列,并且 desc
是 MySql(以及许多其他数据库)中的保留作品。
你可以:
- 来自 this和 this您可以使 hibernate 转义所有列和表名称:
hibernate.globally_quoted_identifiers=true
(在您的persistence.xml
或 hibernate 配置中。 - 更改列的名称
- 来自 this您可以使用以下方法仅转义此标识符:
@Column(name="\"desc\"")
此外,如果不是旧数据库,请考虑重命名。
关于java - Hibernate 抛出 SQLGrammarException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25356600/