Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

problem with Hibernate and table named "users"

 
Dan Howard
Greenhorn
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?

TIA
 
Jeremy Wilson
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.
 
Alexandru Popescu
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?

./pope
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic