hibernate命名策略不适用于spring数据

时间:2016-03-02 21:05:09

标签: hibernate jpa spring-data spring-data-jpa naming

我要添加以下类:

package my.package;

@Entity
public class ExceptionalCalendarDay {
  ...
}

*数据库中的各个表名:tExceptionalCalendarDay

并遵循entityManagerFactory配置:

<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
        <property name="dataSource" ref="dataSource"/>
        <property name="packagesToScan" value="my.package"/>
        <property name="jpaVendorAdapter">
            <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"/>
        </property>
        <property name="persistenceUnitName" value="myUnitName"/>
        <property name="jpaProperties">
            <props>
                <prop key="hibernate.dialect">org.hibernate.dialect.SQLServer2012Dialect</prop>
                <prop key="hibernate.default_schema">dbo</prop>
                <prop key="hibernate.ejb.naming_strategy">my.util.NamingStrategy</prop>

                <prop key="hibernate.show_sql">${db.show_sql}</prop>
                <prop key="hibernate.format_sql">${db.format_sql}</prop>
                <prop key="hibernate.jdbc.use_get_generated_keys">${db.use_generated_keys}</prop>
                <prop key="hibernate.temp.use_jdbc_metadata_defaults">${db.temp.use_jdbc_metadata_defaults}</prop>
                <prop key="current_session_context_class">thread</prop>

                <!-- jdbc connection properties -->
                <prop key="hibernate.connection.isolation">${db.isolation}</prop>
            </props>
        </property>
    </bean>

命名策略:

public class NamingStrategy extends DefaultNamingStrategy {

    @Override
    public String classToTableName(String qualifiedName) {
        String tableName = super.classToTableName(qualifiedName);
        return "t" + tableName;
    }

    @Override
    public String propertyToColumnName(String propertyName) {
        return WordUtils.capitalize(super.propertyToColumnName(propertyName));
    }
}

我有以下资料库:

public interface BusinessDayRepository extends JpaRepository<ExceptionalCalendarDay, Date> {
    List<ExceptionalCalendarDay> findAll();
}

如果将ExceptionalCalendarday作为数据库表调用,一切正常,但如果我将其重命名为tExceptionalCalendarday - 这不起作用:

Caused by: com.microsoft.sqlserver.jdbc.SQLServerException: Invalid object name 'dbo.ExceptionalCalendarDay'.

奇怪。

是什么原因?

P.S。

如果将名称添加到Entity注释

@Entity(name = "tExceptionalCalendarDay")

一切正常

0 个答案:

没有答案