Simple ORM in Groovy


/ Published in: Groovy
Save to your folder(s)

Using Groovy you can extend the Sql, DataSet and GroovyResultSet objects present in the language to do simple object mapping similar to iBatis et al. An example of using the code below

/* POGO used in the mapping */
class BranchFax {
def faxCodeId, nsc, faxNumber, branchName, address1
}

/* Connect to the DB */
def db = Sql.newInstance(
"jdbc:oracle:thin:@//10.7.1.79/DATABSE",
"user1", "test1",
"oracle.jdbc.driver.OracleDriver"
)

/* create a dataset */
def bf = db.dataSet("BRANCH_FAX")

/* now work some magic */
def result01 = bf.firstRow().coerce(BranchFax)
def result02 = bf.firstRow().transmogrify(BranchFax)
def result03 = bf.transmogrify(BranchFax)
def result04 = db.transmogrify("SELECT * FROM BRANCH_FAX", BranchFax)

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.