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