• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Paul Clapham
  • Liutauras Vilda
  • Knute Snortum
  • Bear Bibeault
  • Devaka Cooray
  • Jeanne Boyarsky
  • Junilu Lacar
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • salvin francis
  • Tim Holloway
  • Piet Souris
  • Frits Walraven

problem with Hibernate and table named "users"

Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

I have a somewhat annoying problem. In my database (MSSQL) I have a "users" table. The convention for hibernate seems to be to name the class as the singular so I have "data.User" as a classname. When I issue a query:

String query =
"select user from user "
+ "in class data.User "
+ "where username=:name";
and call:
List list = sess.find(query, name, Hibernate.STRING);

I get an SQL exception (invalid use of keyword USER).

It works if I change the classname + xml files to use "data.XUser" but should I really have to? Is there some way to tell hibernate about keywords or should I just use a different convention for class naming?

Ranch Hand
Posts: 166
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My recommendation is to not use the table name user. modify it to something that is prefixed. USER is a keyword in MSSQL and you have weird things happen if you continue to use it. In select statements within query analyzer you must specify it as ["user"] for example. This is what your hibernate query is attempting to use in the select statement.
Ranch Hand
Posts: 995
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dan, Hibernate was takeing this approach as it is a normal naming convention: the table represents the collection of User tuples. It is quite common to use singular for naming tables/entities.
You are not talking about how you obtain the classname + hbm.xml files? Are you using Middlegen? Are you using the Hibernate tool for this?

What do you have to say for yourself? Hmmm? Anything? And you call yourself a tiny ad.
Java file APIs (DOC, XLS, PDF, and many more)
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!