Alex Rivera | Logout

Best practice: best database naming convention for JPA?

Asked 2010-10-15T18:33:51.910
15

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?

Edit
Report

2 Answers

2

Follow both. The db convention should be there for DBA sake and manual reports and queries where the mind set is different. Use the name params on annotations to achieve this.

answered 2010-10-15T18:56:05.757
2

As far as I'm concerned either are acceptable. But if you decide you don't want the default camel case, you CAN get a different naming strategy without resorting to the tedious and error-prone task of adding the name attribute to every annotation.

Take a look at Hibernate's org.hibernate.cfg.ImprovedNamingStrategy class. It uses underscores instead of camel case. It is simply a matter of setting a property on your Hibernate configuration to use it.

You could also extend the ImprovedNamingStrategy to prepend the table name or do all uppercase if you really want, but that seems unnecessary.

answered 2010-10-15T21:28:35.317

Your Answer