GRAILS – How to retrieve the latest created database entry?

I have the following classes and need to manually add the nextId field.

class SomeIdClass {

Family family
Integer nextId = 0
long timeCreated = new Date().time }

So far, I have been trying to retrieve and the latest database entry to increase it, I Just no luck. Or am I just solving it in a completely wrong way?

Thaks

ps: This is a list of Package.SomeId objects I tried but got

def si = SomeId.executeQuery (" from SomeId where nextId = (select max( nextId) from SomeId) ")

you can Do it this way:

def maxNextId = DomainClass.executeQuery("select max(nextId) from DomainClass")[0]

I haven’t seen In the context of the whole context, it is difficult to tell what you are doing, but other than that, it seems suspicious. The method of assigning the id to the domain object may be the wrong method. But anyway, if you are executing a query and What happens when a new object is inserted into a database with a larger nextId between using values?

I have the following classes and need to manually add the nextId field.

class SomeIdClass {

Family family
Integer nextId = 0
long timeCreated = new Date().time }

So far, I have been trying to retrieve and latest database entries to Add it, I just had no luck. Or am I just solving it in a completely wrong way?

Thaks

ps: This is a list of Package.SomeId objects I tried but got

def si = SomeId.executeQuery (" from SomeId where nextId = (select max( nextId) from SomeId) ")

You can do this:

def maxNextId = DomainClass.executeQuery("select max(nextId) from DomainClass")[0]

Without seeing the entire background, it’s hard to tell What to do, but other than that, this seems suspicious. The method of assigning the id to the domain object may be the wrong method. But anyway, if you insert a new object with a larger value between executing the query and using the value What happens in the database of nextId?

Leave a Comment

Your email address will not be published.