MyBatis ManyToOne& OneToMany映射

时间:2016-01-28 11:00:30

标签: java mapping one-to-many mybatis many-to-one

我使用Java 7和MyBatis 3.2.2。 问题是如下。我有三个班 - A,B,C。

  

A有B列表,B有C列表 - 一对多

     
    

C已设置B,B已设置A. - 多对一

  
 /*
  * Java classes
  */
   public class A {
      private int id;
      private List<B> blist;
      ...
   }

  public class B {
      private int id;
      private A a;
      private List<C> clist;
      ...
   }

   public class C {
      private int id;
      private B b;
      ...
   }

   /*
    * MyBatis result maps
    */

 <resultMap id="aMap" type="A">
  <id     property="id"      column="a_id" />
  <collection property="list" column="b_id" ofType="B" resultMap="bMap"/>
 </resultMap>

 <resultMap id="bMap" type="B">
  <id     property="id"      column="b_id" />
  <association property="a"  column="a_id" resultMap="aMap"/>
  <collection property="list" column="c_id" ofType="C" resultMap="cMap"/>
 </resultMap>

 <resultMap id="cMap" type="C">
  <id     property="id"      column="c_id" />
  <association property="B"  column="b_id" resultMap="bMap"/> 
 </resultMap>

   /*
    * MyBatis select
    */

<select id="get..."  resultMap="xMap">
 SELECT
    a.id AS a_id,
    b.id AS b_id,
    c.id AS c_id
 FROM A a
 LEFT JOIN B b ON a.id = b.idA
 LEFT JOIN C c ON b.id = c.idB
</select>

/ *  *问题  * /

当resultMap xMap = aMap 时,一切都是正确的。创建对象并正确填充

当resultMap xMap = cMap 时,会抛出以下错误。

当来自 aMap 删除了B 的集合时 - 一切都是正确的。   - 这很奇怪,看起来mybatis只能映射到一定的深度?

这个问题使我的工作在mybatis元素的可重用性方面变得非常复杂。当然,我可以创建另一个结果图,但它破坏了可重用性原则。

知道如何解决这个问题吗?

/ *  *错误 - MyBatis抛出以下异常:  * /

org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.reflection.ReflectionException: Could not set property 'blist' of 'A' with value 'B [id=1]' Cause: java.lang.IllegalArgumentException: argument type mismatch
    at org.mybatis.spring.MyBatisExceptionTranslator.translateExceptionIfPossible(MyBatisExceptionTranslator.java:73)
    at org.mybatis.spring.SqlSessionTemplate$SqlSessionInterceptor.invoke(SqlSessionTemplate.java:364)
    at com.sun.proxy.$Proxy23.selectList(Unknown Source)
    at org.mybatis.spring.SqlSessionTemplate.selectList(SqlSessionTemplate.java:194)
    at org.apache.ibatis.binding.MapperMethod.executeForMany(MapperMethod.java:114)
    at org.apache.ibatis.binding.MapperMethod.execute(MapperMethod.java:58)
    at org.apache.ibatis.binding.MapperProxy.invoke(MapperProxy.java:43)
    at com.sun.proxy.$Proxy45.getKeywordsByNodeResult(Unknown Source)
    at xTest.testGetKeywordsByNodeResult(WorkflowMapperTest.java:35)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
    at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:72)
    at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:81)
    at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:72)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:216)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:82)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
    at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:60)
    at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:67)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:162)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)

.....

1 个答案:

答案 0 :(得分:0)

你真的不需要MyBatis这样做。您可以通过以下方式定义父级跟踪的子级列表:

public abstract class ListWithParentTracking<Child> extends ArrayList<Child>
{

    @Override
    public boolean add(Child c)
    {
        this.setParent(c);
        return super.add(c);
    }

    protected abstract void trackParent(Child c);

};

然后将您的A类定义为:

public class A {
  private int id;
  private final List<B> blist;
  ...
  public A()
  {
     final A me = this;
     this.blist = new ListWithParentTracking<B>()
     {
        @Override
        protected void trackParent(B b)
        {
           b.setA(me);
        } 
     }
  }
} 

让你自己的对象发挥魔力。