插入时的iBatis鉴别器 [英] iBatis Discriminator on Insert

查看:63
本文介绍了插入时的iBatis鉴别器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个抽象类Example和一些具体的子类.我使用了一个鉴别器将数据拉出到数据库中,如下所示:

I have an abstract class Example and concrete subclasses to go along with it. I used a discriminator to pull data out of the database, like so:

<resultMap id="ExampleResultMap" class="Example">
    <discriminator column="stateCode" javaType="java.lang.String">
        <subMap value="AL" resultMap="AlabamaStateResultMap"/>
        <subMap value="AR" resultMap="ArkansasStateResultMap"/>
        [...]
    </discriminator>
</resultMap>

<resultMap extends="ExampleResultMap" 
           id="AlabamaStateResultMap"
           class="AlabamaState"/>
<resultMap extends="ExampleResultMap" 
           id="ArkansasStateResultMap"
           class="ArkansasState"/>
[...]

因此,我有一个AlabamaState对象(抽象Example对象的子类),上面没有任何属性.这是人为的,但要点是,我没有任何可唯一标识对象类型的属性-如果没有这种情况,我没有理由.

Thus I have an AlabamaState object (a subclass of the abstract Example object) with no attributes of any kind on him. This is contrived, but the gist is that I don't have any attribute that uniquely identifies the object's type--and there's no reason I would if not for this case.

(注意:这些类不是空的,它们是行为的,因此无法将其重构为不存在.)

(Note: The classes aren't empty, they're behavioral, so refactoring them out of existence isn't an option.)

如何将其保存回数据库?

理想情况下,ParameterMap会有一个Discriminator,但似乎没有一个.

Ideally there would be a Discriminator for ParameterMaps, but there doesn't seem to be one.

据我所知,有许多不良解决方案,其中:

As far as I can tell, there are a number of undesirable solutions, among them:

  • 放弃并在我的所有子类上添加"getType()"方法,该方法返回静态字符串.在这种情况下,AL. (请注意,我非常努力地避免在我的所有代码中都需要这样做,所以有= OOD失败).
  • 制作一个与我的大型复杂对象完全相同的"DB"对象,但恰好还有一个额外的字符串,说哦,顺便说一句,我的TYPE是AL."
  • 在插入对象之前,将要保留的所有20个属性提取到HashMap中.
  • 还有一些疯狂的事情,例如使用toString()或其他可以帮助我的事情.
  • Give up and add a "getType()" method on all my subclasses that returns a static string. In this case, AL. (Note that I tried pretty hard to avoid needing this throughout all my code, so having this = OOD-defeat).
  • Make a "DB" object that's exactly like my big, complex object but happens to also have an extra string saying "Oh, btw, my TYPE is AL."
  • Extract all 20 attributes I want to persist into a HashMap before inserting the object.
  • Some other craziness like using the toString() or something to help me out.

很可能我会选择第一个选项,但这似乎很荒谬,不是吗?如果iBatis可以创建它,那么它不应该能够持久吗?我真正需要的是用于插入的鉴别器.

Likely I'll go with the first option, but it seems rather ridiculous, doesn't it? If iBatis can create it, shouldn't it be able to persist it? What I really need is a discriminator for insert.

我是不是很幸运,还是我只是忽略了一些明显的东西?

Am I out of luck, or am I just overlooking something obvious?

推荐答案

如果您没有属于子类的属性,则应考虑删除这些子类,并为先前的基类添加一个枚举,因为子类的唯一目的是服务是区分对象的类型(如果我理解正确的话).为此,使用枚举更容易扩展,并且在客户端代码中更优雅(因为您可以打开枚举而不是使用instanceof表达式块).

If you have no attributes belonging to your subclasses, you should consider removing these subclasses and add an enum to your former base-class, since the only purpose your subclasses serve is to differentiate the type of your objects (if I understood you correctly). Using an enum for this is easier to extend and more elegant in client code (since you can switch on the enum instead of using blocks of instanceof expressions).

如果在子类上具有某些操作的特殊实现,则也可以将它们移至枚举,并让基类委托给枚举上的实现.

If are having special implementations of certain operations on your subclasses, you could move them to the enum as well, and have your base class delegate to the implementation on the enum.

编辑

这里是一个例子:

public interface GreetingStrategy {
    abstract String sayHello();
}

enum UserType implements GreetingStrategy {
    ADMIN {
        @Override
        public String sayHello() {
            return "hello from admin";
        }
    },

    GUEST {
        @Override
        public String sayHello() {
            return "hello from guest";
        }
    };

}

class User {

    private final GreetingStrategy greetingStrategy;

    public User(GreetingStrategy greetingStrategy) {
        this.greetingStrategy = greetingStrategy;
    }

    public String sayHello() {
        return greetingStrategy.sayHello();
    }

}

这篇关于插入时的iBatis鉴别器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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