Hi all,
I am having a problem reading a collection .. i have three simple tables..
Country, Province and City.. obviously province has a many-to-one with Country and like wise with City and Province..
When i check my log I see that the countries are read and so are the provinces.. when it goes to read the cities for each province i get the error..
Hibernate version:
3.05
Mapping documents:
Country.hbm.xml
Code:
<class name="Country" table="country">
<id name="id" column="id" type="java.lang.Integer">
<generator class="native"/>
</id>
<property name="name" column="name" type="java.lang.String" not-null="true" />
<list name="provinces" inverse="true" >
<key column="countryId" not-null="true" />
<index column="id" type="java.lang.Integer" />
<one-to-many class="Province" />
</list>
</class>
Province.hbm.xml
Code:
<class name="Province" table="province">
<id name="id" column="id" type="java.lang.Integer">
<generator class="native"/>
</id>
<property name="name" column="name" type="java.lang.String" not-null="true" />
<many-to-one name="countryid" column="countryId" not-null="true" class="Country"/>
<list name="cities" lazy="true" inverse="true" >
<key column="provinceId" not-null="true" />
<index column="id" type="java.lang.Integer" />
<one-to-many class="City" />
</list>
</class>
and City.hbm.xml
Code:
<class name="City" table="city">
<id name="id" column="id" type="java.lang.Integer">
<generator class="native"/>
</id>
<property name="name" column="name" type="java.lang.String" not-null="true" />
<many-to-one name="provinceid" column="provinceId" class="Province" not-null="true"/>
<list name="users" lazy="true" inverse="true" >
<key column="cityId" not-null="true" />
<index column="id" type="java.lang.Integer" />
<one-to-many class="Users" />
</list>
</class>
These mapping files look good to me.. what do you guys think?
Code between sessionFactory.openSession() and session.close():Code:
logger.info("Retrieving Collection of Provinces for this Country");
if (countryId == null)
return new ArrayList();
Collection provinces = null;
try
{
Country c = (Country)HibernateSessionFactory
.currentSession()
.load(Country.class, countryId);
logger.info("Country loaded: " + c.getName());
logger.info("Retrieve Provinces for Country " + c.getName());
provinces = c.getProvinces();
if (provinces != null)
provinceId = ((Province)provinces.toArray()[0]).getId();
else
provinces = new ArrayList();
}
catch (Exception e)
{
logger.info("Error retrieving provinces: " + e.toString());
}
HibernateSessionFactory.closeSession();
return provinces;
Full stack trace of any exception that occurs:Code:
DEBUG http-8080-Processor25 org.hibernate.loader.Loader - total objects hydrated: 13
DEBUG http-8080-Processor25 org.hibernate.engine.TwoPhaseLoad - resolving associations for [com.insaini.publogs.hibernate.tables.Province#1]
DEBUG http-8080-Processor25 org.hibernate.event.def.DefaultLoadEventListener - loading entity: [com.insaini.publogs.hibernate.tables.Country#1]
DEBUG http-8080-Processor25 org.hibernate.event.def.DefaultLoadEventListener - entity proxy found in session cache
DEBUG http-8080-Processor25 org.hibernate.engine.CollectionLoadContext - creating collection wrapper:[com.insaini.publogs.hibernate.tables.Province.cities#1]
INFO http-8080-Processor25 com.insaini.publogs.business.LocationBean - Error retrieving provinces: org.hibernate.PropertyAccessException: exception setting property value with CGLIB (set hibernate.cglib.use_reflection_optimizer=false for more info) setter of com.insaini.publogs.hibernate.tables.Province.setCountryid
DEBUG http-8080-Processor25 org.hibernate.impl.SessionImpl - closing session
DEBUG http-8080-Processor25 org.hibernate.jdbc.ConnectionManager - closing JDBC connection [ (open PreparedStatements: 0, globally: 0) (open ResultSets: 0, globally: 0)]
DEBUG http-8080-Processor25 org.hibernate.jdbc.JDBCContext - after transaction completion
DEBUG http-8080-Processor25 org.hibernate.impl.SessionImpl - after transaction completion
ERROR http-8080-Processor25 org.hibernate.LazyInitializationException - illegal access to loading collection
org.hibernate.LazyInitializationException: illegal access to loading collection
at org.hibernate.collection.AbstractPersistentCollection.initialize(AbstractPersistentCollection.java:172)
at org.hibernate.collection.AbstractPersistentCollection.read(AbstractPersistentCollection.java:48)
at org.hibernate.collection.PersistentList.iterator(PersistentList.java:106)
at org.apache.struts.taglib.html.OptionsCollectionTag.getIterator(OptionsCollectionTag.java:337)
at org.apache.struts.taglib.html.OptionsCollectionTag.doStartTag(OptionsCollectionTag.java:183)
at org.apache.jsp.jsp.signUp_jsp._jspx_meth_html_optionsCollection_1(org.apache.jsp.jsp.signUp_jsp:1108)
at org.apache.jsp.jsp.signUp_jsp._jspx_meth_html_select_1(org.apache.jsp.jsp.signUp_jsp:1044)
at org.apache.jsp.jsp.signUp_jsp._jspService(org.apache.jsp.jsp.signUp_jsp:459)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:97)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:322)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:314)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:264)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:252)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:213)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:178)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:126)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:105)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:107)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:148)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:868)
at org.apache.coyote.http11.Http11BaseProtocol$Http11ConnectionHandler.processConnection(Http11BaseProtocol.java:663)
at org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpoint.java:527)
at org.apache.tomcat.util.net.LeaderFollowerWorkerThread.runIt(LeaderFollowerWorkerThread.java:80)
at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:684)
at java.lang.Thread.run(Thread.java:595)
Name and version of the database you are using:MySQL 5.0.15
The generated SQL (show_sql=true):Code:
Hibernate: select this_.id as id0_, this_.name as name2_0_ from country this_
Hibernate: select country0_.id as id0_, country0_.name as name2_0_ from country country0_ where country0_.id=?
Hibernate: select provinces0_.countryId as countryId1_, provinces0_.id as id1_, provinces0_.id as id0_, provinces0_.name as name8_0_, provinces0_.countryId as countryId8_0_ from province provinces0_ where provinces0_.countryId=?
Debug level Hibernate log excerpt:As you can see from this line
Code:
DEBUG http-8080-Processor25 org.hibernate.loader.Loader - total objects hydrated: 13
in the exceptions that 13 provinces were loaded for the country Canada. Then you see the error when it tries to read the cities..
any ideas?
thanks
jaz