0

I have a model class which only holds foreign keys to two other tables. Like this :

CREATE TABLE `ToolOSRelation` (


`toolType` varchar(100) NOT NULL DEFAULT '',
  `OSName` varchar(50) NOT NULL DEFAULT '',
  PRIMARY KEY (`toolType`,`OSName`),
  KEY `OSName` (`OSName`),
  CONSTRAINT `toolosrelation_ibfk_1` FOREIGN KEY (`toolType`) REFERENCES `Tools` (`toolType`),
  CONSTRAINT `toolosrelation_ibfk_2` FOREIGN KEY (`OSName`) REFERENCES `OS` (`OSName`)
)

Correspondingly I have the following Model class as ToolOSRelation.java :

public class ToolOSRelation {

    private Tools toolType;
    private OS OSName;

    public ToolOSRelation() {

    }
//remaining getters and setters here

After this, I have the following mybatis mapper :

<mapper namespace="com.dexter.deviceType.ToolOSRelationDao">
    <select id="getToolsForOS" resultMap="ToolOSRelationMap" parameterType="String">
        SELECT
            t.toolType
        FROM
            ToolOSRelation t
        LEFT JOIN
            OS o
        ON
            t.OSName=o.OSName
        WHERE
            o.OSName=#{osName}
    </select>

    <resultMap id="ToolOSRelationMap" type="ToolOSRelation"> 
<association property="toolType" javaType="Tools">
     <result property="toolType" column="toolType" jdbcType="VARCHAR"/> 
</association> 
<association property="OSName" javaType="OS">
    <result property="OSName" column="OSName" jdbcType="VARCHAR" />
</association>
</mapper>

javaType="OS" points to file OS.java and "Tools" points to Tools.java.

The query works fine when I run it in mysql shell. However, this returns me a single object of ToolOSRelation with all values (Tools and OS) as null. Where am I going wrong?

Edit : Adding Tools class and OS class :

public class Tools {

    private String toolType;
    private List<Template> template;
    private List<ToolOSRelation> OSName;

    public Tools() {}
    }
    public Tools(String toolType) {

        this.toolType =toolType;
    }

    public String getToolType() {
        return toolType;
    }

    public void setToolType(String toolType) {
        this.toolType = toolType;
    }
    public List<Template> getTemplate() {
        return template;
    }
    public void setTemplate(List<Template> template) {
        this.template = template;
    }

    public List<ToolOSRelation> getOSName() {
        return OSName;
    }
    public void setOSName(List<ToolOSRelation> oSName) {
        OSName = oSName;
    }

}

public class OS {

    private String OSName;
    private List<DeviceType> deviceType;
    private List<ToolOSRelation> toolType;

    public OS() {
    }

    public OS(String OSName) {
        this.OSName = OSName;
    }

    public String getOSName() {
        return OSName;
    }

    public void setOSName(String oSName) {
        OSName = oSName;
    }
    public List<DeviceType> getDeviceType() {
        return deviceType;
    }

    public void setDeviceType(List<DeviceType> deviceType) {
        this.deviceType = deviceType;
    }
    public List<ToolOSRelation> getToolType() {
        return toolType;
    }

    public void setToolType(List<ToolOSRelation> toolType) {
        this.toolType = toolType;
    }
}

1 Answers1

0

I suppose that is Mybatis cant find inflected properties, have you checked setters and getter in the model class.

Hong
  • 3
  • I did. So this is the file along with the getters and setters : `public class ToolOSRelation { private Tools toolType; private OS OSName; public ToolOSRelation() { } public ToolOSRelation(Tools toolType , OS OSName) { this.toolType = toolType; this.OSName = OSName; } public Tools getToolType() { return toolType; } public void setToolType(Tools toolType) { this.toolType = toolType; } public OS getOSName() { return OSName; } public void setOSName(OS oSName) { OSName = oSName; } }` – Kumar Pratyush Jan 21 '16 at 10:02
  • What exactly do you mean by `javaType="toolTypesMap"` Is it the model file or just a ? Sorry, but I am new at this. – Kumar Pratyush Jan 21 '16 at 11:39
  • `javaType` is a attribute of `association` tag, it tells the type of the association, i havent written a Mybatis mapper like you do, but my way works. and remember add `jdbcType` attribute into each result tag. – Hong Jan 21 '16 at 11:46
  • I did add the javaType. Yet, I am not getting the result. Is there anything else that I am doing wrong? – Kumar Pratyush Jan 21 '16 at 11:59
  • can i see your latest code? and put class `OS` and class `Tools ` as well. – Hong Jan 21 '16 at 12:03
  • Updated problem statement. Also added `OS` and `Tools` class. – Kumar Pratyush Jan 21 '16 at 12:16
  • ' ' is the property `column="toolType"` matched the name in your database? anyway it shouldn't looks like that. – Hong Jan 21 '16 at 13:05
  • Yes it does match from the database column name. Why shouldn't it look like this? Can you please elaborate? – Kumar Pratyush Jan 21 '16 at 15:55
  • column names should be like `tool_type`, this is a standard. – Hong Jan 21 '16 at 16:35
  • here is a fault in your sql script i just noticed, varchar can neither be `Foreign Key` nor `PRIMARY KEY`, those keys must be int, tinyint or small int in mysql for instance. – Hong Jan 21 '16 at 16:51
  • Actually varchar can be Foreign key or Primary Key. It's not a good way though. It will work if it holds unique values. So, problem lies somewhere else – Kumar Pratyush Jan 27 '16 at 08:10