Dave
Dave

Reputation: 5173

Should I specify names via @Column and @Table always or only when necessary

This is more of a question of best practice, hoping to learn from others experiences. Would you recommend always specifying the names of columns and tables explicitly using @Column(name=) and @Table(name=)? I'm slightly worried that some implementation detail could change. Also, specifying it might provide some resilience against refactoring my code. I also think it might just make the code more clear to explicitly state names. On the other hand, it makes the code more verbose where it many not need to be.

Upvotes: 3

Views: 896

Answers (2)

home
home

Reputation: 12538

My 2 cent: For a given project do it always or never, but do not mix up things. In large organizations I've seen (useful?) database design guidelines that force you to define tables and columns based on certain naming conventions, e.g. Project acronym_Counter_Table. This results in tablenames like MYPROJ_CUSTOMER_1, no question this shouldn't be our Java classes' name.

BTW: I'm a fan of orm.xml which enables you to remove database specifics from your java classes and bring it back to XML. I know this is not trendy :-)

Upvotes: 1

billygoat
billygoat

Reputation: 21984

Since this is an question on best practice, I can only share my perspective. According to me, the key question is "How fluid is your database design?" (which probably boils down to how fluid is your domain model).

Either way, I will provide table name and column name always. ( this provides a certain degree of clarity). If I had written an abstraction layer on top of entity ( which groups entity access as functional units), then I can, to a certain degree handle the changes in my entity design gracefully.

Not providing table and column name just reduces the readability of code and with proper design, we can make sure that the name does not cause any undue resilience while refactoring. I should be able to regenerate my entity model and the refactoring could be done in the abstraction layer gracefully. Hope I have expressed my thoughts clearly.

Upvotes: 5

Related Questions