I'm using Hibernate 2.1.1.
As you can see below,
mpReqItems is a "set" of
MpRequisicaoSq044. So with the registers shown below the returned list must have one object of
MpRequisicaoSq044 and two in it's
mpReqItems "set". But that's not happening! It's returning two objects of
MpRequisicaoSq044 each one with two in it's
mpReqItems "set". Is my method code correct?
Please help.
I need to understand this. It's fundamental for Hibernate knowledge.
The testing code is returning me (which I don't want):
Code:
SIZE: 2
----- MP_REQUISICAO -----
ID: 1 + DATA: 2003-12-14
----- MP_REQ_ITEM -----
ID: 1 + QTD: 220
ID: 1 + QTD: 110
------------------------------------
----- MP_REQUISICAO -----
ID: 1 + DATA: 2003-12-14
----- MP_REQ_ITEM -----
ID: 1 + QTD: 220
ID: 1 + QTD: 110
------------------------------------
instead of returning:
Code:
SIZE: 1
----- MP_REQUISICAO -----
ID: 1 + DATA: 2003-12-14
----- MP_REQ_ITEM -----
ID: 1 + QTD: 220
ID: 1 + QTD: 110
------------------------------------
I have two tables:
MP_REQUISICAO_SQ044 and
MP_REQ_ITEM.
In my DB I have:
MP_REQUISICAO_SQ044 (1 register)
Code:
id: 1
data_requisicao: 2003-12-14
MP_REQ_ITEM (2 registers)
Code:
mp_requisicao_fk: 1 (FK to above table)
mp_fk: 1
quantidade: 220
mp_requisicao_fk: 1 (FK to above table)
mp_fk: 2
quantidade: 110
---------------------
My mapping files
---------------------
MpRequisicaoSq044Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 2.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd" >
<hibernate-mapping>
<!--
Created by Middlegen Hibernate plugin
http://boss.bekk.no/boss/middlegen/
http://hibernate.sourceforge.net/
-->
<class
name="vo.MpRequisicaoSq044"
table="mp_requisicao_sq044"
>
<id
name="id"
type="long"
column="id"
>
<generator class="increment" />
</id>
<property
name="dataRequisicao"
type="java.sql.Date"
column="data_requisicao"
not-null="true"
length="4"
/>
<!-- associations -->
<!-- bi-directional one-to-many association to MpReqItem -->
<set
name="mpReqItems"
lazy="true"
inverse="true"
>
<key>
<column name="mp_requisicao_fk" />
</key>
<one-to-many
class="vo.MpReqItem"
/>
</set>
</class>
</hibernate-mapping>
MpReqItemCode:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 2.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd" >
<hibernate-mapping>
<!--
Created by Middlegen Hibernate plugin
http://boss.bekk.no/boss/middlegen/
http://hibernate.sourceforge.net/
-->
<class
name="vo.MpReqItem"
table="mp_req_item"
>
<composite-id name="comp_id" class="pt.comseal.arsol.vo.MpReqItemPK">
<!-- bi-directional many-to-one association to MpRequisicaoSq044 -->
<key-many-to-one
name="mpRequisicaoSq044"
class="vo.MpRequisicaoSq044"
>
<column name="mp_requisicao_fk" />
</key-many-to-one>
<!-- bi-directional many-to-one association to Mp -->
<key-many-to-one
name="mp"
class="vo.Mp"
>
<column name="mp_fk" />
</key-many-to-one>
</composite-id>
<property
name="quantidade"
type="int"
column="quantidade"
length="4"
/>
<!-- associations -->
</class>
</hibernate-mapping>
----------------
Method code
----------------
Here is my code to retrieve a list of
MpRequisicaoSq044 with the
mpReqItems set eagerly loaded:
Code:
public static List consultarMpRequisicao(Date data_ini, Date data_fim) throws MpBOException, BadFactoryException {
Session session = null;
List res = null;
try {
SessionFactory sessionFactory = HibernateFactory.createFactory();
session = sessionFactory.openSession();
if(data_ini != null && data_fim == null) {
res = session.find(
"from vo.MpRequisicaoSq044 mr " +
"left join fetch mr.mpReqItems where mr.dataRequisicao >= ? order by mr.dataRequisicao",
data_ini,
Hibernate.DATE
);
} else if (data_ini == null && data_fim != null) {
res = session.find(
"from vo.MpRequisicaoSq044 mr " +
"left join fetch mr.mpReqItems where mr.dataRequisicao <= ? order by mr.dataRequisicao",
data_fim,
Hibernate.DATE
);
} else if (data_ini != null && data_fim != null) {
res = session.find(
"from vo.MpRequisicaoSq044 mr " +
"left join fetch mr.mpReqItems where mr.dataRequisicao >= ? and mr.dataRequisicao <= ? order by mr.dataRequisicao",
new Object[] {data_ini, data_fim},
new Type[] {Hibernate.DATE, Hibernate.DATE}
);
} else {
res = session.find(
"from vo.MpRequisicaoSq044 mr " +
"left join fetch mr.mpReqItems order by mr.dataRequisicao"
);
}
session.close();
}
catch(HibernateException e) {
throw new MpBOException("Error.");
}
catch(BadFactoryException e) {
throw new BadFactoryException(e.getMessage());
}
finally {
try {
if(session != null) session.close();
}
catch(HibernateException e) {throw new MpBOException("Error.");}
}
return(res);
}
------------------
Testing code
------------------
Code:
try {
List res = MpBO.consultarMpRequisicao(Date.valueOf("2003-12-12"), Date.valueOf("2003-12-14"));
System.out.println("SIZE: " + res.size());
Iterator it = res.iterator();
while(it.hasNext()) {
MpRequisicaoSq044 mp_req = (MpRequisicaoSq044)it.next();
System.out.println("----- MP_REQUISICAO -----");
System.out.println("ID: " + mp_req.getId() + " + DATA: " + mp_req.getDataRequisicao());
Iterator it1 = mp_req.getMpReqItems().iterator();
System.out.println("----- MP_REQ_ITEM -----");
while(it1.hasNext()) {
MpReqItem mp_req_item = new MpReqItem();
mp_req_item = (MpReqItem)it1.next();
System.out.println("ID: "+mp_req_item.getComp_id().getMpRequisicaoSq044().getId()+
" + QTD: "+mp_req_item.getQuantidade());
}
System.out.println("------------------------------------");
}
}
catch(MpBOException e) {
System.out.println(e.getMessage());
}
catch(BadFactoryException e) {
System.out.println(e.getMessage());
}