override the method "private boolean insert()"

66 views
Skip to first unread message

Gladyston Batista

unread,
Oct 1, 2012, 9:54:47 AM10/1/12
to activejd...@googlegroups.com
Hi.

I need to override the method "private boolean insert()", file "./activejdbc/src/main/java/org/javalite/activejdbc/Model.java."

How do without losing compatibility of future releases/updates?

Atenciosamente,
--
Gladyston Batista
Belo Horizonte - Brazil

igor

unread,
Oct 1, 2012, 3:01:06 PM10/1/12
to activejd...@googlegroups.com
Gladyston, what is the purpose of the override? In other words, what do you want to accomplish?

This method is private, you cann't really override it, you can override only save() and saverIt(), but really I want to understand the reason to help you better

Gladyston Batista

unread,
Oct 1, 2012, 3:28:11 PM10/1/12
to activejd...@googlegroups.com
Igor I can replicate this snippet of code "insert()" throughout my project, but this way is centralized.


public class Invoice extends Model {

  //composite primary key

  public static Invoice findPK(int storeId, int invoiceId) {
    return
Invoice.findFirst("storeid=? AND invoiceid=?", storeId, invoiceId);
  }

  @Override

  public boolean exists() {
    return
Invoice.findPk(get("storeid"), get("invoiceid")) == null;
  }

  @Override
  protected boolean insert() {
    boolean insert = super.insert();
    if(insert) {
        doLogInvoice();
   
    doPrintInvoice();
    }
    return insert;
  }
}



Suggestion: change the Save () method:

        if (blank(getId())) {
            result =  insert();
        } else {
            result = update();
        }

        if (exists()) {
            result =  insert();
        } else {
            result = update();
        }



Atenciosamente,
--
Gladyston  Batista
Belo Horizonte-Brazil


2012/10/1 igor <ig...@polevoy.org>

igor

unread,
Oct 1, 2012, 3:36:17 PM10/1/12
to activejd...@googlegroups.com
maybe you can use callbacks:

so you can print invoice before/after save. Changing logic of the save() method is very risky because this project has been out there for over three years, and this can break thousands of other projects. 
The other solution might be always consider that the object is new and simply try to save it. If another client saved a different one with the same composite PK, you will get an exception from DB - capture that and act on it.
Does this make sense?

thanks
igor

Gladyston Batista

unread,
Oct 1, 2012, 3:37:34 PM10/1/12
to activejd...@googlegroups.com
Another example: I need to method "insert" execute "replace":

public class Customer extends Model {
  @Override
  public boolean insert() {
    int i = 0;
    MetaModel metaModel = getMetaModel();
    metaModel.getAttributeNamesSkipGenerated();
    String query = "REPLACE INTO " + metaModel.getTableName() + " SET ";
    Map<String, Object> names = getAttributes();
    for (String name : names.keySet()) {
      if (names.get(name) instanceof String) {
        query += name + " = \"" + names.get(name) + "\"";
      } else {
        query += name + " = " + names.get(name);
      }
      if (i++ < names.size() - 1) {
        query += ", ";
      }
    }
    int insert = new DB(metaModel.getDbName()).exec(query);
    if (metaModel.cached()) {
      QueryCache.instance().purgeTableCache(metaModel.getTableName());
    }
    return insert > 0;
 
}
}

Atenciosamente,
--
Gladyston  Batista
Belo Horizonte - Brazil


2012/10/1 Gladyston Batista <glbat...@gmail.com>

igor

unread,
Oct 1, 2012, 3:41:13 PM10/1/12
to activejd...@googlegroups.com
why do you care to REPLACE, ActiveJDBC will run UPDATE if ID exists, which will give you the same result, right?

Gladyston Batista

unread,
Oct 1, 2012, 4:29:57 PM10/1/12
to activejd...@googlegroups.com
Igor, sorry.

I talked with the manager of the development and he said the "insert" problem resolves with "callback".

The software is that we are rewriting is old (almost 20 years) and has several tables "primary key composed" and it is not possible to change the tables. Then the methods "update" and "delete" must be overridden to generate the clause "where" correctly.

Example:


public class Invoice extends Model {
 
  //private boolean update() {
  //  .....
  //  query += " where " + metaModel.getIdName() + " = ?";
  //  values.add(getId());
  //  .....
  //}


  private boolean update() {
    .....
    query += " where
storeid = ? AND invoiceid = ?";
    values.add(get("storeid"));
    values.add(get("invoiceid"));
    .....
  }
}


Atenciosamente,
--
Gladyston Pereira Batista
Sócio-Diretor / Stone Tecnologia
Belo Horizonte-MG / (31)8638-8101


2012/10/1 igor <ig...@polevoy.org>

igor

unread,
Oct 1, 2012, 4:31:29 PM10/1/12
to activejd...@googlegroups.com
great, so is this problem resolved then?

Gladyston Batista

unread,
Oct 1, 2012, 4:46:52 PM10/1/12
to activejd...@googlegroups.com
Suggestion: Implement a method to generate the clause "where" and allow it to be overwritten. Thus, the "insert", "update" and "delete" may continue "private"

public abstract class Model extends CallbackSupport implements Externalizable {
  protected HashMap generateWhere() {
    HashMap<String, Object> where = new HashMap<String, Object>();
    where.put(metaModel.getIdName(), getId()); //key: field name, value: field value
    return where;
  }
}



public class Invoice extends Model {
  @Override
  protected
HashMap generateWhere() {
    HashMap<String, Object> where = new HashMap<String, Object>();
    where.put("
storeid", get("storeid"));
    where.put("invoiceid", get("invoiceid"));
    return where;
  }
}

igor

unread,
Oct 1, 2012, 4:48:43 PM10/1/12
to activejd...@googlegroups.com
is this because you do not have surrogate PKs? 

Gladyston Batista

unread,
Oct 1, 2012, 4:51:16 PM10/1/12
to activejd...@googlegroups.com
and because it is an old system (legacy) built more than 20 years in C and it is not possible to change the tables.

--
Gladyston



2012/10/1 igor <ig...@polevoy.org>

Gladyston Batista

unread,
Oct 1, 2012, 5:02:22 PM10/1/12
to activejd...@googlegroups.com
Example: table "products the order"

CREATE TABLE `eoprd` (
  `ordno` int(10) NOT NULL DEFAULT '0',
  `qtty` int(10) NOT NULL DEFAULT '0',
  `price` bigint(15) NOT NULL DEFAULT '0',
  `seqno` int(10) NOT NULL DEFAULT '0',
  `auxLong1` int(10) NOT NULL DEFAULT '0',
  `auxLong2` int(10) NOT NULL DEFAULT '0',
  `auxLong3` int(10) NOT NULL DEFAULT '0',
  `auxLong4` int(10) NOT NULL DEFAULT '0',
  `auxMy1` bigint(15) NOT NULL DEFAULT '0',
  `auxMy2` bigint(15) NOT NULL DEFAULT '0',
  `storeno` smallint(5) NOT NULL DEFAULT '0',
  `empno` smallint(5) NOT NULL DEFAULT '0',
  `status` smallint(5) NOT NULL DEFAULT '0',
  `bits` smallint(5) NOT NULL DEFAULT '0',
  `padbyte` smallint(5) NOT NULL DEFAULT '0',
  `reserved` smallint(5) NOT NULL DEFAULT '0',
  `bits2` smallint(5) NOT NULL DEFAULT '0',
  `bits3` smallint(5) NOT NULL DEFAULT '0',
  `bits4` smallint(5) NOT NULL DEFAULT '0',
  `bits5` smallint(5) NOT NULL DEFAULT '0',
  `prdno` char(16) NOT NULL DEFAULT '',
  `grade` char(8) NOT NULL DEFAULT '',
  `memo` char(180) NOT NULL DEFAULT '',
  `auxString` char(20) NOT NULL DEFAULT '',
  PRIMARY KEY (`storeno`,`ordno`,`prdno`,`grade`),
  KEY `i2` (`ordno`),
  KEY `i3` (`prdno`,`grade`,`storeno`),
  KEY `i4` (`storeno`,`ordno`,`seqno`,`prdno`,`grade`),
  KEY `i5` (`storeno`,`status`,`prdno`,`grade`),
  KEY `i6` (`storeno`,`prdno`,`grade`,`status`)
) ENGINE=MyISAM;

Atenciosamente,
--
Gladyston Pereira Batista
Sócio-Diretor / Stone Tecnologia
Belo Horizonte-MG / (31)8638-8101


2012/10/1 Gladyston Batista <glbat...@gmail.com>

Gladyston Batista

unread,
Oct 1, 2012, 5:15:47 PM10/1/12
to activejd...@googlegroups.com
is this because you do not have surrogate PKs?

resp: yes. the PK are all "natural"

Example:
table store {PK: id_store }
//not have surrogate PK
table order {PK: id_store, id_order } //not have surrogate PK
table orderProduct {PK: id_store, id_order, id_product, id_grade } //not have surrogate PK

igor

unread,
Oct 1, 2012, 5:30:26 PM10/1/12
to activejd...@googlegroups.com
OK, no problem. Please, pull down sources, add your code, ensure that all tests are still working, and submit a patch. 
I will verify that all is working across multiple databases, and if no regression defects are found, I will integrate the patch and make a new release

thanks
igor

igor

unread,
Oct 1, 2012, 5:33:17 PM10/1/12
to activejd...@googlegroups.com
BWT, if only you could simply add a surrogate PK to the table, all would be fine. 

Gladyston Batista

unread,
Oct 1, 2012, 7:14:01 PM10/1/12
to activejd...@googlegroups.com
also agree, but unfortunately the customer does not allow to change the structure of the tables under any circumstances (this was a pre-requisite). :-(
The old system (in C) will still continue to run in parallel with the new.

igor

unread,
Oct 2, 2012, 12:41:45 PM10/2/12
to activejd...@googlegroups.com
ok, back to a patch :)

Gladyston Batista

unread,
Oct 15, 2012, 4:34:03 PM10/15/12
to activejd...@googlegroups.com
Ok Igor. Please can create a branch so I can make the changes?

--
Gladyston Batista
Senior Systems Analyst
Belo Horizonte / MG / Brazil


2012/10/1 igor <ig...@polevoy.org>

igor

unread,
Oct 15, 2012, 7:32:51 PM10/15/12
to activejd...@googlegroups.com
Gladyston, your branch:
I also made you a committer, 
let me know if you have any questions

thanks,
igor

bernha...@alpenconsulting.com

unread,
Jan 31, 2013, 8:16:11 AM1/31/13
to activejd...@googlegroups.com
Hello Igor,

First let me say I really enjoy the ease of use of ActiveJDBC, it's exactly what I was looking for. But unfortunately I am having the same issue as Gladyston: We can't change the DB design and some PKs are composite, so I need to be able to change the where clause (at least for updates). Any chances of merging the changes with the main branch?

Thanks,
Bernhard

igor

unread,
Jan 31, 2013, 1:40:27 PM1/31/13
to activejd...@googlegroups.com
Bernard, unfortunately there is no code on that branch, I guess Gladystone never has time for this contribution.



Overriding the "where" is not possible, since  this is a static method

If this is not helping, can you please explain what is the problem again?

Gladyston Batista

unread,
Jan 31, 2013, 2:00:35 PM1/31/13
to activejd...@googlegroups.com
Hello Bernard! I started the project but the changes "crack" at various points due to time and I gave up.

My solution:

1) Make "checkout" the project;


2) Change the method "save" as below:

    public boolean save() {
        .......
        .......
        if (isInsert()) { 
            result =  insert();
        } else {
            result = update();
        }
        .......
        .......
    }

and for each model I implement the "isInsert ()". Example:
  
   public class Prd extends Model {
     protected boolean isInsert() {
   
   return Prd.findFirst("key1=? AND key2=?", get("key1"), get("key2")) == null;
     }
   }


3) Change the method "update" as below:

     private boolean update () {
         .......
         .......
         List <String> primaryKeys metaModel.getPrimaryKeys = ();
         / / query = "where" metaModel.getIdName () "=";
         for (String pkName: primaryKeys) {
           if (primaryKeys.indexOf (pkName) == 0) {
             query = "WHERE" pkName "=";
           Else {}
             query = "AND" pkName "=";
           }
           values.add (get (pkName));
         }
         .......
         .......
     }


4) Implement the method "getPrimaryKeys" as below:

public class MetaModel <T extends Model, Association> extends E implements Serializable {
         .......
         .......
     <String> getPrimaryKeys public List () {
       List <String> primaryKeys <String> = new ArrayList ();
       try {
         ResultSet rs = new DB (getDbName ()). GetConnection (). GetMetadata (). GetPrimaryKeys (null, null, getTableName ());
         while (rs.next ()) {
           primaryKeys.add (rs.getString ("COLUMN_NAME"));
         }
         rs.close ();
       } Catch (SQLException e) {
         e.printStackTrace ();
       }
       primaryKeys return;
     }
}

Atenciosamente,
--
Gladyston Pereira Batista
Sócio-Diretor / Stone Tecnologia
Belo Horizonte-MG / (31)8638-8101


2013/1/31 igor <ig...@polevoy.org>
--
You received this message because you are subscribed to the Google Groups "ActiveJDBC Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to activejdbc-gro...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Reply all
Reply to author
Forward
0 new messages