Announcement Announcement Module
Collapse
No announcement yet.
Hibernate 4 Spring 3 -> Hibernate Naming Strategy ignored Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Hibernate 4 Spring 3 -> Hibernate Naming Strategy ignored

    I'm trying to get a NamingStrategy working, because with have to Prefix our tables with the project name, for Example: TEST_TableName

    Now I've tried to configure this(Not really Documented), but it is being ignored so far:

    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
    	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx"
    	xsi:schemaLocation="http://www.springframework.org/schema/beans
    		http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    		http://www.springframework.org/schema/tx 
    		http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
    	<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
    		destroy-method="close">
    		<property name="driverClass">
    			<value>com.ibm.db2.jcc.DB2Driver</value>
    		</property>
    		<property name="jdbcUrl">
    			<value>jdbc:db2://localhost:50000/TEST
    			</value>
    		</property>
    		<property name="user">
    			<value>***</value>
    		</property>
    		<property name="password">
    			<value>***</value>
    		</property>
    	</bean>
    
    	<bean id="sessionFactory"
    		class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
    		<property name="namingStrategy" ref="MyNamingStrategy" />
    		<property name="dataSource" ref="dataSource" />
    		<property name="packagesToScan">
    			<list>
    				<value>ch.test.MyWebShop.domain.*
    				</value>
    			</list>
    		</property>
    		<property name="hibernateProperties">
    			<value>
    				hibernate.c3p0.min_size=5
    				hibernate.c3p0.max_size=20
    				hibernate.c3p0.timeout=1800
    				hibernate.c3p0.max_statements=50
    				hibernate.dialect=org.hibernate.dialect.DB2Dialect
    				hibernate.show_sql=true
    				hibernate.format_sql=true
    				hibernate.hbm2ddl.auto=create
    				hibernate.id.new_generator_mappings=true
    	      </value>
    		</property>
    	</bean>
    
    	<bean id="transactionManager"
    		class="org.springframework.orm.hibernate4.HibernateTransactionManager">
    		<property name="sessionFactory" ref="sessionFactory" />
    	</bean>
    
    	<bean id="MyNamingStrategy" class="ch.test.MyWebShop.hibernate.MyNamingStrategy" />
    
    	<tx:annotation-driven transaction-manager="transactionManager" />
    </beans>

    Tried to if something happens:

    Code:
    package ch.test.MyWebShop.hibernate;
    
    import org.hibernate.cfg.ImprovedNamingStrategy;
    
    @SuppressWarnings("serial")
    public class MyNamingStrategy extends ImprovedNamingStrategy {
    
    	@Override
    	public String collectionTableName(String ownerEntity, String ownerEntityTable, String associatedEntity, String associatedEntityTable, String propertyName) {
    		// TODO Auto-generated method stub
    		return "FRT_" + super.collectionTableName(ownerEntity, ownerEntityTable, associatedEntity, associatedEntityTable, propertyName);
    	}
    
    	@Override
    	public String columnName(String columnName) {
    		// TODO Auto-generated method stub
    		return "FRT_" + super.columnName(columnName);
    	}
    
    	@Override
    	public String foreignKeyColumnName(String propertyName, String propertyEntityName, String propertyTableName, String referencedColumnName) {
    		// TODO Auto-generated method stub
    		return"FRT_" +  super.foreignKeyColumnName(propertyName, propertyEntityName, propertyTableName, referencedColumnName);
    	}
    
    	@Override
    	public String joinKeyColumnName(String joinedColumn, String joinedTable) {
    		// TODO Auto-generated method stub
    		return"FRT_" +  super.joinKeyColumnName(joinedColumn, joinedTable);
    	}
    
    	@Override
    	public String logicalCollectionColumnName(String columnName, String propertyName, String referencedColumn) {
    		// TODO Auto-generated method stub
    		return"FRT_" +  super.logicalCollectionColumnName(columnName, propertyName, referencedColumn);
    	}
    
    	@Override
    	public String logicalCollectionTableName(String tableName, String ownerEntityTable, String associatedEntityTable, String propertyName) {
    		// TODO Auto-generated method stub
    		return "FRT_" + super.logicalCollectionTableName(tableName, ownerEntityTable, associatedEntityTable, propertyName);
    	}
    
    	@Override
    	public String logicalColumnName(String columnName, String propertyName) {
    		// TODO Auto-generated method stub
    		return"FRT_" +  super.logicalColumnName(columnName, propertyName);
    	}
    
    	@Override
    	public String propertyToColumnName(String propertyName) {
    		// TODO Auto-generated method stub
    		return"FRT_" +  super.propertyToColumnName(propertyName);
    	}
    
    	@Override
    	public String tableName(String tableName) {
    		// TODO Auto-generated method stub
    		return "FRT_" + super.tableName(tableName);
    	}
    
    	@Override
    	public String classToTableName(String arg0) {
    		return "FRT_" + super.classToTableName(arg0);
    	}
    }
    Nothing happend, nowhere has FRT shown up, not on the Database nor in the logging Output. Bean MyNamingStrategy is being initialized and Property namingStrategy is recognized.

  • #2
    I have the same problem when using Spring 3.1 and Hibernate 3.5. I defined UnderscoreNamingStrategy but it is not used at all. Did you ever solve the problem?

    Thanks in advance for your answer.

    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xmlns:context="http://www.springframework.org/schema/context"
           xmlns:tx="http://www.springframework.org/schema/tx"
           xmlns:util="http://www.springframework.org/schema/util"
           xsi:schemaLocation="
           http://www.springframework.org/schema/beans
           http://www.springframework.org/schem...-beans-3.1.xsd
           http://www.springframework.org/schema/context
           http://www.springframework.org/schem...ontext-3.1.xsd
           http://www.springframework.org/schema/tx
           http://www.springframework.org/schem...ing-tx-3.1.xsd
           http://www.springframework.org/schema/util
           http://www.springframework.org/schema/util/spring-util-3.1.xsd">
    
        <!-- Use unitils configured database -->
        <bean id="dataSource" class="org.unitils.database.UnitilsDataSourceFactoryBean"/>
    
        <bean id="entityManagerFactory"
              class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    
            <property name="dataSource" ref="dataSource"/>
    
            <property name="jpaVendorAdapter">
                <bean class="org.unitils.orm.jpa.util.provider.hibernate.UnitilsHibernateJpaVendorAdapter">
                    <property name="showSql" value="true"/>
                    <property name="generateDdl" value="false"/>
                </bean>
            </property>
    
            <property name="jpaProperties" ref="jpaPropertiesHibernate"/>
    
            <property name="packagesToScan" ref="packagesToScanHibernate"/>
        </bean>
    
        <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
            <property name="entityManagerFactory" ref="entityManagerFactory"/>
            <property name="jpaDialect" ref="jpaDialectHibernate"/>
        </bean>
    
        <tx:annotation-driven transaction-manager="transactionManager"/>
    
        <bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor"/>
    
        <!-- Hibernate specific configuration -->
        <bean id="jpaDialectHibernate" class="org.springframework.orm.jpa.vendor.HibernateJpaDialect"/>
    
        <bean id="jpaVendorAdapterHibernate"
              class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
            <property name="databasePlatform" value="org.hibernate.dialect.Oracle11gDialec"/>
            <property name="generateDdl" value="false"/>
            <property name="showSql" value="true"/>
        </bean>
    
        <util:list id="packagesToScanHibernate">
                       ......
        </util:list>
    
        <bean id="hibernateNamingStrategy" class="...UnderscoreNamingStrategy"/>
    
        <util:map id="jpaPropertiesHibernate">
            <entry key="hibernate.ejb.naming_strategy" value-ref="hibernateNamingStrategy"/>
            <entry key="hibernate.dialect" value="....Oracle11gDialect"/>
            <entry key="hibernate.cache.use_second_level_cache" value="false"/>
            <entry key="hibernate.cache.use_query_cache" value="false"/>
            <entry key="hibernate.jdbc.fetch_size" value="512"/>
            <entry key="hibernate.jdbc.batch_size" value="512"/>
            <entry key="hibernate.bytecode.provider" value="javassist"/>
    
            <!-- <entry key="hibernate.hbm2ddl.auto" value="create"/> -->
        </util:map>
        <!-- end of Hibernate specific properties -->
    
        <bean id="hibernateSessionFactory" class="org.springframework.orm.jpa.vendor.HibernateJpaSessionFactoryBean">
            <property name="entityManagerFactory" ref="entityManagerFactory"/>
        </bean>
    </beans>
    Last edited by ramon.turnes; Sep 7th, 2012, 11:35 AM. Reason: Hibernate version 3.5 instead of version 4.

    Comment

    Working...
    X