0

有没有办法标记某些属性根本不检索?

我的用户表中有一个唯一的胡椒行,如果检索到用户,我不想加载这个胡椒和散列密码。

我在远程客户端上没有任何需要,所以我想确保它不会被意外传递给它。

如果我需要在应用程序服务器上重新生成或更改密码,我可以通过命名查询检索辣椒。

4

2 回答 2

1

您可能会实现一个拦截器并覆盖该onLoad方法:有关 Hibernate Interceptors Here 的更多信息

我已使用此策略覆盖 onFlushDirty 和 onSave 方法来更新 Timestamp 字段。期间我从未使用过它onLoad

这是一个基于我使用过的类似拦截器的示例拦截器(未测试):

public final class PepperInterceptor extends EmptyInterceptor {

    /** The Value to set as the Pepper field when loading from the DB. */
    private static final String DEFAULT_PEPPER = "[PROTECTED-PEPPER]";

    @Override
    public boolean onLoad(final Object entity, final Serializable id, final Object[] currentState,
            final Object[] previousState, final String[] propertyNames, final Type[] types) {


        if (entity instanceof User) {
            for (int i = 0; i < propertyNames.length; i++) {
                if (propertyNames[i].equalsIgnoreCase("pepper")) {
                    currentState[i] = DEFAULT_PEPPER;
                }
            }

            return true;
        }
        return super.onLoad(entity, id, currentState, previousState, propertyNames, types);
    } 

}


编辑:

进一步考虑这一点,您必须确保该值永远不会被 覆盖DEFAULT_PEPPER,可能使用类似于updatable=false实体类的列定义中的内容

于 2012-07-16T01:08:16.427 回答
0

Hibernate 允许根据需要覆盖生成的 sql。

http://docs.redhat.com/docs/en-US/JBoss_Enterprise_Web_Platform/5/html/Hibernate_Annotations_Reference_Guide/entity-hibspec-customsql.html

@Loader 注释可能有用。

于 2012-07-15T23:18:56.643 回答