修剪JPA中的字符串字段 [英] Trim string field in JPA

查看:195
本文介绍了修剪JPA中的字符串字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个db表,其数据类型为char(20)。我不允许将其更改为varchar。

I have a db table with column of datatype char(20). I'm not allowed to change it to a varchar.

我正在编写映射到此表的JPA实体。我希望在我的实体类中表示此列的字符串字段始终包含修剪后的值,而不是用db中存在的空格填充的20个字符值。

I'm writing a JPA entity mapped to this table. I would like the string field representing this column in my entity class to always contain the trimmed value, not the 20-character value padded with spaces that exists in the db.

我看不到任何简单的方法来做到这一点。 (注释会摇滚!)。目前我只是从我的getter()中返回一个修剪过的值,但这感觉就像一个kludge。

I can't see any easy way to do this. (an annotation would rock!). At the moment I'm just returning a trimmed value from my getter(), but this feels like a kludge.

谷歌搜索没有提供任何帮助。有什么想法?

A google search is offering no help on this. Any ideas?

推荐答案

或者你可以使用生命周期注释:

Or you can use lifecycle annotations:

@Entity
public class MyEntity {

    @PostLoad
    protected void repair(){
        if(myStringProperty!=null)myStringProperty=myStringProperty.trim();
    }

    private String myStringProperty;
    public String getMyStringProperty() {
        return myStringProperty;
    }
    public void setMyStringProperty(String myStringProperty) {
        this.myStringProperty = myStringProperty;
    }

}

如果这发生在多个实体上你可以创建自定义注释并编写专用的EntityListener。

If this occurs on multiple entities you can create a custom annotation and write a dedicated EntityListener.

注释

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
public @interface Trim {}

监听器

public class TrimListener {

    private final Map<Class<?>, Set<Field>> trimProperties = 
        new HashMap<Class<?>, Set<Field>>();

    @PostLoad
    public void repairAfterLoad(final Object entity) throws Exception {
        for (final Field fieldToTrim : getTrimProperties(entity.getClass())) {
            final String propertyValue = (String) fieldToTrim.get(entity);
            if (propertyValue != null)
                fieldToTrim.set(entity, propertyValue.trim());
        }
    }

    private Set<Field> getTrimProperties(Class<?> entityClass) throws Exception {
        if (Object.class.equals(entityClass))
            return Collections.emptySet();
        Set<Field> propertiesToTrim = trimProperties.get(entityClass);
        if (propertiesToTrim == null) {
            propertiesToTrim = new HashSet<Field>();
            for (final Field field : entityClass.getDeclaredFields()) {
                if (field.getType().equals(String.class)
                    && field.getAnnotation(Trim.class) != null) {
                    field.setAccessible(true);
                    propertiesToTrim.add(field);
                }
            }
            trimProperties.put(entityClass, propertiesToTrim);
        }
        return propertiesToTrim;
    }

}

现在用所有相关的String字段注释 @Trim 并将监听器注册为persistence.xml中的默认实体监听器:

Now annotate all relevant String fields with @Trim and register the Listener as default entity listener in your persistence.xml:

<persistence-unit ..>
    <!-- ... -->
    <default-entity-listeners>
      com.somepackage.TrimListener
      and.maybe.SomeOtherListener
    </default-entity-listeners>
</persistence-unit>

 

这篇关于修剪JPA中的字符串字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆