2010-03-19 33 views

回答

3

您需要實現自己的NamingStrategy(org.hibernate.cfg.NamingStrategy)。也許子類化你當前使用的是最簡單的方法(默認情況下,hibernate使用的是DefaultNamingStrategy)。

然後用你的命名策略配置SessionFactory:

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

或通過在會話工廠春天依賴注入,如果你正在使用它:

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

還,如果你有一個特定的您要使用的非編程命名策略只能更改hbm.xml文件的外鍵映射。這是所有相關設置的屬性等。

1

您無法用批註或命名策略覆蓋任何內容。例如,連接表的FK不能使用命名策略來指定,並且使用註釋會很痛苦。

如果你願意來配置一切以同樣的方式,你可以將你模式導出到數據庫

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(); 
... 
之前修改註釋配置