Search code examples
mysqlhibernatespringobject-persistence

MySQL table creation with Spring + Hibernate


We came across the following situation.

Please note that I know reserved words should not be used for table names, but I'm asking the question anyway out of curiosity more than anything.

We are using Spring + Hibernate to manage our database. I am adding a new model to the database called Group. So, I define my model as:

@Entity
@Table(name = "group")
public class Group {
    ...
}

Now, the problem is, when recreating the tables, the SQL that gets generated looks as follows:

create table group (...)

This unfortunately is not allowed in MySQL since group is a reserved word. The correct SQL should be:

create table `group`(...)

Is there any way for me to do this?


Solution

  • You can force Hibernate to escape identifiers by doing this:

    http://docs.jboss.org/hibernate/core/3.3/reference/en/html/mapping.html#mapping-quotedidentifiers

    Basically, you can quote them yourself, and then Hibernate will use the appropriate quoting technique according to the dialect