本文介绍了从JPA到MyBatis:组合键?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我以前为JPA构建了一个域模型,如下所示:

I have a domain model previously structured as for JPA like follows:

@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,并且对映射这样的东西有疑问.

Now I'm migrating to MyBatis and I'm having doubts about mapping something like that.

我知道我可以制作一个resultMap,例如:

I know I can make a resultMap like:

<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>

是否将association中的<id ... />字段用作类的ID?如果我想将id标记为final并将其传递给构造函数怎么办?

Will the <id ... /> fields in association be used as id of the class? What if I want to mark the id as final and pass it to the constructor?

我也将该ID用于其他课程:

I use that ID for other classes as well:

@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个主键.

Because the two tables share 3 primary keys.

是否可以保留这种结构?

Is it possible to keep such structure?

推荐答案

我发现了两种解决方法:

I Found two ways of solving this:

第一:进入构造函数(实际上我正在使用它)

1st: Into constructor (actually I'm using this)

public class MyClassImpl implements MyClassWritable, Serializable {

   public class IdImpl implements MyClass.Id, Comparable<IdImpl> {
       private final int id1;
       private final int id2;
       private final String id3;

       private IdImpl(int id1, int id2, String id3) {
          this.id1 = id1;
          this.id2 = id2;
          this.id3 = id3;
       }

       // getters, compareTo, hashCode, equals and toString.
       // implementing Comparable to use MyClass.Id as indexes into Collections
   }

   private final Id id;
   ...
   private String other;

   public MyClassImpl(int id1, int id2, String id3) {
      this.id = new IdImpl(id1, id2, id3);
   }

   // methods
}

结果映射为:

<resultMap id="myClassResultMap" type="MyClass">
   <constructor>
      <idArg column="ID_COL_1" javaType="java.lang.Integer"/>
      <idArg column="ID_COL_2" javaType="java.lang.Integer"/>
      <idArg column="ID_COL_3" javaType="java.lang.String"/>
   </contructor>
   <result property="other" column="OTHER_COL" />
</resultMap>

第二个::非最终字段和ID的二传手.结果图将是:

2nd: non-final fields and setters into the Id class. The resultMap would be:

public class MyClassImpl implements MyClassWritable, Serializable {

   public class IdImpl implements MyClass.IdWritable, Comparable<IdImpl> {
       private int id1;
       private int id2;
       private String id3;

       private IdImpl() {
          super();
       }

       // SETTERS, getters, compareTo, hashCode, equals and toString.
       // implementing Comparable to use MyClass.Id as indexes into Collections
   }

   private final Id id;
   ...
   private String other;

   public MyClassImpl(int id1, int id2, String id3) {
      this.id = new IdImpl();
   }

   // methods
}

<resultMap id="myClassResultMap" type="MyClass">
   <id property="id.id1" column="ID_COL_1" javaType="java.lang.Integer"/>
   <id property="id.id2" column="ID_COL_2" javaType="java.lang.Integer"/>
   <id property="id.id3" column="ID_COL_3" javaType="java.lang.String"/>
   <result property="other" column="OTHER_COL" />
</resultMap>

我采用了第一个方法,即在将对象传递给线程时保持只读"类的状态下编写更少的样板文件.容器类的结构如下:

I adopted the first to have less boilerplate written while keeping class "read-only" when passing objects to threads. The container class is structured like this:

+ public interface MyClass // declares getters
+- public interface Id {...} // MyClass.Id declares getters for id's
|
+--+ public interface MyClassWritable extends MyClass // declares setters
   |
   +-- public class MyClassImpl implements MyClassWritable

这篇关于从JPA到MyBatis:组合键?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-22 09:26