-->
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.  [ 5 posts ] 
Author Message
 Post subject: one-to-many problem and LazyInitializationException
PostPosted: Wed Mar 24, 2004 11:11 pm 
Newbie

Joined: Wed Mar 24, 2004 10:34 pm
Posts: 4
Hi,

I am new to hibernate and i think i'm doing some stuff completely wrong. I'd appreciate if could tell what i am doing wrong.

I'm using hibernate 2.1, mysql 4.

I have two tables in my database:

Domain(domainID,domainPath)
Policy(policyID,policyName,subjectDomain ,secondDomain)

Every policy can have one subjectDomain and one targetDomain only
a domain can be either a subjectDomain or targetDomain in many policies.

I created the mapping files all that like:

Policy Mapping
----------------
<hibernate-mapping package="pas.dao">
<class name="Policy" table="policy">
<id
name="policyID"
type="java.lang.Integer"
column="policyID"
>
<generator class="vm"/>
</id>

<property
name="policyName"
column="policyName"
type="java.lang.String"
not-null="true"
length="100"
/>
<property
name="status"
column="status"
type="java.lang.Integer"
not-null="true"
length="11"
/>
<many-to-one name="policySubjectDomain" class="pas.dao.Domain" column="subjectDomain" />
<many-to-one name="policyTargetDomain" class="pas.dao.Domain" column="targetDomain"/>
</class>
</hibernate-mapping>

Domain Mapping:
-------------------
<hibernate-mapping package="pas.dao">
<class name="Domain" table="domain">
<id
name="domainID"
type="java.lang.Integer"
column="domainID"
>
<generator class="vm"/>
</id>

<property
name="domainPath"
column="domainPath"
type="java.lang.String"
not-null="true"
length="100"
/>
<set name="subjectPolicy" cascade="all" inverse="true" lazy="true">
<key column="domainID"/>
<one-to-many class="pas.dao.Policy"/>
</set>
<set name="targetPolicy" cascade="all" inverse="true" lazy="true">
<key column="domainID"/>
<one-to-many class="pas.dao.Policy"/>
</set>
</class>
</hibernate-mapping>


Quering for Policy object(Working fine):
-----------------
I want to find a Policy by policy name and return a Policy object which contains the subjectDomain 'Domain' and the targetDomain 'Domain' objects. Iused the query :

String query="from Policy as policy where policy.policyName=?";

And i got what I expected


Quering for Domain object(NOT Working):
----------------------------------------------
When I try to query for a Domain object -by domainPath attribute, I get a Domain object with the corect domainPath.

The problem occures when I try to access the collection of Policies in which this Domain is a subjectDomain, and the collection of Policies in which this Domain is a targetDomain.

I'm using the following query:
"from Domain as domain where domain.domainPath=?";

I tried different left join but no success :-(
DomainInstance.getDomainPath() works fine

but

Set sub = DomainInstance.getSubjectPolicy(); //Does NOT WORK

I've spent ages on it and i'll really appreciate some help.

This is the generated sql
-----------------------------
Hibernate: select domain0_.domainID as domainID, domain0_.domainPath as domainPath from domain domain0_ where (domain0_.domainPath=? )
/testing-target

Hibernate: select subjectpol0_.policyID as policyID__, subjectpol0_.domainID as domainID__, domain1_.domainID as domainID0_, domain1_.domainPath as domainPath0_, domain2_.domainID as domainID1_, domain2_.domainPath as domainPath1_, subjectpol0_.policyID as policyID2_, subjectpol0_.policyName as policyName2_, subjectpol0_.status as status2_, subjectpol0_.subjectDomain as subjectD4_2_, subjectpol0_.targetDomain as targetDo5_2_ from policy subjectpol0_ left outer join domain domain1_ on subjectpol0_.subjectDomain=domain1_.domainID left outer join domain domain2_ on subjectpol0_.targetDomain=domain2_.domainID where subjectpol0_.domainID=?

The Exception
-----------------
-----------------
net.sf.hibernate.LazyInitializationException: Failed to lazily initialize a collection
at net.sf.hibernate.collection.PersistentCollection.initialize(PersistentCollection.java:206)
at net.sf.hibernate.collection.PersistentCollection.read(PersistentCollection.java:71)
at net.sf.hibernate.collection.Set.iterator(Set.java:130)
at pas.PasTest.main(PasTest.java:39)
Caused by: net.sf.hibernate.JDBCException: could not initialize collection: [pas.dao.Domain.subjectPolicy#4]
at net.sf.hibernate.collection.AbstractCollectionPersister.initialize(AbstractCollectionPersister.java:287)
at net.sf.hibernate.impl.SessionImpl.initializeCollection(SessionImpl.java:3226)
at net.sf.hibernate.collection.PersistentCollection.initialize(PersistentCollection.java:200)
... 3 more
Caused by: java.sql.SQLException: Column not found: Unknown column 'subjectpol0_.domainID' in 'field list'
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:800)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:1159)
at com.mysql.jdbc.Connection.execSQL(Connection.java:1897)
at com.mysql.jdbc.PreparedStatement.executeQuery(PreparedStatement.java:1497)
at net.sf.hibernate.impl.BatcherImpl.getResultSet(BatcherImpl.java:87)
at net.sf.hibernate.loader.Loader.getResultSet(Loader.java:795)
at net.sf.hibernate.loader.Loader.doQuery(Loader.java:189)
at net.sf.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:133)
at net.sf.hibernate.loader.Loader.loadCollection(Loader.java:910)
at net.sf.hibernate.loader.Loader.loadCollection(Loader.java:885)
at net.sf.hibernate.loader.OneToManyLoader.initialize(OneToManyLoader.java:80)
at net.sf.hibernate.collection.AbstractCollectionPersister.initialize(AbstractCollectionPersister.java:284)
... 5 more


Top
 Profile  
 
 Post subject: look below
PostPosted: Wed Mar 24, 2004 11:12 pm 
Beginner
Beginner

Joined: Mon Mar 22, 2004 9:37 am
Posts: 22
Location: Willow Grove, PA
java.sql.SQLException: Column not found: Unknown column 'subjectpol0_.domainID' in 'field list'


Top
 Profile  
 
 Post subject: still unclear
PostPosted: Wed Mar 24, 2004 11:39 pm 
Newbie

Joined: Wed Mar 24, 2004 10:34 pm
Posts: 4
Thanks for your quick respond.

I noticed this exception, but it made me even more confused. I don't have any field of that name in the database or in the mapping and i didn't use it in any the queries! . I thought this is caused by a wrong query or something like that. Could you please explain your answer a bit further?.

Many Thanks


Top
 Profile  
 
 Post subject: still unclear
PostPosted: Wed Mar 24, 2004 11:40 pm 
Newbie

Joined: Wed Mar 24, 2004 10:34 pm
Posts: 4
Thanks for your quick respond.

I noticed this exception, but it made me even more confused. I don't have any field of that name in the database or in the mapping and i didn't use it in any the queries! . I thought this is caused by a wrong query or something like that. Could you please explain your answer a bit further?.

Many Thanks


Top
 Profile  
 
 Post subject: Found the problem
PostPosted: Thu Mar 25, 2004 12:33 am 
Newbie

Joined: Wed Mar 24, 2004 10:34 pm
Posts: 4
I made a mistake in the mapping for Domain.

it should be

<key column="subjectDomain"/>

and NOT

<key column="domainID"/>

Thanks a lot for your help


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