-1

I am using MongoDb database and grails 2.2.2 in this i want to assign a random Id rather than by default incremental Id generating by Db.

for e.g : abcd.com/demo/view/14524 rather than
abcd.com/demo/view/5

my domain code:

student.groovy

class student{
static mapping = {
        id generator:'assigned'
}

static constraints = {
        id generator:'assigned'
}

def beforeInsert() {
        Random randomNumber = new Random();

        id=randomNumber.nextInt(100**4);

        println "id "+ id;

    }
}

this is dependencies list :-

grails.project.dependency.resolution = { 
    // inherit Grails' default dependencies 
    inherits("global") { 
        // specify dependency exclusions here; for example, uncomment this to disable ehcache: 
        // excludes 'ehcache' 
    } 
    log "error" // log level of Ivy resolver, either 'error', 'warn', 'info', 'debug' or 'verbose' 
    checksums true // Whether to verify checksums on resolve 
    legacyResolve false // whether to do a secondary resolve on plugin installation, not advised and here for backwards compatibility 

    repositories { 
        inherits true // Whether to inherit repository definitions from plugins 

        grailsPlugins() 
        grailsHome() 
        grailsCentral() 

        mavenLocal() 
        mavenCentral() 

        // uncomment these (or add new ones) to enable remote dependency resolution from public Maven repositories 
        //mavenRepo "http://snapshots.repository.codehaus.org" 
        //mavenRepo "http://repository.codehaus.org" 
        //mavenRepo "http://download.java.net/maven/2/" 
        //mavenRepo "http://repository.jboss.com/maven2/" 
    } 

    dependencies { 
        // specify dependencies here under either 'build', 'compile', 'runtime', 'test' or 'provided' scopes e.g. 

        // runtime 'mysql:mysql-connector-java:5.1.20' 
                compile 'org.imgscalr:imgscalr-lib:4.1' 
    } 

    plugins { 
        runtime ":hibernate:$grailsVersion" 
        runtime ":jquery:1.8.3" 

        runtime ":resources:1.1.6" 

        // Uncomment these (or add new ones) to enable additional resources capabilities 
        runtime ":zipped-resources:1.0" 
        runtime ":cached-resources:1.0" 
        //runtime ":yui-minify-resources:0.1.4" 

                compile ":cache-headers:1.1.5" 

        build ":tomcat:$grailsVersion" 

        runtime ":database-migration:1.2.1" 

        compile ':cache:1.0.1' 

                compile ":rest:0.7" 

                compile ":rendering:0.4.4" 

                compile ":mail:1.0.1" 

                compile ":oauth:2.1.0" 

                compile ":spring-security-core:1.2.7.3" 

                compile ":spring-security-ui:0.2"   

                compile ":jquery-ui:1.8.24" 

                compile ":famfamfam:1.0.1" 

                compile ":calendar:1.2.1" 

    } 
} 

but its giving error. Is any other option to assign a random Id?

Thank You. :)

SAGAR MANE
  • 685
  • 2
  • 8
  • 23

1 Answers1

1

You can use the Mongo's ObjectId as a random identifier:

import org.bson.types.ObjectId

class Student {
   ObjectId id
   String name
}
Lari Hotari
  • 5,190
  • 1
  • 36
  • 43
  • So,ObjectId creates Random Id? – SAGAR MANE Aug 07 '14 at 08:27
  • you can also make the id type of String. I'm not sure about the randomness. Why do you want it to be random? – Lari Hotari Aug 07 '14 at 08:36
  • so that user can not get total number of user on my website – SAGAR MANE Aug 07 '14 at 09:18
  • ObjectId or String type id with the mongodb plugin should be ok for you in that case. – Lari Hotari Aug 08 '14 at 06:39
  • thank you , Nut now its giving me an error when i am using "ObjectId" ,"No converter found capable of converting from type java.lang.Integer to type org.bson.types.ObjectId" – SAGAR MANE Aug 08 '14 at 07:43
  • That's probably caused by some code that's expecting the id to be a Integer. Checkout target/stacktrace.log for the exact stacktrace if you cannot find the location in the error message. – Lari Hotari Aug 08 '14 at 08:15
  • Canyou please help me.. how to remove gmongo dependency. from the dependencies.. i Updated My Question As well please check out.. – SAGAR MANE Aug 27 '14 at 06:20