user170008
user170008

Reputation: 1066

Hibernate throws SQLGrammarException

The entity class is :

@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

}

I am trying to persist a MovieDetail entity with only cast set. Rest of the fields are null.

Hibernate throws the below exception:

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

In my 'database.properties' , I have :

jdbc.dialect=org.hibernate.dialect.MySQL5Dialect

I am using a MySQL Community Server 5.6.20. and the mysql java driver version dependency defined in maven's pom.xml is

 <dependency>
     <groupId>mysql</groupId>
     <artifactId>mysql-connector-java</artifactId>
     <version>5.1.28</version>
 </dependency>

Am I missing anything here?

Upvotes: 0

Views: 516

Answers (1)

Arturo Volpe
Arturo Volpe

Reputation: 3637

You have a column named desc, and desc is a reserved work in MySql (and many other databases).

You can:

  • From this and this you can make hibernate escape all column and table names: hibernate.globally_quoted_identifiers=true (in your persistence.xml, or hibernate configuration.
  • Change the name of the column
  • From this you can escape only this identifier using:

@Column(name="\"desc\"")

Also, if is not a legacy database, please consider renaming.

Upvotes: 2

Related Questions