我有一个以前为 JPA 构建的域模型,如下所示:
@Entity
@Table(name="TABLE")
public class MyClassImpl implements MyClass, Serializable {
@Embeddable
public static class ID {
@Column( name = "COL_1", nullable = false)
protected int id1;
@Column( name = "COL_2", nullable = false)
protected int id2;
@Column( name = "COL_3", nullable = false, unique = true)
protected String id3;
...
}
@EmbeddedId
protected ID id;
@Column(name = "COL_4")
protected String otherData;
...
}
现在我正在迁移到 MyBatis,我对映射类似的东西有疑问。
我知道我可以制作一个像这样的 resultMap:
<resultMap id="myClassResultMap" type="MyClass">
<association property="id" javaType="MyClass.ID">
<id property="id1" column="COL_1" />
<id property="id2" column="COL_2" />
<id property="id3" column="COL_3" />
</association>
<result property="otherData" column="COL_4" />
...
</resultMap>
中的<id ... />
字段association
会用作类的 id 吗?如果我想将其标记id
为final
并将其传递给构造函数怎么办?
我也将该 ID 用于其他类:
@Entity
@Table(name = "OTHERTABLE")
public class MyOtherClassImpl implements MyOtherClass, Serializable {
@Embeddable
public static class ID {
@Embedded
MyClass.ID ref;
@Column(name = "ID", nullable = false, unique = true)
protected int id;
//methods
}
@EmbeddedId
protected ID id;
@Column(name = "COL_5")
protected String otherData;
...
}
因为这两个表共享 3 个主键。
有可能保持这样的结构吗?