Grails/Hibernate: No row with the given identifier exists
Asked Answered
T

3

7

I have a domain as follow:

class Author {
    String id
    static hasMany = [accounts: Account]
    static belongsTo = Account
    static mapping = {
        accounts joinTable: [name: "SOMETABLE", key: 'SOMEFIELD'], 
                 ignoreNotFound: true
    }
    static constraints = {}
}

I get the following error when no record are found. I tried the ignoreNotFound, it not working.

error message: accounts=org.hibernate.ObjectNotFoundException: 
No row with the given identifier exists: 
[com.myapplication.Account#123465489785]

it happens when trying to select join 2 records that you dont have access to insert in the db. Is there a workaround, please?

Transubstantiation answered 4/10, 2013 at 17:42 Comment(0)
V
3

it means there is no row in your Account table with id 123465489785. Your author has an account with id 123465489785. Hibernate cannot find it so it throws an exception. if its a new account make the id on the account null so that hibernate knows its a new row.

Vesicatory answered 4/10, 2013 at 17:50 Comment(11)
I understand that it means no row in the Author table exist. But is there a way to ask grails to ignore it? Can we tell grails to just return an empty list?Transubstantiation
what are you trying to do? save an author?Vesicatory
This is a legacy database. I'm just reading the record. We will not update any records. There's a many-to-many relationship, that why I had to specified the joinTable.Transubstantiation
Basically, the issue is in reading the Author record, it throws the "No row with the given identifier exists" exception which break the page.Transubstantiation
Maybe is there a way to catch the exception on the gsp page?Transubstantiation
then try and do this. select * from author where accountId=123465489785 and select * from account where accountId = 123465489785. if the first one gives you 1 row and the second query 0 rows, it means your author has an attached account that doesnt existVesicatory
My record has an attached account that does not exist.Transubstantiation
I'm trying to handle this use case on the gsp pageTransubstantiation
then you should create an account with id 123465489785, or change the authors accountid to an id that existsVesicatory
I don't have this option to create account. I'm reading legacy data.Transubstantiation
you should also state on your question 1) that it happens when trying to select join 2 that you dont have access to insert in the db. its harder to assume what your are trying to doVesicatory
H
2

Adding ignoreNotFound = true mapping solves the issue according to the Grails documentation.

Happenstance answered 24/12, 2015 at 20:49 Comment(0)
T
0

You can use Author.findById(id) instead of Author.get(id)

Thunder answered 9/6, 2022 at 2:16 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.