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
|