Say I have a table in a database like this:
CREATE TABLE entities(
id INT PRIMARY KEY AUTO_INCREMENT,
friendly_name VARCHAR(100),
data1 BLOB NOT NULL,
data2 BLOB NOT NULL,
loads of other fields
)
ENGINE INNODB;
And I have a MyBatis Mapper which I want to use to create an insert that only inserts friendly_name, data1 and data2, but none of the other fields.
I would like to create a method along these lines in the mapper:
public void addNewUnverifiedEntity(String friendlyName, byte[] data1, byte[] data2);
I have the following in my mapper xml:
<insert id="addNewUnverifiedEntity" useGeneratedKeys="true" keyProperty="id">
insert into entities (friendly_name,data1,data2)
values (#{friendlyName},#{data1},#{data2})
</insert>
But when I try to call the mapper method I get the following exception:
Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Column 'data1' cannot be null
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:408)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:406)
...
In the log output a little I also see this gem:
16:59:36.754 [main] DEBUG o.a.i.d.pooled.PooledDataSource - Created connection 1110623531.
16:59:36.764 [main] DEBUG java.sql.Connection - ooo Connection Opened
16:59:36.987 [main] DEBUG java.sql.PreparedStatement - ==> Executing: insert into entities (friendly_name,verified_by,key_digest,key_data) values (?,?,?,?)
16:59:36.987 [main] DEBUG java.sql.PreparedStatement - ==> Parameters: null, null, null, null
Data does not seem to actually go from the mapper to statement, and therefore the table. (I called the method with two non-empty byte arrays and a defined string)
I can see that I can create a POJO to only contain these fields but I would much rather go with the approach in my mapper.
How exactly do I go about making a method like addNewUnverifiedEntity work?