Nico Huysamen
Nico Huysamen

Reputation: 10417

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?

Upvotes: 1

Views: 379

Answers (2)

octav
octav

Reputation: 1181

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

Upvotes: 2

Ralph
Ralph

Reputation: 120781

You could try to implement your own org.hibernate.cfg.NamingStrategy, which add the backticks to all tables. -- But I am sure that this abuse the NamingStrategy class.

Upvotes: 0

Related Questions