hibernateTemplate and reading parent/Child classes into dataModel
I have come unstuck with this goal after successfully using a dataModel when rendering the result from a hibernateTemplate().find on another class where there isn't a many-to-one mapping to another class.
Has anyone used a similar definition like the one below where you need all of ParentClass plus just one property from the child class. In my Use case I need to display the receiptDate alongside all the other values contained in the ParentClass instance.
<hibernate-mapping> <class catalog="myCatalog" name="myCatalog.model.orm.MyClass" table="myTable">
<id name="consignmentId" type="java.lang.Integer">
<column name="myTable_id"/>
<generator class="identity"/>
</id>
<many-to-one class="myCatalog.model.orm.ParentClass" fetch="select" name="pTable">
<column name="pTable_id" not-null="true"/>
</many-to-one>
<property name="receiptDate" type="timestamp">
<column length="19" name="receipt_date" not-null="true"/>
</property> </class> </hibernate-mapping>
The nearest I have got is with: hibernateTemplate().find("select c.parentClass from myClass c");
However when trying to adress the parentClass items e.g: parentClass.depotAddress in the dataTable I get the following error as the parentClass has not been loaded: org.hibernate.LazyInitializationException: could not initialize proxy - no Session
I would appreciate any pointers in the right directio开发者_如何学Cn.
I have just found a solution anyway, by forcing eager loading on the ParentClass via:
<many-to-one class="myCatalog.model.orm.ParentClass" lazy=false fetch="select" name="pTable">
Your question is far from being clear. If what you want to do is load all the MyClass
instances along with their pTable
parent, the query to use is
select m from MyClass m inner join fetch m.pTable
This is of course described in the Hibernate reference documentation.
精彩评论