Can a hibernate application share a database with another application? And be happy at the same time?
Here is my problem:
Let’s make a very simple version of my problem. I have a database with two tables, Person and Address.
Code:
Person:
+-----------+-------------+
| Name | Type |
+-----------+-------------+
| id |int not null |
| name |Varchar |
+-----------+-------------+
Address:
+-----------+-------------+
| Name | Type |
+-----------+-------------+
| id |int not null |
| person_id |int not null |
| street |Varchar |
| city |Varchar |
+-----------+-------------+
And I have a Java Servlet that can access the Oracle database thru Hibernate (let’s call it newApp) and another old application that can access the same database (let’s call it oldApp).
Now I find a person in newApp and show the person and the entire address to the client. In oldApp, I find the same person and delete on of the address.
In newApp, we have a refresh button so we can refresh the data from the database (we make a session.refresh(person)) and see if any data is changed in the database. Here comes the problems. Hibernate refreshes the data from the database by making the following selects:
select * from person where id = ?
select * from adresse where id in (?,?,?) (this person has 3 address)
BUT in oldApp, we have deleted one of the addresses, so one of the address’ id doesn’t exists anymore and we get the following exception
org.hibernate.UnresolvableObjectException: No row with the given identifier exists:
Is it possible to change the mapping (or something else) so hibernate refreshes the addressList when you refresh a person and not just refresh the address that it already knows about.
//Mads