-->
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: Bidirectional: How to Insert Child of existing Parent?
PostPosted: Wed Jun 06, 2012 10:51 am 
Newbie

Joined: Wed Jun 06, 2012 9:27 am
Posts: 1
Hello, *(see question in title)

I have a small application which consists of two tables, locations and poeple.
They are mapped in database through the postal code of the locations.
Location is inverse=true and cascade=all.

Normally I woul create a new people and create a new location, add the people to the locations set and set the peoples "Location loc" to the new location. When executing "session.save(theNewLocation)" both the location and the people were inserted correctly into my database.

I come into trouble when I try to create a new people which lives in a still existing location.
For this I single-select a Location from my database, create a new people, add the new people to the exisiting location and set the peoples location to the existing location.
When now calling session.save(theExistingLocation) (the same with saveorupdate() and update()) it crahes when trying to exxecute
Code:
location_peopleSet.add(people);

the error:
Code:
failed to lazily initialize a collection of role: dbTables.locationClass.peopleSet, no session or session was closed

My fault, I closed the session, returned the selected location and tried to add a people... bam!
Now, while session remains open I am doing the same logic, just adding a new people to my exisiting location, session is not empty now.... bam!
Code:
org.hibernate.exception.SQLGrammarException: Invalid column name 'T_Location_PostalCode'.

This does not happen when having a new location and a new people. Why now does it not find my column now??

Thanks for any advice!

some code snippets:
Code:
@SuppressWarnings("unchecked")
   public void saveObject(Start startClass, SessionFactory sessionFactory) throws Exception
   {
      
      Session session = sessionFactory.openSession();
      Transaction tx = null;
      List<LocationClass> existingLocation = null;
      LocationClass locObj = null;
      
      try{
         tx = session.beginTransaction();
         Query query = null;
         
         query = session.createQuery("from dbTables.LocationClass where T_Location_PostalCode = '" + this.postalCode + "'");
         existingLocation =(List<LocationClass>) query.list();
         tx.commit();
         
         if(existingLocation.size() > 1)
         {
            throw new Exception("Database Locations inconsistent!");
         }
         else if(existingLocation.size() == 0)
         {
            locObj = new LocationClass(this.postalCode, this.locationName);
         }
         else
         {
            locObj = existingLocation.get(0);
         }
         
         PeopleClass peopObj = new PeopleClass(this.peopleName);
         locObj.addPeoplle(peopObj);
         
         session.saveOrUpdate(locObj);
         
      }catch(Exception ex){
         ex.printStackTrace();
      }finally{session.close();}
   }


the "locObj.addPeoplle(peopObj);" method
Code:
public void addPeople(PeopleClass people)
   {
      this.peopleSet.add(people);
      people.setLocation(this);
   }


peoples hbm.xml:
Code:
<many-to-one name="location" class="dbTables.LocationClass" column="T_People_PostalCode" not-null="true"/>


locations hbm.xml:
Code:
<set
         name="peopleSet" cascade="all" inverse="true">
         <key column="T_Location_PostalCode"></key>
         <one-to-many class="dbTables.PeopleClass" />
      </set>



edit:
may I have another fault in my hbm.xml:
when changin locations hbm.xml "<key column="T_Location_PostalCode"></key>" to "<key column="T_People_PostalCode"></key>" no error occurs.
But also no insert nor any update takes place. The logic only does some select.
Just confused...


Top
 Profile  
 
 Post subject: Re: Bidirectional: How to Insert Child of existing Parent?
PostPosted: Wed Jun 06, 2012 1:03 pm 
Beginner
Beginner

Joined: Mon Jun 04, 2012 12:31 pm
Posts: 20
The key column on the Set has to exist on the one-to-many class that you specified. That's why changing it to T_People_PostalCode eliminated the error. However, on your query you can't refer directly to that column. You have to refer to the properties on the entity.

Code:
query = session.createQuery("from dbTables.LocationClass where postalCode = '" + this.postalCode + "'");


You also can't use saveOrUpdate for every case you've given. If no locations are returned and you construct a new LocationClass. You need to call insert on that instance, not saveOrUpdate.

Also

Code:
public void addPeople(PeopleClass people)
   {
      this.peopleSet.add(people);
      people.setLocation(this);  <-- you don't need this line here.  Hibernate handles that relationship for you when you add it to the Set.
   }


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.