-->
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.  [ 3 posts ] 
Author Message
 Post subject: issue with update an element of a component
PostPosted: Fri Feb 03, 2006 3:06 am 
Beginner
Beginner

Joined: Thu Mar 03, 2005 9:19 pm
Posts: 22
Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp

Hibernate version:3.1.2

Code between sessionFactory.openSession() and session.close():

log.debug("Testing update address...");
user.getAddress().setCountry(new Country("US"));
dao.saveObject(user);
log.debug("Country name is " + user.getAddress().getCountry().getCountryName());



I have two tables:
User (userId, name, addr1, addr2, city, state, countryCode)
Country (countryCode, countryName)

Address is a component class consisting of (addr1, addr2, city, state, country), where country is a class. User to Country is many-to-one. countryCode is the PK for country table.

Country constructor:
Country(final String countryCode) {
this.countryCode = countryCode;
}


The above code executes correctly. It changes the countryCode column of User table to "US". But when I call
user.getAddress().getCountry().getCountryName()
after the update, it returns null. It should hit DB and return the countryName.

If I retrieve the user by userId, then user.getAddress().getCountry().getCountryName() works correctly:
User user = (User) dao.getObject(User.class, new Long(1));
assertNotNull(user);
log.debug("Country name is " + user.getAddress().getCountry().getCountryName());


Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 17, 2006 8:03 am 
Newbie

Joined: Fri Feb 17, 2006 4:58 am
Posts: 1
What is your mapping file look like?

Did you define a many-to-one relationship to the country entity or just defined a component class with properties. In the last case it won't probably work. It should need the relationship.


Top
 Profile  
 
 Post subject: Try to get the country from the database
PostPosted: Fri Feb 17, 2006 8:31 am 
Beginner
Beginner

Joined: Fri May 13, 2005 9:39 am
Posts: 21
If I were you, rather than:
Code:
user.getAddress().setCountry(new Country("US"));

I'd use:
Code:
user.getAddress().setCountry(session.get(Country.class, "US"));


I don't know if it will work, but my experience with hibernate tells me to always (as far as possible) get objects from the database then to work with.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 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.