我的表格看起来像这样。
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
?我找不到任何好的例子。