Monday, 30 May 2011

sMash: Insert a record in database table and return the auto generated key

The code sample below inserts a record in table1 which contains three columns ID, FIRST_NAME & LAST_NAME.

Comment:
zero.data.groovy.Manager assumes for simplicity that the auto generated key is always of type java.lang.Number. However, in zero.data.Manager, the return value of the method is the key itself cast to a class you define.

def storeRecord(referenceMap){

mgr=zero.data.groovy.Manager.create('development/services');
def query="""
INSERT INTO TABLE1
(FIRST_NAME,LAST_NAME)
VALUES (${referenceMap.fname},${referenceMap.lname})
""";
def id = mgr.insert(query,["id"]);
logger.INFO {"Reference inserted with ID: " +id};

return id;
}

No comments:

Post a Comment