• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Inserting into table with Hibernate question

 
Henrik Engert
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

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:

POJO's:

Person:



Pilot:



Radio:



Hibernate mapping:






[ February 14, 2007: Message edited by: Henrik Engert ]
 
sathish kumar
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do you think if the sessions are being handled properly? That might be an issue for you to think about. Looking at your code, looks well to me.
 
Henrik Engert
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have based my code from the "Beginning Hibernate (Apress)" book.

If one object contains another object (one-to-one relationship) should Hibernate handle the inserts into both tables?
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"skumar"

You have been warned before, plese change your display name to meet the JavaRanch Naming Policy of using your real first and real last names or you account will be suspended.

Thanks

Mark
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are missing cascade options to tell Hibernate what to do with the related objects.

Try this

Change
<one-to-one name="radio" class="model.Radio" constrained="false"/>

to

<one-to-one name="radio" class="model.Radio" constrained="false" cascade="save-update"/>

Mark
 
Henrik Engert
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That worked a little bit better. It actually inserted the values correctly.

But now I can't select the Pilots radio.

I thought it would handle the following call for me:



When I look at the tables in DBVisualizer I don't see any connection between the Pilto table and the Radio table. No id refers to the Radio table at all.

The pilot mapping xml now has the following line of code:



and the Radio mapping xml now has the following line of code:



Any ideas?

Thanks.
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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

<one-to-one name="radio" class="model.Radio" constrained="false" cascade="save-update"/>

add



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.

Good Luck.

Mark
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic