Search code examples
javaintellij-ideamybatishsqldb

Mybatis 3.4.6 - Data is not being inserted into HSQLDB


I am using Mybatis 3.4.6 in conjunction with HSQLDB 2.4.1 and can't seem to get any data to insert into the database. The program executes without error and seems like it completed, but when I check the database nothing is inserted.

Retrieving/selecting rows from the database seems to work correctly which leads me to believe that something is wrong with my syntax/structure in regards to the 'insert' mapping. Any help would be greatly appreciated.

configuration.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>

    <!--suppress XmlPathReference -->
    <properties resource="org/mybatis/hsqldb/db.properties"/>
    <settings>
        <!-- Globally enables or disables any caches configured in any mapper under this configuration -->
        <setting name="cacheEnabled" value="false"/>
        <!-- Sets the number of seconds the driver will wait for a response from the database -->
        <setting name="defaultStatementTimeout" value="5"/>
        <!-- Enables automatic mapping from classic database column names A_COLUMN to camel case classic Java property names aColumn -->
        <setting name="mapUnderscoreToCamelCase" value="true"/>
        <!-- Allows JDBC support for generated keys. A compatible driver is required.
        This setting forces generated keys to be used if set to true,
         as some drivers deny compatibility but still work -->
        <setting name="useGeneratedKeys" value="true"/>
    </settings>
    <typeAliases>
        <typeAlias type="org.mybatis.hsqldb.POJO.Contact" alias="contact" />
        <typeAlias type="org.mybatis.hsqldb.POJO.EIList" alias = "EIList" />
    </typeAliases>
    <environments default="development">
        <environment id="development">
            <transactionManager type="JDBC" />
            <dataSource type="POOLED">
                <!--suppress MybatisConfigXml -->
                <property name="driver" value="${jdbc.driver}" />
                <!--suppress MybatisConfigXml -->
                <property name="url" value="${jdbc.url}" />
                <!--suppress MybatisConfigXml -->
                <property name="username" value="${jdbc.username}" />
                <!--suppress MybatisConfigXml -->
                <property name="password" value="${jdbc.password}" />
            </dataSource>
        </environment>
    </environments>
    <mappers>
        <!--suppress XmlPathReference -->
        <mapper resource="org/mybatis/hsqldb/mappers-xml/ContactMapper.xml" />
        <!--suppress XmlPathReference -->
        <mapper resource="org/mybatis/hsqldb/mappers-xml/EIListMapper.xml" />
    </mappers>
</configuration>

db.properties

jdbc.driver=org.hsqldb.jdbcDriver
jdbc.url=jdbc:hsqldb:file:hsqldb/db/ipdb;shutdown=true
jdbc.username=admin
jdbc.password=password

Contact.java

package org.mybatis.hsqldb.POJO;

public class Contact {
    Integer id;
    String lastName;
    String firstName;
    String phone;
    String email;

    public Contact(Integer id, String lastName, String firstName, String phone, String email) {
        this.id = id;
        this.lastName = lastName;
        this.firstName = firstName;
        this.phone = phone;
        this.email = email;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }
}

ContactMapper.java

package org.mybatis.hsqldb.mappers_interface;

import java.util.List;

import org.mybatis.hsqldb.POJO.Contact;

public interface ContactMapper {

    Integer insert(Contact contact);

    List<Contact> selectAll();

    Contact select(Integer id);

    Integer update(Contact contact);

    Integer delete(Integer id);

}

ContactMapper.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//ibatis.apache.org//DTD Mapper 3.0//EN"
        "http://ibatis.apache.org/dtd/ibatis-3-mapper.dtd">
<mapper namespace="org.mybatis.hsqldb.mappers_interface.ContactMapper">

    <insert id="insert" parameterType="contact">
        INSERT INTO CONTACT VALUES (${id}, ${lastName}, ${firstName}, ${phone}, ${email})
    </insert>

    <update id="update">
        UPDATE CONTACT SET
        "lastName" = #{lastName},
        "firstName" = #{firstName},
        "phone" = #{phone},
        "email" = #{email}
        WHERE "id" = #{id}
    </update>

    <delete id="delete">
        DELETE FROM CONTACT WHERE "id" = #{value}
    </delete>

    <select id="selectAll" resultType="contact">
        SELECT "id", "lastName", "firstName", "phone", "email" from CONTACT
    </select>

    <select id="select" resultType="contact">
        SELECT "id", "lastName", "firstName", "phone", "email" from CONTACT where "id" = #{value}
    </select>

</mapper>

Driver.java

package org.mybatis.hsqldb;

import java.io.IOException;
import java.io.InputStream;
import java.io.Reader;
import java.util.List;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.mybatis.hsqldb.POJO.Contact;
import org.mybatis.hsqldb.POJO.EIList;
import org.mybatis.hsqldb.mappers_interface.ContactMapper;
import org.mybatis.hsqldb.mappers_interface.EIListMapper;

public class Driver {

    public static void main(String[] args)
    {
        SqlSessionFactory sqlMapper = null;
        String resource = "org/mybatis/hsqldb/configuration.xml";
        Reader reader = null;

        try {
            reader = Resources.getResourceAsReader(resource);
            sqlMapper = new SqlSessionFactoryBuilder().build(reader);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            System.out.println("Problem opening configuration.xml");
        }
        SqlSession session = sqlMapper.openSession();
        try {

            ContactMapper mapper = session.getMapper(ContactMapper.class);
            Contact testContact = new Contact(3,"'last'","'first'","'phone'","'email'");
            int result = mapper.insert(testContact);
            session.commit();
        } finally {
            session.close();
        }

    }
}

If this is a dumb question/has been asked before I apologize. Been banging my head on my keyboard for the past couple hours and can't figure out the problem.


Solution

  • Solved my own issue so I'll answer it here for anyone else who runs into the same problem.

    In the .script file I set the 'File Write Delay' to 0 ms as opposed to the original delay of 500 ms which did not seem to persist any writes to the database.

    Normal database operations are successful now.