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?

biniam
  • 8,099
  • 9
  • 49
  • 58
user2679352
  • 81
  • 1
  • 2
  • 5

3 Answers3

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.

Foo Bar User
  • 2,401
  • 3
  • 20
  • 26
  • 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? – user2679352 Oct 04 '13 at 17:54
  • 1
    what are you trying to do? save an author? – Foo Bar User Oct 04 '13 at 17:55
  • 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. – user2679352 Oct 04 '13 at 18:00
  • Basically, the issue is in reading the Author record, it throws the "No row with the given identifier exists" exception which break the page. – user2679352 Oct 04 '13 at 18:01
  • Maybe is there a way to catch the exception on the gsp page? – user2679352 Oct 04 '13 at 18:04
  • 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 exist – Foo Bar User Oct 04 '13 at 18:04
  • My record has an attached account that does not exist. – user2679352 Oct 04 '13 at 18:05
  • I'm trying to handle this use case on the gsp page – user2679352 Oct 04 '13 at 18:05
  • then you should create an account with id 123465489785, or change the authors accountid to an id that exists – Foo Bar User Oct 04 '13 at 18:06
  • I don't have this option to create account. I'm reading legacy data. – user2679352 Oct 04 '13 at 18:09
  • 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 do – Foo Bar User Oct 04 '13 at 18:17
2

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

biniam
  • 8,099
  • 9
  • 49
  • 58
0

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

Procrastinator
  • 2,526
  • 30
  • 27
  • 36