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...