1

我的桌子看起来像这样。

A               PROPERTY                B
--------        --------                --------
ID (PK)  <-|    ID (PK)             |-> ID (PK)
           |    DISCRIMINATOR       |
           |    ...                 |
           ---- A_ID (FK, Null)     |
                B_ID (FK, Null) ----|

这是我的 POJO。

public abstract Property<T> {
    private T owner;
}

public class AProperty extends Property<A> {
    // super.owner is an instance of A
}

public class BProperty extends Property<B> {
    // super.owner is an instance of B
}

我发现我可以@TypeDiscriminator像这样选择。

public interface PropertyMapper<T extends Property> {

    @Select("SELECT * FROM PROPERTY WHERE ID = #{id}")
    @TypeDiscriminator(
        column = "DISCRIMINATOR",
        cases = {@Case(results = {@Result(column = "A_ID", property = "owner",
                                          one = @One(select = "AMapper.select"))},
                       type = AProperty.class, value = "A"),
                 @Case(results = {@Result(column = "B_ID", property = "owner",
                                          one = @One(select = "BMapper.select"))},
                       type = BProperty.class, value = "B")
        }
    )
    T select(long id);

    /**
     * HOW CAN I DO THIS?
     */
    int insert(T entity);
}

如何@TypeDiscriminator在插入中使用?我找不到任何好的例子。

4

0 回答 0