这里我附上了我尝试的bean定义和我遵循的命名策略。但是jpa没有意识到这一点。我哪里做错了?
Xml配置文件:
<bean id="EntityManagerFactory"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="jpaDialect">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaDialect" />
</property>
<property name="persistenceUnitName" value="PU" />
<property name="jpaVendorAdapter" ref="JpaVendorAdapter" />
<property name="packagesToScan" value="com.<package-name>.entity" />
<property name="jtaDataSource" ref="DataSource" />
<property name="jpaPropertyMap">
<props>
<prop key="hibernate.current_session_context_class">jta</prop>
<prop key="javax.persistence.transactionType">jta</prop>
<prop key="hibernate.transaction.jta.platform">com.atomikos.icatch.jta.hibernate4.AtomikosPlatform</prop>
<prop key="hibernate.search.autoregister_listeners">false</prop>
<prop key="spring.jpa.hibernate.naming_strategy">com.hrc.commons.CustomNamingStrategy</prop>
<prop key="jpa.hibernate.naming_strategy">com.hrc.commons.CustomNamingStrategy</prop>
<prop key="hibernate.ejb.naming_strategy">com.hrc.commons.CustomNamingStrategy</prop>
<prop key="hibernate.naming_strategy">com.hrc.commons.CustomNamingStrategy</prop>
</props>
</property>
<property name="jpaProperties">
<props>
<prop key="hibernate.naming_strategy">com.hrc.commons.CustomNamingStrategy</prop>
<prop key="jpa.hibernate.naming_strategy">com.hrc.commons.CustomNamingStrategy</prop>
<prop key="hibernate.ejb.naming_strategy">com.hrc.commons.CustomNamingStrategy</prop>
<prop key="spring.jpa.hibernate.naming_strategy">com.hrc.commons.CustomNamingStrategy</prop>
</props>
</property>
</bean>
命名策略Java代码文件:
package com.hrc.commons;
import org.hibernate.cfg.ImprovedNamingStrategy;
public class CustomNamingStrategy extends ImprovedNamingStrategy {
private static final long serialVersionUID = 1L;
private static final String PREFIX = "prefix_";
@Override
public String classToTableName(final String className) {
return this.addPrefix(super.classToTableName(className));
}
@Override
public String tableName(final String tableName) {
return this.addPrefix(super.tableName(tableName));
}
@Override
public String collectionTableName(final String ownerEntity, final String ownerEntityTable, final String associatedEntity, final String associatedEntityTable, final String propertyName) {
return this.addPrefix(super.collectionTableName(ownerEntity, ownerEntityTable, associatedEntity, associatedEntityTable, propertyName));
}
@Override
public String logicalCollectionTableName(final String tableName, final String ownerEntityTable, final String associatedEntityTable, final String propertyName) {
return this.addPrefix(super.logicalCollectionTableName(tableName, ownerEntityTable, associatedEntityTable, propertyName));
}
private String addPrefix(final String tableName) {
return PREFIX + tableName;
}
}
我也尝试了各个属性以及个人jpaPropertiesMap
和jpaProperties
。但仍然没有运气。请任何人帮忙。
我需要将prefix_
添加到我在该特定packagesToScan
文件夹中找到的每个实体。所以对于ex:如果有任何实体文件定义为
@Entity @Table(name="student")
如果前缀是"iiit"
,我需要iiit_student
作为使用此命名策略的表名。我没有得到任何关于此的错误。但是仅在架构错误中找不到表。