Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hibernate chokes on missing rows when dealing with a legacy database

I am trying to implement hibernate on a legacy database (that still has a legacy PHP client), and am running into some problems because the people who wrote the original app had no idea what they were doing.

The database is set up so that none of the columns are nullable, so they default foreign keys to 0 if there is no record for them. Additionally, they don't have proper foreign keys on the tables so there are a few with invalid IDs. I do not have an option to change the schema or null the appropriate columns.

This is the error I get from hibernate:

Caused by: org.hibernate.ObjectNotFoundException: No row with the given identifier exists: [com.tv.platform.domain.Program#0]

What I would like is a graceful way to deal with this muck, where the field will just null if the row is invalid or doesn't exist, but I am not having any luck finding how to handle this in the documenation.

Any tips?

like image 984
liam Avatar asked Apr 26 '11 18:04

liam


3 Answers

The annotation: @NotFound( action = NotFoundAction.IGNORE )

Does exactly what I was looking for. I found it through here:

Hibernate Many-To-One Foreign Key Default 0

like image 187
liam Avatar answered Oct 19 '22 19:10

liam


I don't think Hibernate is a good fit for this type of problem. Hibernate expects the records in your tables to relate to each other, and really can't work if the foreign-key relationships are only sometimes enforced. I can't imagine it would be easy to change or configure Hibernate to behave this way - to know how to deal with broken foreign-key relationships.

You may get more mileage out of a framework like MyBatis SQLMaps, in which you provide the SQL statements to load your data in files external to your program, but the framework provides options for chaining SELECT statements together to load full object graphs. This way you could supplement the SQL statements with logic to filter out the 0 values.

like image 31
matt b Avatar answered Oct 19 '22 17:10

matt b


I think an interceptor might do the trick.

like image 33
Jeremy Avatar answered Oct 19 '22 17:10

Jeremy