toddeTV
toddeTV

Reputation: 1517

Hibernate create table with name escaped

I have a Entity class like the following:

@Entity
public class perm {

    @Id
    @GeneratedValue
    private Long id;

    private boolean add;

    // Getter & Setter are also there ...
}

When I start spring (with JPA hibernate), he generates the following CREATE TABLE statement for my MySQL database:

create table perm (id bigint not null auto_increment, add bit not null, primary key (id))

This create will fail, because the column name add is not escaped with

`

like it normally should be.
Normally I would create it manually with the following code:

create table perm (`id` bigint not null auto_increment, `add` bit not null, primary key (`id`))

Of course I could create a ImprovedNamingStrategy in order to manipulate all the column names by setting a prefix or suffix to them. But then all my columns have this syntax.

Now my question:
Is there maybe a jpaProperty that escapes a column name, everytime it is used inside a sql syntax? At first I thought, that this will already be done, when I set my

jpaProperties.put("hibernate.dialect", "org.hibernate.dialect.MySQLDialect");`

But apparently this is not the case. Maybe there is another setting for this?

Upvotes: 0

Views: 1346

Answers (2)

Evgeni Dimitrov
Evgeni Dimitrov

Reputation: 22506

Add @Column annotation to the field:

@Entity
public class perm {

    @Id
    @GeneratedValue
    private Long id;
    @Column(name="`add`")
    private boolean add;

    // Getter & Setter are also there ...
}

Also consider using a better name for the column. This may cause problems somewhere else.

Upvotes: 2

Rocherlee
Rocherlee

Reputation: 2781

add is a reserved keyword in Mysql as you can see here https://dev.mysql.com/doc/refman/5.7/en/keywords.html

You are better to avoid using it. Using isAdded may be an alternative.

In case you cannot change the column name, use this to instruct Hibernate:

@Column(name="[add]")
private boolean add;

Upvotes: -1

Related Questions