Please help !
Hibernate Version: 2.1.6
Trying to get going with Spring and Hibernate using DAOs and having a heck of a time. Please help me figure this out.
One project has many project Logs... But as you'll see below, the collection is giving me problems.
Here are my mapping files that describe the relationship..
---------Project.hbm.xml contains:
<set name="projectLogs" table="project_log">
<key column="project_id"/>
<one-to-many class="org.appfuse.model.ProjectLog"/>
</set>
---------ProjectLog.hbm.xml contains:
<many-to-one name="project" column="project_id" class="org.appfuse.model.Project"/>
-------- Core Testing Code
I can create a project_log and add it to an existing project using the following code:
project = (Project)dao.getProject(new Long(1));
ProjectLog projectLog = new ProjectLog();
projectLog.setLog("Changed blah to blah");
// Add new log to the set
Set s = project.getProjectLogs();
s.add(projectLog);
// Save the projectlog and project
pldao.saveProjectLog(projectLog);
dao.saveProject(project);
----
This does pretty much what you'd expect it to do, creates a project_log and sets it's foreign key to project_id=1. However, from this point on, and anytime I rerun the test, as soon as projectBean.getProjects() is called, I will get the following error:
[junit] cannot access loading collection
[junit] net.sf.hibernate.LazyInitializationException: cannot access loading collection
[junit] at net.sf.hibernate.collection.PersistentCollection.initialize(PersistentCollection.java:191)
[junit] at net.sf.hibernate.collection.PersistentCollection.read(PersistentCollection.java:71)
[junit] at net.sf.hibernate.collection.Set.hashCode(Set.java:383)
[junit] at org.apache.commons.lang.builder.HashCodeBuilder.append(HashCodeBuilder.java:392)
[junit] at org.apache.commons.lang.builder.HashCodeBuilder.reflectionAppend(HashCodeBuilder.java:353)
[junit] at org.apache.commons.lang.builder.HashCodeBuilder.reflectionHashCode(HashCodeBuilder.java:327)
[junit] at org.apache.commons.lang.builder.HashCodeBuilder.reflectionHashCode(HashCodeBuilder.java:194)
[junit] at org.appfuse.model.BaseObject.hashCode(BaseObject.java:28)
[junit] at org.apache.commons.lang.builder.HashCodeBuilder.append(HashCodeBuilder.java:392)
[junit] at org.apache.commons.lang.builder.HashCodeBuilder.reflectionAppend(HashCodeBuilder.java:353)
[junit] at org.apache.commons.lang.builder.HashCodeBuilder.reflectionHashCode(HashCodeBuilder.java:327)
[junit] at org.apache.commons.lang.builder.HashCodeBuilder.reflectionHashCode(HashCodeBuilder.java:194)
[junit] at org.appfuse.model.BaseObject.hashCode(BaseObject.java:28)
[junit] at org.appfuse.model.ProjectLog.hashCode(ProjectLog.java:114)
[junit] at java.util.HashMap.hash(HashMap.java:261)
[junit] at java.util.HashMap.put(HashMap.java:379)
[junit] at java.util.HashSet.add(HashSet.java:192)
[junit] at java.util.AbstractCollection.addAll(AbstractCollection.java:319)
[junit] at net.sf.hibernate.collection.Set.endRead(Set.java:244)
[junit] at net.sf.hibernate.impl.SessionImpl.endLoadingCollections(SessionImpl.java:3082)
[junit] at net.sf.hibernate.impl.SessionImpl.endLoadingCollections(SessionImpl.java:3069)
[junit] at net.sf.hibernate.loader.Loader.initializeEntitiesAndCollections(Loader.java:320)
[junit] at net.sf.hibernate.loader.Loader.doQuery(Loader.java:305)
[junit] at net.sf.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:133)
[junit] at net.sf.hibernate.loader.Loader.loadCollection(Loader.java:990)
[junit] at net.sf.hibernate.loader.Loader.loadCollection(Loader.java:965)
[junit] at net.sf.hibernate.loader.OneToManyLoader.initialize(OneToManyLoader.java:93)
[junit] at net.sf.hibernate.collection.AbstractCollectionPersister.initialize(AbstractCollectionPersister.java:284)
[junit] at net.sf.hibernate.impl.SessionImpl.initializeCollection(SessionImpl.java:3268)
[junit] at net.sf.hibernate.collection.PersistentCollection.forceInitialization(PersistentCollection.java:336)
[junit] at net.sf.hibernate.impl.SessionImpl.initializeNonLazyCollections(SessionImpl.java:3123)
[junit] at net.sf.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:138)
[junit] at net.sf.hibernate.loader.Loader.doList(Loader.java:1033)
[junit] at net.sf.hibernate.loader.Loader.list(Loader.java:1024)
[junit] at net.sf.hibernate.hql.QueryTranslator.list(QueryTranslator.java:854)
[junit] at net.sf.hibernate.impl.SessionImpl.find(SessionImpl.java:1544)
[junit] at net.sf.hibernate.impl.QueryImpl.list(QueryImpl.java:39)
[junit] at org.springframework.orm.hibernate.HibernateTemplate$23.doInHibernate(HibernateTemplate.java:501)
[junit] at org.springframework.orm.hibernate.HibernateTemplate.execute(HibernateTemplate.java:243)
[junit] at org.springframework.orm.hibernate.HibernateTemplate.executeFind(HibernateTemplate.java:263)
[junit] at org.springframework.orm.hibernate.HibernateTemplate.find(HibernateTemplate.java:498)
[junit] at org.appfuse.dao.hibernate.ProjectDAOHibernate.getProjects(ProjectDAOHibernate.java:20)
[junit] at org.appfuse.dao.ProjectDAOTest.testGetProjects(ProjectDAOTest.java:86)
[junit] at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
[junit] at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
[junit] at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
---------Project.java contains:
private Set projectLogs = new HashSet();
public Set getProjectLogs() {
return projectLogs;
}
public void addProjectLogs(ProjectLog projectLog) {
getProjectLogs().add(projectLog);
}
public void setProjectLogs(Set projectLogs) {
this.projectLogs = projectLogs;
}
---------ProjectDAO.java contains:
public List getProjects() {
return getHibernateTemplate().find("from Project"); }
----------
Please help me figure out how to fix this problem.. Thanks!
-Gregg
|