-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: Question in servlets environment
PostPosted: Wed Jan 18, 2006 11:57 pm 
Newbie

Joined: Wed Jan 18, 2006 11:48 pm
Posts: 1
Hello all,

I have a dude about the use of ORMs in general, in this case Hibernate ;)

Well, supose we have a two persistent class

Person.java

Long id;
String name;
String surname;
Country country;

Country.java

Long id;
String name


then when a user click in my creator form we recibe parameters

name, surname, countryId (from a combobox)

MY QUESTION:

Is necessary to find first for Country and when we have the object reference persist person?

Actually in SQL is sufficient with these 3 parameters an the person id is auto_increment in mysql for example

So in resume, is necesary all the data in country when we really insert a row in person table ONLY

Thanks in advance

kodaky


Top
 Profile  
 
 Post subject: Re: Question in servlets environment
PostPosted: Thu Jan 19, 2006 1:19 am 
Newbie

Joined: Thu Jul 14, 2005 5:39 pm
Posts: 12
Location: Ann Arbor, MI
Hi kodaky,

I think you're looking for something like the following:

Code:
  // ... Start Hibernate session

  Person person = new Person();
  person.setName(request.getParameter("name"));
  person.setSurname(request.getParameter("surname"));
  Country country = session.load(Country.class,
        new Long(request.getParameter("countryId")));
  person.setCountry(country);
  session.save(person);

  // ... Finish session


If you use session.get(..), then the Country object will be queried out. If you use session.load(..), however, you're actually getting a proxy-subclass of the Country class, with properties unset. These properties are "lazily" set, meaning that the full Country object will be queried out only-if and not-until you try to access a property of it (e.g. country.getName()).

Thus, the above code will only insert into the Person table (including the countryId foreign key), and not query the Country table.

-- Jim Steinberger


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.