sql - Lazy Loading using MyBatis 3 with Java -


i using mybatis (3.2.7 version) orm framework java project. i'm jpa background, keen explore lazyloading supported mybatis. couldn't across substantial.
(i configuring mybatis using java api , annotations solely querying purpose)

as per mybatis documentation: 1. lazyloadingenabled: default value=true

globally enables or disables lazy loading. when enabled, relations lazily loaded. value can superseded specific relation using fetchtype attribute on it.

2. aggressivelazyloading : default value=true

when enabled, object lazy loaded properties loaded entirely upon call of lazy properties. otherwise, each property loaded on demand.

using following attributes, tried following code:

a. java classes :

feedback.java

public class feedback implements serializable { private static final long serialversionuid = 1l;  private int id; private string message;     /**    * while loading feedback, want sender object lazily loaded    */ private user sender; private boolean seen;  // getters , setters } 

user.java

public class user implements serializable, { private static final long serialversionuid = 1l; private int id; private string email;  // getters , setters } 

b. db schema:

feedback table

                table "public.feedback"    column | type      |    modifiers                        -------------+-----------+-------------------------------------------------------  id          | integer   | primary key  seen        | boolean   | not null  sender_id   | integer   | foreign key (sender_id) references users(id)  message     | text      |  

user table:

                table "public.users"  column   | type     |     modifiers                       -------------+----------+---------------------------------------------------- id          | integer  | primary key email       | text     |  

c. configuring mybatis via java api:

datasource datasource = new pgsimpledatasource();         ((pgsimpledatasource) datasource).setservername("localhost");         ((pgsimpledatasource) datasource).setdatabasename(dbname);         ((pgsimpledatasource) datasource).setportnumber(5432);         ((pgsimpledatasource) datasource).setuser(new unixsystem().getusername());         ((pgsimpledatasource) datasource).setpassword("");          transactionfactory transactionfactory = new jdbctransactionfactory();         environment environment = new environment(dbname, transactionfactory, datasource);         configuration configuration = new configuration(environment);              configuration.addmapper(feedbackmapper.class);              //              configuration.setaggressivelazyloading(false);              sqlsessionfactory = new sqlsessionfactorybuilder().build(configuration); 

d. querying db , db queries in feedbackmapper:

d.1 code in feedbackmapper:

@select("select f.id, f.message, f.seen, f.sender_id feedback f f.id= #{feedbackid}") @results(value = {          @result(property = "id", column = "id"),         @result(property = "sender", column = "sender_id", javatype = user.class, 1 = @one(select = "getuser", fetchtype=fetchtype.default)) }) public feedback getfeedback(@param("feedbackid") int feedbackid);  @select("select id, email users id=#{id}") public user getuser(int id); 

d.2: code invoke queries in feedbackmapper

    // setup mybatis session factory , config     feedback feedback =feedbackmapper.getfeedback(70000);     system.out.println(feedback); 

but still "sender" object populated upon querying getfeedback(id). expect sender object shouldn't populated when call getsender() on fetched feedback object . please help.

my recent observations:

mybatis team has indeed got wrong in documentation ie in documentation:

  1. lazyloadingenabled: default value=true

  2. aggressivelazyloading : default value=true

    but looking @ source code:

     protected boolean lazyloadingenabled = false;  protected boolean aggressivelazyloading = true; 

    **however being corrected, results not affected , lazy loading isnt working :( **

i think found way enable lazyloading (though not cent-percent sure):

  • mybatis documentation has following setting in configuration:

setting : lazyloadtriggermethods

description : specifies object's methods trigger lazy load

valid values : method name list separated commas

default: equals,clone,hashcode,tostring

  • per source code, thing maps given in documentation:

    public class configuration { // other attributes + methods protected set<string> lazyloadtriggermethods = new hashset<string>(arrays.aslist(new          string[] { "equals", "clone", "hashcode", "tostring" })); } 
    • i have changed mybatis configuration follows:

      transactionfactory transactionfactory = new jdbctransactionfactory(); environment environment = new environment(dbname, transactionfactory,  datasource); configuration configuration = new configuration(environment);  /** *this cause why lazy loading working */   configuration.getlazyloadtriggermethods().clear();    ///////////////////////////////////////////////////   configuration.setlazyloadingenabled(true);   configuration.setaggressivelazyloading(false); 
    • the queries in mapper (mostly unchanged):

      @select("select id, message, seen, sender_id  feedback f.id= #{feedbackid}") @results(value = {     @result(property = "id", column = "id"),    @result(property = "sender", column = "sender_id", javatype = user.class, 1 = @one(select = "getuser"))  // set fetchtype default or lazy or don't set @ all-- lazy loading takes place // set fetchtype eager --sender object loaded  }) public feedback getfeedback(@param("feedbackid") int feedbackid);  @select("select id, email users id=#{id}") public user getuser(int id); 

- java code invoke mapper

        feedbackmapper mapper = sqlsession.getmapper(feedbackmapper.class);         feedback feedback =mapper.getfeedback(69999);                            system.out.println("1. feedback object before sender lazily load: \n"+ feedback);         system.out.println("2. sender loaded explicitly \n" +feedback.getsender());         system.out.println("3. feedback object after sender loading \n" + feedback); 
  • output of code

1. feedback object before sender lazily load:

{id : 69999,  message : message123, sender : null, seen : false}  2. sender loaded explicitly   {id : 65538 , email: hemant@gmail.com}  3. feedback object after sender loading:  {id : 69999, message : message123, sender : {id : 65538, email : hemant@gmail.com},  seen : false} 

  • though works satisfactorily, upon doing

configuration.getlazyloadtriggermethods().clear();

however lack of documentation in mybatis, i'm not sure, whether associated drawbacks such.


Comments

Popular posts from this blog

commonjs - How to write a typescript definition file for a node module that exports a function? -

openid - Okta: Failed to get authorization code through API call -

ios - Change Storyboard View using Seague -