Hi All,
In my application I have loaded the user information and stored in Http session object. In one scenario I want to save record in address table which has userid as foreign key. Here when I try to save address Object which has a User object by using below code: Address address=new Address(); User user=new User() user.setUserId(1);//Id will be retrieved from Http session object. address.setUser(user); session.save(address); //The above lines of code throws exception.
but when I use below lines of code it works perfectly but hits the DB to fetch user object. Address address=new Address(); User user=(User)session.get(User.class,1);//one hit for fetching user and same for session.load() also address.setUser(user); session.save(address);//one hit to save address
How to avoid the hit to fetch user as I have userid already with me.
|