0

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?

2
  • you could change your table definition to make the column 'key_data' nullable. Commented Feb 16, 2015 at 12:43
  • Crap, i made a boo boo in my exception report. It should have said "data1" :) The problem is that even though i put data in "data1" data does not get to the table, and the exception occurs. Commented Feb 16, 2015 at 13:06

1 Answer 1

2

Answering my own question. It seems that when posting several parameters to a mybatis mapper, the parameters are named by their position and not their name, so #{1} #{2} #{3} in my case. In order to make the mapper xml work in my case (With meaningful names) the mapper methods parameters can be annotated with the @Param tag.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.