@Entity
@Table(name="jobs")
public class Job implements Serializable {
…
However, there are hooks to change the default Hibernate NamingStrategy implementation. This means, if you don't provide the @Table annotation then the class name maps directly to the table name.
In fact Hibernate comes with 2 naming strategy implementations out of the box:
- org.hibernate.cfg.DefaultNamingStrategy
- org.hibernate.cfg.ImprovedNamingStrategy
(separates camel case fragments by an underscore)
String result = Noun.pluralOf(“cat”) ; // = cats
As a result of that find, I created my own class ImprovedPluralizedNamingStrategy which uses ImprovedNamingStrategy as a base. I refactored the contained method classToTableName() so that it returns a pluralized version of the class name.
Also, if you like to know more on how to configure Hibernate naming strategies when using Spring, check out this blog.
What I have done is certainly not production ready but it shows that adding your custom naming strategy to Hibernate is fairly easy to accomplish and helps reducing annotation clutter in my classes.
No comments:
Post a Comment