views:

209

answers:

5

I am mapping my database tables to my java objects. Generally I name my tables in the plural form in that a table holding books information is called BOOKS. The java object represents however one book and should be called Book. Similarly for AUTHORS/Author etc.

On the other hand, its kind of simplistic to give the same to the domain object and the table.

Is there some kind of naming convention that people follow? I guess this applies to applications in general and not just while doing O/R mapping.

+8  A: 

Your initial thoughts are spot on.

Objects should be singular, as each object is individual.

Tables should be plural, as the table contains all.

Check out the naming conventions built into Ruby on Rails, they're relevant.

Bevan
I was typing a very similar response as this one. In the web world, Ruby on Rails is sort of the standard on how it handles ORM, so looking there for answers is a great suggestion. +1 :-)
Topher Fangio
You could argue that table names should be singular since each row represents a single instance of the entity.
GreenieMeanie
@GreenieMeanie - my choice to name tables with plural comes from wanting my SQL to read more naturally. To me "Select * from Persons where ..." reads more naturally than "Select * from Person where ...". YMMV.
Bevan
Plural table names also leads to sql that looks like "where users.email = " or "where accounts.value in (x,y,z)" . Also, "select user.name, user.email from user" is nicer to read than "select users.name, users.email from users"
sal
@GreenieMeanie you are mixing the terms row and table. Yes, if we are talking about a row, then the name should be Person (i.e. Person row). However, since a table holds multiple rows, and we are talking about a table, then the name should be Persons (i.e. Persons table).
Gary Kephart
+1  A: 

I use SubSonic in my ASP.NET application, and I have it strip the plurals when naming the ActiveRecord classes. It's more a matter of style than a standard.

I prefer working with Invoice rather than Invoices because I'm usually dealing with 1 record at a time.

Robert S.
A: 

I usually just make sure i use the same standard everywhere, and also that i use logical names for my namings.

So Books become something like DbBooks, Authors becomes DbAuthors etc.

Silfverstrom
+5  A: 

We use singular for table names and for OM classes. It makes more sense, to me, to say

person.last_name

than

people.last_name,

whether I'm writing SQL or Java (where, of course, it would be person.lastName, but you get the point).

Carl Manaster
The benefit to singular named tables is that you never have to deal with pluralization issues: 's' or 'es', -f+'ves' (Car/cars, Bus/busses, Wolf/wolves), and a host of irregulars (Child/children, Goose/geese, Moose/Moose). Rails has a special rule system to deal with this which is configurable per project, but I see it as unnecessary hassle.
Chadwick
A: 

CJ Date does not use Plural names and neither should you. The only exception is the word "SALES". Other than that, use singular names.

compare

user.email = ? and account.value in (1,2,3)

to

users.email = ? and accounts.value in (1,2,3)

or (the worst option)

users.email = ? and account.values in (1,2,3)

sal