Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mybatis - Inherited properties not being mapped

I have two classes where one inherits the other. I'm trying to map my resultSet to the subclass and Mybatis is ignoring the properties on the superclass (Setters also on the superclass)

Code is as below:

public class CocTreeNode extends CocBean implements TreeNode<CocTreeNode> {

  private String level1, level2;

  public void setLevel1(String level1){...}
  public void setLevel2(String level2){...}

  public String getLevel1(){...}
  public String getLevel1(){...}

}

public class CocBean {

  protected String name;
  protected Double volume;

  public void setName(String name){...}
  public void setVolume(Double volume){...}

  public String getName(){...}
  public Double getVolume(){...}

}

My resultMap is -

<resultMap id="simpleRow" type="CocTreeNode">
  <id property="level1" column="LEVEL1"/>
  <id property="level2" column="LEVEL2"/>
  <result property="name" column="NAME"/>
  <result property="volume" column="VOLUME"/>
</resultMap>

The resulting CocTreeNode objects are populated with 'level1' and 'level2' attributes but not 'name' and 'volume'.

I have tried using extends but that didn't make any difference.

Any ideas will be appreciated.

like image 814
Seeta Somagani Avatar asked Dec 06 '12 00:12

Seeta Somagani


People also ask

What is result map in MyBatis?

The resultMap element is the most important and powerful element in MyBatis. It's what allows you to do away with 90% of the code that JDBC requires to retrieve data from ResultSet s, and in some cases allows you to do things that JDBC does not even support.

What is TypeHandler in MyBatis?

typeHandlers. Whenever MyBatis sets a parameter on a PreparedStatement or retrieves a value from a ResultSet, a TypeHandler is used to retrieve the value in a means appropriate to the Java type.

How do I call a stored procedure in MyBatis?

Unlike IBATIS, there is no <procedure> tag in MyBatis. To map the results of the procedures, we have created a resultmap named Student and to call the stored procedure named read_recordById. We have defined a select tag with id callById, and we use the same id in the application to call the procedure.


1 Answers

You have to use extends in your simpleRow resultmap to extend properties from CocBean's resultmap:

<resultMap id="CocBeanResult" type="CocBean">
    <result property="name" column="NAME"/>
    <result property="volume" column="VOLUME"/>
</resultMap>

<resultMap id="simpleRow" type="CocTreeNode" extends="CocBeanResult">
    <result property="level1" column="LEVEL1"/>
    <result property="level2" column="LEVEL2"/>
</resultMap>
like image 135
Jan Krakora Avatar answered Oct 01 '22 14:10

Jan Krakora