I am creating a small application for myself and I am trying to insert a Radio object into the database through the Pilot Object. It is a one-to-one relationship. It seems to insert the data, since I can list the pilots and their radios after insert. But if I run the app again the pilots don't have any Radios, they are NULL. I use PilotDAO to insert data like this:
I use Derby database. The pilots stick to the tables but not the radios.
I have the following:
[ February 14, 2007: Message edited by: Henrik Engert ]
In a one-to-one mapping, Hibernate can use a couple of approaches for PK/FK relationships. You see in a database, in terms of referential integrity, does not have a concept of one-to-one. You can either have the two tables share the same PK value, which is what I believe is in your schema. Or you could have one table have a FK value to a PK in the other table.
So in your case it looks like the PK of both tables are the same. So the Radio that corresponds to the Pilot record would have the same PK value. That is what the "constrained=true" does.
Now, the next thing about the Query, and your new mapping. So now you are mapping a bi-directional mapping, which means your Radio needs to have a reference to the Pilot.
Your Pilot query returns a Pilot, but there is no Radio put in it until you try to access the radio via, getRadio() method. This is called lazy loading. Hibernate will not load the Radio in the first query because it is by default lazy loaded. So you can add to your
setting to lazy="false" means it will eager fetch the Radio at the point of your query to Pilot. However, it will still be two SQL statements to the database. To make it just one SQL statement the fetch="join" will make it run in one query using a LEFT OUTER JOIN.