*
The moose likes Object Relational Mapping and the fly likes problem with Hibernate and table named Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "problem with Hibernate and table named "users"" Watch "problem with Hibernate and table named "users"" New topic
Author

problem with Hibernate and table named "users"

Dan Howard
Greenhorn

Joined: Oct 10, 2004
Posts: 15
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

Joined: Feb 18, 2003
Posts: 166
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.


Jeremy Wilson
Alexandru Popescu
Ranch Hand

Joined: Jul 12, 2004
Posts: 995
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


blog - InfoQ.com
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: problem with Hibernate and table named "users"