我知道这方面的线程很少,但我不明白为什么它不适用于我的代码。我有一个包含大写字母、列、表名、sequences.But的整个数据库,当我试图通过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;
}
我试图在堆栈溢出中跟踪一些线程,实现我自己的命名策略,但两者都不起作用。
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方法的中设置一个断点。当我尝试一个单元测试,但它并没有阻止there.Is有任何方法来做我想做的?还是我会被迫保留这些?
提前感谢
发布于 2017-05-24 05:38:08
我认为您必须在您的示例中添加注释@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;
}
https://stackoverflow.com/questions/44146039
复制相似问题