-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 
Author Message
 Post subject: Prefix all fields and tables from a database
PostPosted: Mon Feb 11, 2008 4:41 pm 
Newbie

Joined: Mon Feb 11, 2008 4:29 pm
Posts: 6
Hi!

I'm quite new in the Hibernate community and I have an urgent problem so I really need your help.

For the project I'm working to we have to change the persistence layer and we thought Hibernate will be the best solution. The problem is that the databases contains tables and fields with the same datas, but their names are prefixed. This prefix thing have to be done by our customer. Is Hibernate able to automatically change the tables and fields name by adding them a prefix like a_ or u_, and this prefix to be set in a configuration file?

We have to decide this days if we'll use Hibernate or not and we have to find a solution for this problem. Please help me!

Thank you!


Top
 Profile  
 
 Post subject: Re: Prefix all fields and tables from a database
PostPosted: Mon Feb 11, 2008 6:50 pm 
Senior
Senior

Joined: Fri Jun 01, 2007 12:41 pm
Posts: 121
Here is extract from Hibernate Book about naming strategies. I hope this will help you resolve you issue.


4.3.6 Implementing naming conventions


We often encounter organizations with strict conventions for database table and column names. Hibernate provides a feature that allows you to enforce naming standards automatically.

Suppose that all table names in CaveatEmptor should follow the pattern
CE_<table name>. One solution is to manually specify a table attribute on all <class> and collection elements in the mapping files. However, this approach is time-consuming and easily forgotten. Instead, you can implement Hibernate’s NamingStrategy interface, as in listing 4.1.

Code:
public class CENamingStrategy extends ImprovedNamingStrategy {
   public String classToTableName(String className) {
      return StringHelper.unqualify(className);
   }
   public String propertyToColumnName(String propertyName) {
      return propertyName;
   }
   public String tableName(String tableName) {
      return "CE_" + tableName;
   }
   public String columnName(String columnName) {
      return columnName;
   }
   public String propertyToTableName(String className, String propertyName) {
      return "CE_" + classToTableName(className) + '_'
         + propertyToColumnName(propertyName);
   }
}


You extend the ImprovedNamingStrategy, which provides default implementations for all methods of NamingStrategy you don’t want to implement fromscratch (look at the API documentation and source). The classToTableName() method is called only if a <class> mapping doesn’t specify an explicit table name. The propertyToColumnName() method is called if a property has no explicit column name. The tableName() and columnName() methods are called when an explicit name is declared. If you enable this CENamingStrategy, the class mapping declaration
<class name="BankAccount"> results in CE_BANKACCOUNT as the name of the table. However, if a table name is specified, like this, <class name="BankAccount" table="BANK_ACCOUNT"> then CE_BANK_ACCOUNT is the name of the table. In this case, BANK_ACCOUNT is passed to the tableName() method.

So in your case read the prefix from your application properties and supply it to CENamingStrategy class.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Feb 14, 2008 4:38 am 
Newbie

Joined: Mon Feb 11, 2008 4:29 pm
Posts: 6
Thank you very much for your response.

We decided to use Hibernate for our project, so it's time for study now :).

Regarding the subject of my first post, one more question: haw can I enable CENamingStrategy?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Feb 14, 2008 5:10 am 
Newbie

Joined: Mon Feb 11, 2008 4:29 pm
Posts: 6
I found the answer:

Configuration cfg = new Configuration();
cfg.setNamingStrategy( new CENamingStrategy() );
SessionFactory sessionFactory sf = cfg.configure().buildSessionFactory();


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.