2
votes

I'm new in Hibernate and Groovy, i dont know why i got error id in this code. My Grails version is 2.1.1

Grails-app/Domain using Hibernate

class Deposit implements Validateable{

BigInteger **id**
BigDecimal amount
BigDecimal currentBalance

static mapping = {
    datasource 'test'
    table 'DEPOSIT'
    id column: "ID"
    amount column: "amount"
    currentBalance column: "currentBalance"

    version false
}

DepositContoller

def depositTrx(){
    def savedata = new Deposit()    
    savedata.id=3;
    savedata.amount=122223;
    savedata.currentBalance=1511122;
    savedata.save()
    redirect(uri: "/Deposit")
}

if I use id in grails-app/Domain, i got this error: batch update returned unexpected row count from update [0]; actual row count: 0; expected: 1

when I change grails-app/Domain: id become to ide or whatever, the code successfully save data to database, but page show me this error null id in Deposit entry (don't flush the Session after an exception occurs)

1
What version of grails are you using? Are you updating existing data? I think maybe assigning the ID to a new object may be the issue - could you try def saveData = Deposit.load(3)?erichelgeson
@erichelgeson i use Grails-2.1.1. i want to save(add) new transaction.user11458909

1 Answers

0
votes

If you would like to be able to manually assign the id you can set the generator to assigned:

static mapping = {
    id generator: 'assigned', column: "ID"
    ...
}

http://docs.grails.org/3.1.1/ref/Database%20Mapping/id.html