Set brackets in query

16 views
Skip to first unread message

Laurentb0204

unread,
Dec 20, 2011, 1:02:34 PM12/20/11
to nhusers
Hi,

I have create a table "USER" in my DataBase, but when NHibernate can't
execute a query with this table name. USER is a keyword of SqlServer.
Below, the query that NHibernate try to execute :
SELECT this_.ID as ID65_0_, this_.LOGIN as LOGIN65_0_, this_.PASSWORD
as PASSWORD65_0_, ...
FROM USER this_
WHERE this_.LOGIN = @p0 and this_.PASSWORD = @p1
I want to force put a bracket around table name and/or fields but I
haven't find anything. There are a option which can this add ?

Thanks,

Laurent

sbohlen

unread,
Dec 20, 2011, 1:10:30 PM12/20/11
to nhu...@googlegroups.com
Actually, you don't want square brackets [ ] because that is an escape sequence specific to SQL Server and won't be portable across different databases (one of the points of an ORM like NH).  Instead, you want the ` (back-tick) which is what NH uses to escape reserved words in a DB-portable manner.  So instead of [User] you actually want `User`.

You can either do this on a case-by-case basis (if you are using XML mapping files) or set it globally in the hibernate.cfg file by adding the following entry: <property name="hbm2ddl.keywords">auto-quote</property>

-Steve B.

TheCPUWizard

unread,
Dec 20, 2011, 5:03:41 PM12/20/11
to nhu...@googlegroups.com

Or better yet, do not use tables that are known reserved words (including other potential RDBMS systems). Even though the information from Steve will address this particular issue, there are likely to be other problems down the road (especially when other applications/tools need to work with the DB schema!)

--
You received this message because you are subscribed to the Google Groups "nhusers" group.
To view this discussion on the web visit https://groups.google.com/d/msg/nhusers/-/IY4BgpO9dboJ.
To post to this group, send email to nhu...@googlegroups.com.
To unsubscribe from this group, send email to nhusers+u...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/nhusers?hl=en.

Reply all
Reply to author
Forward
0 new messages