3

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  {
2
  • Call the table Users, since it is going to store a set of users, and in addition this will not conflict with a reserved word? I understand that in the entity model the thing is a user, but you need to draw a line between the app and the schema somewhere. Commented Aug 13, 2013 at 13:33
  • @Reimeus that's because you're looking at MySQL documentation. The question is about SQL Server. Try here. Commented Aug 13, 2013 at 13:35

2 Answers 2

5

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

Sign up to request clarification or add additional context in comments.

Comments

1

I should use grave accent (`). for use keyword in hibernate we have to use (`) around keyword.

@Entity
@Table(name = "`user`")
public class CompanyUser extends

2 Comments

It didn't work with oracle,Exception in thread "main" org.hibernate.exception.SQLGrammarException: Could not execute JDBC batch update
In Oracle and JPA 2.0 it works with double qouts. for ex: @Table(name="\"user\"")

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.