我知道关于这一点的线索很少,但我无法理解为什么它不能使用我的代码。我有整个数据库,包括大写字母,列,表名,序列。但是当我尝试通过sql
或criteria
进行查询时,它会以小写形式转换所有值。我找到了一种解决方法,但我不想写下这样的查询:
select a."COLUMN_1", a."COLUMN_2" from schema."A" a
和映射一样:
@Entity(name = "`A`")
public class A implements Serializable {
@Column(name = "`COLUMN_1`")
private Integer column1;
@Column(name = "`COLUMN_2`")
private Integer column2;
}
我尝试跟踪stackoverflow中的一些线程,实现我自己的命名策略,但它都没有用。
public class ModifiedImprovedNamingStrategy extends PhysicalNamingStrategyStandardImpl{
@Override
public final Identifier toPhysicalColumnName(final Identifier name, final JdbcEnvironment context) {
return new Identifier(addUnderscores(name.getText()), name.isQuoted());
}
/**
* Adds the underscores.
*
* @param name
* the name
* @return the string
*/
protected static String addUnderscores(final String name) {
final StringBuilder buf = new StringBuilder(name.replace('.', '_'));
for (int i = 1; i < (buf.length() - 1); i++) {
if (Character.isLowerCase(buf.charAt(i - 1))
&& Character.isUpperCase(buf.charAt(i))
&& Character.isLowerCase(buf.charAt(i + 1))) {
buf.insert(i++, '_');
}
}
return "`" + buf.toString().toUpperCase(Locale.ROOT) + "`";
}
}
然后在我的applicationContext中调用它:
<bean id="sessionFactory"
class="org.springframework.orm.hibernate5.LocalSessionFactoryBean"
p:dataSource-ref="dataSource">
<property name="packagesToScan" value="com.services.vo"/>
<property name="mappingLocations">
<list>
<value>classpath*:hibernate/queries/**.hbm.xml</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.transaction.coordinator_class">org.hibernate.transaction.JDBCTransactionFactory</prop>
<prop key="hibernate.physical_naming_strategy">com.services.util.hibernate.ModifiedImprovedNamingStrategy</prop>
<prop key="hibernate.format_sql">true</prop>
</props>
</property>
</bean>
我的目的是避免在任何地方写这些。我试图在覆盖的ModifiedImprovedNamingStrategy 方法中设置断点。当我尝试单元测试时,但它并没有停在那里。有什么办法可以做我想要的吗?或者我会被迫保留那些?
提前致谢
答案 0 :(得分:0)
我相信你必须在你的案例中添加注释@Table(name = "A")
来将你的实体A
映射到数据库表A
,这是我如何使用它,希望它有帮助:
@Entity
@Table(name = "house")
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
@Document(indexName = "house")
public class House implements Serializable {
@NotNull
@Column(name = "location", nullable = false)
private String location;
@Size(max = 200)
@Column(name = "description", length = 200)
private String description;
}