首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >postgresql的Hibernate 5命名策略

postgresql的Hibernate 5命名策略
EN

Stack Overflow用户
提问于 2017-05-23 22:26:52
回答 1查看 774关注 0票数 1

我知道这方面的线程很少,但我不明白为什么它不适用于我的代码。我有一个包含大写字母、列、表名、sequences.But的整个数据库,当我试图通过sqlcriteria进行查询时,它会转换所有的小写值。我找到了一个解决办法,但我不想写这样的查询:

代码语言:javascript
运行
复制
select a."COLUMN_1", a."COLUMN_2" from schema."A" a 

和映射,如:

代码语言:javascript
运行
复制
@Entity(name = "`A`")
public class A implements Serializable {
     @Column(name = "`COLUMN_1`")
     private Integer column1;
     @Column(name = "`COLUMN_2`")
     private Integer column2;

}

我试图在堆栈溢出中跟踪一些线程,实现我自己的命名策略,但两者都不起作用。

代码语言:javascript
运行
复制
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里这样称呼它:

代码语言:javascript
运行
复制
<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有任何方法来做我想做的?还是我会被迫保留这些?

提前感谢

EN

回答 1

Stack Overflow用户

发布于 2017-05-24 05:38:08

我认为您必须在您的示例中添加注释@Table(name = "A"),以便将实体A映射到数据库表A,下面是我如何使用它,希望它能有所帮助:

代码语言:javascript
运行
复制
@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;
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/44146039

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档