Kripton ORM: how to store byte arrays

Abubusoft Blog

Kripton ORM: how to store byte arrays

Stackoverflow is always a good place to get ideas to write posts. Today I answer to this question. The original question is about using Room Library. But I want to change the question: how can I persist a byte array using Kripton ORM Library?

With kripton, you can easily manage byte[] fields. Just to show you how to do:

I define a bean FileBean binded to a table file_bean:

public class FileBean {
  public long id;
  public Date date;
  public String title;
  public String text;
  public String address;
  public byte[] image;

A DAO definition:

public interface FileBeanDao {
  long insert(FileBean bean);
  List selectById(long id);

And a data source interface:

@BindDataSource(daoSet={FileBeanDao.class}, fileName = "example.db")
public interface ExampleDataSource {

After project configuration to use Kripton and its annotation processor in Gradle config file:

dependencies {
    // annotation processors
    // dependencies
    implementation "com.abubusoft:kripton-android-library:6.0.0"

Kripton will generate for you a DAO implementation like this (Javadoc too):

 * <p>
 * DAO implementation for entity <code>FileBean</code>, based on interface <code>FileBeanDao</code>
 * </p>
 *  @see FileBean
 *  @see FileBeanDao
 *  @see FileBeanTable
public class FileBeanDaoImpl extends AbstractDao implements FileBeanDao {
  public FileBeanDaoImpl(BindExampleDataSource dataSet) {
   * <p>SQL insert:</p>
   * <pre>INSERT INTO file_bean (date, title, text, address, image) VALUES (${}, ${bean.title}, ${bean.text}, ${bean.address}, ${bean.image})</pre>
   * <p><code></code> is automatically updated because it is the primary key</p>
   * <p><strong>Inserted columns:</strong></p>
   * <dl>
   *    <dt>date</dt><dd>is mapped to <strong>${}</strong></dd>
   *    <dt>title</dt><dd>is mapped to <strong>${bean.title}</strong></dd>
   *    <dt>text</dt><dd>is mapped to <strong>${bean.text}</strong></dd>
   *    <dt>address</dt><dd>is mapped to <strong>${bean.address}</strong></dd>
   *    <dt>image</dt><dd>is mapped to <strong>${bean.image}</strong></dd>
   * </dl>
   * @param bean
   *    is mapped to parameter <strong>bean</strong>
   * @return <strong>id</strong> of inserted record
  public long insert(FileBean bean) {
    ContentValues contentValues=contentValues();
    if (!=null) {
      contentValues.put("date", DateUtils.write(;
    } else {
    if (bean.title!=null) {
      contentValues.put("title", bean.title);
    } else {
    if (bean.text!=null) {
      contentValues.put("text", bean.text);
    } else {
    if (bean.address!=null) {
      contentValues.put("address", bean.address);
    } else {
    if (bean.image!=null) {
      contentValues.put("image", bean.image);
    } else {
    // log for insert -- BEGIN 
    StringBuffer _columnNameBuffer=new StringBuffer();
    StringBuffer _columnValueBuffer=new StringBuffer();
    String _columnSeparator="";
    for (String columnName:contentValues.keySet()) {
      _columnSeparator=", ";
    }"INSERT INTO file_bean (%s) VALUES (%s)", _columnNameBuffer.toString(), _columnValueBuffer.toString());
    // log for content values -- BEGIN
    Object _contentValue;
    for (String _contentKey:contentValues.keySet()) {
      if (_contentValue==null) {"==> :%s = <null>", _contentKey);
      } else {"==> :%s = '%s' (%s)", _contentKey, StringUtils.checkSize(_contentValue), _contentValue.getClass().getCanonicalName());
    // log for content values -- END
    // log for insert -- END 
    long result = database().insert("file_bean", null, contentValues);;
    return result;
   * <h2>Select SQL:</h2>
   * <pre>SELECT id, date, title, text, address, image FROM file_bean WHERE id=${id}</pre>
   * <h2>Projected columns:</h2>
   * <dl>
   *    <dt>id</dt><dd>is associated to bean's property <strong>id</strong></dd>
   *    <dt>date</dt><dd>is associated to bean's property <strong>date</strong></dd>
   *    <dt>title</dt><dd>is associated to bean's property <strong>title</strong></dd>
   *    <dt>text</dt><dd>is associated to bean's property <strong>text</strong></dd>
   *    <dt>address</dt><dd>is associated to bean's property <strong>address</strong></dd>
   *    <dt>image</dt><dd>is associated to bean's property <strong>image</strong></dd>
   * </dl>
   * <h2>Query's parameters:</h2>
   * <dl>
   *    <dt>${id}</dt><dd>is binded to method's parameter <strong>id</strong></dd>
   * </dl>
   * @param id
   *    is binded to <code>${id}</code>
   * @return collection of bean or empty collection.
  public List<FileBean> selectById(long id) {
    StringBuilder _sqlBuilder=getSQLStringBuilder();
    _sqlBuilder.append("SELECT id, date, title, text, address, image FROM file_bean");
    // generation CODE_001 -- BEGIN
    // generation CODE_001 -- END
    ArrayList<String> _sqlWhereParams=getWhereParamsArray();
    // manage WHERE arguments -- BEGIN
    // manage WHERE statement
    String _sqlWhereStatement=" WHERE id=?";
    // manage WHERE arguments -- END
    // build where condition
    String _sql=_sqlBuilder.toString();
    String[] _sqlArgs=_sqlWhereParams.toArray(new String[_sqlWhereParams.size()]);;
    // log for where parameters -- BEGIN
    int _whereParamCounter=0;
    for (String _whereParamItem: _sqlWhereParams) {"==> param%s: '%s'",(_whereParamCounter++), StringUtils.checkSize(_whereParamItem));
    // log for where parameters -- END
    try (Cursor cursor = database().rawQuery(_sql, _sqlArgs)) {"Rows found: %s",cursor.getCount());
      LinkedList<FileBean> resultList=new LinkedList<FileBean>();
      FileBean resultBean=null;
      if (cursor.moveToFirst()) {
        int index0=cursor.getColumnIndex("id");
        int index1=cursor.getColumnIndex("date");
        int index2=cursor.getColumnIndex("title");
        int index3=cursor.getColumnIndex("text");
        int index4=cursor.getColumnIndex("address");
        int index5=cursor.getColumnIndex("image");
          resultBean=new FileBean();
          if (!cursor.isNull(index1)) {; }
          if (!cursor.isNull(index2)) { resultBean.title=cursor.getString(index2); }
          if (!cursor.isNull(index3)) { resultBean.text=cursor.getString(index3); }
          if (!cursor.isNull(index4)) { resultBean.address=cursor.getString(index4); }
          if (!cursor.isNull(index5)) { resultBean.image=cursor.getBlob(index5); }
        } while (cursor.moveToNext());
      return resultList;

Kripton, how you can see from generated code, natively persists byte[] fields into BLOB type columns. No special operation or configuration is required.

Quite easy no? For any question does not hesitate to comment on this post.

For more information about Kripton Persistence Library:


Leave a Reply

Your email address will not be published. Required fields are marked *