The Artima Developer Community
Sponsored Link

Java Buzz Forum
Hibernate ImprovedNamingStrategy

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
dion

Posts: 5028
Nickname: dion
Registered: Feb, 2003

Dion Almaer is the Editor-in-Chief for TheServerSide.com, and is an enterprise Java evangelist
Hibernate ImprovedNamingStrategy Posted: Jan 19, 2005 12:33 PM
Reply to this message Reply

This post originated from an RSS feed registered with Java Buzz by dion.
Original Post: Hibernate ImprovedNamingStrategy
Feed Title: techno.blog(Dion)
Feed URL: http://feeds.feedburner.com/dion
Feed Description: blogging about life the universe and everything tech
Latest Java Buzz Posts
Latest Java Buzz Posts by dion
Latest Posts From techno.blog(Dion)

Advertisement
By default, a Hibernate class named FooBar will become a table named FooBar. A lot of the time you don't want this behaviour as: You are mapping to existing schema which has different table names (e.g. foo_bar) You have corporate standards You just don't like camel case for DB items I have seen a lot of projects where the table name is manually added in: @hibernate.class table="foo_bar" ... @hibernate.property column="last_updated" That is fine of course, but if you want to do it across the board you can nicely use the net.sf.hibernate.cfg.ImprovedNamingStrategy. You need to set this up in Hibernates configuration. It isn't a property (so you don't just put it in the hibernate.properties file). Instead you need to have setNamingStrategy(...) called. If you are using Spring to manage hibernate you can just do the following: <bean id="sessionFactory" class="org.springframework.orm.hibernate.LocalSessionFactoryBean"> <property name="dataSource"><ref local="dataSource" /></property> <!-- Must references all OR mapping files. --> <property name="mappingDirectoryLocations"> <list> <value>classpath:/homeabroad/model</value> </list> </property> <!-- Set the various Hibernate properties. E.g. type of database; changing this one property will port this to Oracle, MS SQL etc. --> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect">${jdbc.hibernate.dialect}</prop> <prop key="hibernate.hbm2ddl.auto">${jdbc.hibernate.hbm2ddl.auto}</prop> <prop key="hibernate.show_sql">${jdbc.hibernate.show_sql}</prop> </props> </property> <property name="namingStrategy"><ref bean="namingStrategy"/></property> </bean> <bean id="namingStrategy" class="net.sf.hibernate.cfg.ImprovedNamingStrategy"/> This is one naming strategy, which handles a simple CamelCase to camel_case conversion. You can even write your own to do more complicated things. I saw a mapper like this on another ORM product which was even smart enough to do mappings such as making the tables plural. It even handled the case: Person class to people table

Read: Hibernate ImprovedNamingStrategy

Topic: Dealing With Information Overload In Emails Previous Topic   Next Topic Topic: Apache Incubator gets portal based CMS

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use