Andrew
Andrew

Reputation: 98

Grails/Groovy CRUD Service

Hi I am a student and I am currently learning Grails and Groovy. I want to make Crud Service with a Service, Controller and a GSP. I connected my Grails application with a PostgreSQL database. My problem is I can't make the CRUD service. I did it with scaffold, then I try it manually in a controller and now I want to put it in a service. My code is this:

My service

import groovy.sql.Sql
import grails.transaction.Transactional

@Transactional
class ContactListService {
    def DataSource

    def listAction(){
        def sql = new Sql(DataSource)
        return sql.rows ("SELECT * FROM  mn")
    }

    def insertAction(){
        def sql = new Sql(DataSource)
        sql.execute("INSERT INTO  mn (id, name) VALUES ($Id,$Name)")
    }

I get this "Message: No such property: Id for class: contactlist.ContactListService"

Upvotes: 0

Views: 512

Answers (3)

Ghata.Shah
Ghata.Shah

Reputation: 11

@Andrew, You do not have to write sql queries in such scenarios which can lead you to the SQL Injection risk in your Application. Rather you can utilize GORM features to make your work easier.

Assuming that you have a class named Mn like this:

class Mn{
  Long id
  String name
}

Now, you can write list and insert action in service like this:

@Transactional
class ContactListService {

def listAction(){
    return Mn.list()
}

def insertAction(id, name){
    def mn = new Mn(id: id, name: name)
    mn.save()
}
}

Upvotes: 0

Burt Beckwith
Burt Beckwith

Reputation: 75681

For starters I would avoid using SQL unless it is needed, for example if you need to use something that is non-standard that cannot be done with Hibernate and/or GORM. Simple insert and select statements like yours are trivial with the built-in features of Grails and GORM.

There are many resources available to learn the basics of Grails. See the reference docs but also check out one or more of the many books that are available. There are no books specific to Grails 3 yet, but the core concepts in Grails 2 and 3 are very similar, so the Grails 2 books will help a lot.

The reason your code is failing is that you are referencing a non-existent Id variable in the GString with your SQL. Assuming you fix that, it will fail again because of the missing Name variable. You probably want to pass those in as method parameters from the controller and other classes that call your service, e.g.

def insertAction(long id, String name) {
    def sql = new Sql(DataSource)
    sql.execute("INSERT INTO  mn (id, name) VALUES ($id,$name)")
}

Note that you are very close to adding a SQL injection risk to your application by using SQL with embedded variables like this. You're lucky that groovy.sql.Sql understands GStrings and avoids the problem in this case though.

Upvotes: 1

injecteer
injecteer

Reputation: 20707

1st, there's no bean named DataService (what for a weird naming btw). Instead there's a bean named dataService

2nd, To solve the compile error, you must declare the Id and Name (my eyes are bleeding now!) somewhere in the context of your insertAction() method. Either as arguments, or fields or local vars, like:

def insertAction( String Id, String Name ){ // aaarrrgggghhhh
    def sql = new Sql(DataSource)
    sql.execute("INSERT INTO  mn (id, name) VALUES ($Id,$Name)")
}

Upvotes: 0

Related Questions