MS SQL Reserved Keywords and Hibernate

Surprise of the day, it turns out that User is a reserved keyword in T-SQL. You are still allowed to use keywords as identifiers, but you will need to escape them with square brackets:

[code lang=”latex”]
CREATE TABLE [User] (Id int);
[/code]

So far, so inconvenient. But things start getting really pretty when you clicked your schema together with SQL Server Management Studio, which will hide the keyword restrictions from you. Unfortunately Hibernate is not equally generous, so if you go from there and use your generated Hibernate mapping, you will be presented with a beautiful exception:

[code lang=”java”]
org.hibernate.exception.SQLGrammarException: Incorrect syntax near the keyword ‘user’.
[/code]

The solution for my case was to escape the table name in the related User.hbm.xml to table=”[user]”. Mildly annoying caveat: now the file has to be edited manually each time you generate your mappings, since there is no option to wire this type of adjustment into the reveng.xml.