Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best practice: best database naming convention for JPA? [closed]

In Java the naming convention for properties en classes (entities) are done the CamelCase way:

@Entity 
public class UserMessage implements Serializable { 
    @Id 
    private Integer id; 
    private String shortTitle;
    private String longTitle;
    private String htmlMessage; 
} 

But in the SQL world it’s considered a best practice to use upper case with underscores between words (like Java constants). In the SQL world is also considered a best practice to include the table name in the column names, this way foreign keys are in most cases named exactly the same as the id in the original table.

CREATE TABLE USER_MESSAGE (
    USER_MESSAGE_ID  MEDIUMINT(8) NOT NULL,
    USER_MESSAGE_SHORT_TITLE VARCHAR(20),
    USER_MESSAGE_LONG_TITLE VARCHAR(80),
    USER_MESSAGE_HTML_MESSAGE TEXT NOT NULL
); 

Should I follow both standards and use the name attribute on @Table and @Column? Or should I follow the Java conventions and rely on the default JPA mappings.

What is the most common approach and/or the best approach on this conflict of standards?

like image 463
Kdeveloper Avatar asked Oct 15 '10 18:10

Kdeveloper


People also ask

Which are correct guidelines for naming database tables?

The rules for naming database objects (such as tables, columns, views, and database procedures) are as follows: Names can contain only alphanumeric characters and must begin with an alphabetic character or an underscore (_). Database names must begin with an alphabetic character, and cannot begin with an underscore.

How do you name a database convention?

For the traditional naming convention: Database names must only consist of the letters a to z (both lower and upper case allowed), the numbers 0 to 9 , and the underscore ( _ ) or dash ( - ) symbols. This also means that any non-ASCII database names are not allowed. Database names must always start with a letter.

What is Spring JPA Hibernate naming strategy?

Hibernate uses the Physical Naming Strategy to map our logical names to a SQL table and its columns. By default, the physical name will be the same as the logical name that we specified in the previous section. If we want to customize the physical names, we can create a custom PhysicalNamingStrategy class.

Which naming convention is used in Java?

Java uses CamelCase as a practice for writing names of methods, variables, classes, packages, and constants.


1 Answers

Should I follow both standards and use the name attribute on @Table and @Column? Or should I follow the Java conventions and rely on the default JPA mappings.

If the JPA default conventions don't match the preferred conventions of your company (there is no "one true" standard), override them. This can be done using the @Table and @Column annotations (in the particular case of Hibernate, you could also provide your own implementation of a NamingStrategy).

What is the most common approach and/or the best approach on this conflict of standards?

There is no conflict, there are Java naming conventions, there is one default convention on the JPA side for the mapping of objects to tables (because JPA had to pick one) and there is no "one true" standard on the SQL side. So:

  • if your company doesn't have any SQL naming conventions, you could use the JPA conventions
    • if you don't like them, override them
  • if your company has conventions in place, follow them and override the JPA defaults
like image 159
Pascal Thivent Avatar answered Sep 29 '22 16:09

Pascal Thivent