---------- Forwarded message ---------- From: Oliver Zeigermann <[EMAIL PROTECTED]> Date: Tue, 30 Nov 2004 17:30:02 +0100 Subject: Re: A few dumb questions To: [EMAIL PROTECTED]
On Mon, 29 Nov 2004 23:32:34 +0000 (GMT), Kris A. Jenkins <[EMAIL PROTECTED]> wrote: > --- Oliver Zeigermann <[EMAIL PROTECTED]> > wrote: > > > > Kris, > > > > see comments inline... > > > > > > On Mon, 29 Nov 2004 18:05:39 +0000, Kris Jenkins > > <[EMAIL PROTECTED]> wrote: > > > The thing I like about SqlMap (that I vehemently > > dislike about most > > > alternatives) is that it doesn't get in my way. > > It gives me full unfettered > > > access to the SQL that fetches my data. So I know > > that if I can construct > > > the SQL query, SqlMap can handle it. Just as > > important, I know that if I > > > /optimise/ that query, SqlMap won't assume it > > knows better. > > > > I see, what is the benefit over using pure SQL, JDBC > > or something like > > http://jakarta.apache.org/commons/dbutils/index.html > > then? > > String answer = "It may sound really simple," + > " but the first reason I turned away from" + > " solutions like dbutils is that I was fed" + > " up of writing statements like this."; > > StringBuffer answer2 = new StringBuffer(); > answer2.append( "It sounds like a small thing," ); > answer2.append( " but you try porting a query from an > SQL prompt" ); > answer2.append( " and then into Java, and then back" > ); > answer2.append( " and then back into your SQL prompt" > ); > > ...and you'll find it gets incredibly tedious. You > can only trim off the "'s and +'s so many times before > you flip. That's when I screamed, "Why can't I just > stick this in a £*$%^"ing query seperate file?!?" I > went looking again. I found SqlMap. Heaven and earth > rejoiced. :-D > > SqlMap lets me manage my own database access. It lets > me make my own decisions about what will run fast or > slow. "And once I've made those decisions" + " it > helps me" + " take the pain out of" + " > implementing/debugging/maintaining them." :-) My statements look like these: insert into OBJECT (URI_ID,CLASS_NAME) values (?,?) and Java binding works like this: statement = connection.prepareStatement("insert into OBJECT (URI_ID,CLASS_NAME) values (?,?)"); statement.setLong(1, uriid); statement.setString(2, className); Isn't that already simple and straight ahead? What's the benefit of IBatis here? > > > > > 3) if I store back a huge object which is only > > partly changed, how does > > > ibatis know which data to store back? Does it > > compare the object to > > > anything? Is caching involved in this? Or does it > > store back everything? > > > SqlMap doesn't do any 'dirtiness' checking on > > objects. It won't know to > > > only persist child[3], for example. That falls on > > the programmer I'm > > > afraid. > > > > What you describe pretty much is my scenario. As I > > am still struggling > > to get a feeling for ibatis how would such a > > solution look like? Would > > I have a mapped statement for storing a child and > > would only call it > > for child no. 3? > > Yep, pretty much. You'd have something like... > > for ( int i = 0; i < children.length; i++ ) { > if ( children[i].isNew() ) { > sqlMap.insert( "createChild", children[i] ); > } else if ( children[i].isDirty() ) { > sqlMap.update( "updateChild", children[i] ); > } > } > > ...or something like it. The point being that if > that's what you want, you'll have to implement isNew() > and isDirty(). OK, I think I have got it now :) Thanks :) > ( > Actually, that's not how I've done it. I've done > something more like: > > public void saveChildren( Child[] children ) { > for ( int i = 0; i < children.length; i++ ) { > saveChild( children[i] ); > } > } > > private void saveChild( Child child ) { > if ( child.getPrimaryKey() == null ) { > Integer pk = (Integer) sqlMap.insert( > "createChild", child ); > child.setPrimaryKey( pk ); > } else { > sqlMap.update( "updateChild", child ); > } > } > > ...but that suits my current project. YMMV. > ) Ah, so your mapping contains a statement that retrieves the (auto generated) pk after inserted? Good idea, that makes later retrieval easies, I guess... Oliver