开发者

How can we change method for generating PK and FK names in hibernate?

Can i provide some algorithm for generate smth intead of FK030H开发者_如何学GoF9840303 for example?


You need to implement your own NamingStrategy (org.hibernate.cfg.NamingStrategy). Maybe subclassing the one you are currently using is the easiest way (by default hibernate is using the DefaultNamingStrategy).

Then configure the sessionFactory with your naming strategy:

SessionFactory sf = new Configuration()
    .setNamingStrategy(new YourNamingStrategy())
    .addFile(...)
    .buildSessionFactory();

Or via spring dependency injection on the session factory, if you are using it :

<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
...
  <property name="namingStrategy">
    <ref bean="namingStrategy" />
  </property>
...


You can't override anything with annotation or naming strategy. FK of joined tables for example can't be specified using naming strategy and are a pain using annotation.

If you are willing to configure everything the same way, you could modify the annotation config before you export the schema to the database

AnnotationConfiguration configuration = ...;
Iterator<Table> tables = configuration.getTableMappings();
...
Iterator<ForeignKey> keys = table.getForeignKeyIterator();
while ( keys.hasNext() ) {
ForeignKey key = keys.next();
key.setName( ... );
}
...

SchemaExport schemaExport = new SchemaExport( configuration );
schemaExport.setHaltOnError( true );
schemaExport.execute( false, true, false, true );
List exceptions = schemaExport.getExceptions();
...


also, if you have a specific NON PROGRAMMATIC naming strategy that you want to use you can just change the foreign-key mapping of the hbm.xml file. It's an attribute of all the relevant sets, etc.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜