Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create user table name with hibernate

I would like to create an object in MS SQL with hibernate, name of this table is "user". it does not work!. I think this problem may caused by name of table/entity, user is keyword. what should I do to have table with name "user"?

@Entity
@Table(name = "user")
public class User  {
like image 499
Emad Aghayi Avatar asked Mar 13 '26 15:03

Emad Aghayi


1 Answers

Enclose the table name in square braces:

@Entity
@Table(name = "[user]")
public class User  {

That way Hibernate will escape it when it creates the table. If you put all your field and table names in square braces as a rule, you never have to consider the underlying DBMS when setting up your mappings.

<rant>It always bothered me that you had to do this. One of the major goals of Hibernate is to isolate your code from the underlying database -- I never understood why the dialect implementations don't properly handle escaping reserved keywords.</rant>

See also: Using database reserved keywords in Hibernate

like image 145
Jason C Avatar answered Mar 16 '26 04:03

Jason C



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!