Query By Example not working consistently (for me!).
I am reverse engineering from DB schema. The first code-excerpt works but the second one doesn't work as I would expect.
EXCERPT#1: Works
Code:
acc = new TAccount();
acc.setDeletedFg('N'); // this is a char in the generated DTO
Criteria crit = HibernateUtil.getSessionFactory().getCurrentSession().createCriteria(TAccount.class);
Example example = Example.create(acc);
String[] excludeProperty = new String[3];
//each of these 3 entries are chars in the generated DTO
excludeProperty[0] = "mtmEnabledFg";
excludeProperty[1] = "autoCloseOutFg";
excludeProperty[2] = "tradingEnableFg";
for (String exclude : excludeProperty) {
example.excludeProperty(exclude);
}
crit.add(example);
This successfully fetches the correct number of rows. The generated SQL is
Quote:
Hibernate:
select
this_.ACCOUNT_ID as ACCOUNT1_0_0_,
this_.MTM_ENABLED_FG as MTM2_0_0_,
this_.DELETED_FG as DELETED3_0_0_,
this_.CREATED_DTS as CREATED4_0_0_,
this_.LAST_UPDATED_DTS as LAST5_0_0_,
this_.AUTO_CLOSE_OUT_FG as AUTO6_0_0_,
this_.STAMM as STAMM0_0_,
this_.ACCOUNT_NM_TXT as ACCOUNT8_0_0_,
this_.TRADING_ENABLE_FG as TRADING9_0_0_,
this_.STAMP as STAMP0_0_
from
T_ACCOUNT this_
where
(
this_.DELETED_FG=?
)
EXCERPT#2: Doesn't work as I'd expectTable TMtsCcyCategory has a composite primark key clas TMtsCcyCategoryId made up of SCHEME_ID and CCY_CD.
Code:
MtsCcyCategoryDAO mtsCcyCategoryDAO = DaoFactory.getInstance().getMtsCcyCategoryDAO();
TMtsCcyCategory cc = new TMtsCcyCategory();
TMtsCcyCategoryId id = new TMtsCcyCategoryId();
id.setSchemeId(new Long(2));
cc.setId(id);
Criteria crit = HibernateUtil.getSessionFactory().getCurrentSession().createCriteria(TMtsCcyCategory.class);
Example example = Example.create(cc);
String[] excludeProperty = new String[4];
excludeProperty[0] = "ccyCd";
excludeProperty[0] = "standardPct";
for (String exclude : excludeProperty) {
example.excludeProperty(exclude);
}
crit.add(example);
System.out.println("the size =" + crit.list().size());
}
The generated SQL is
Quote:
Hibernate:
select
this_.SCHEME_ID as SCHEME1_38_0_,
this_.CCY_CD as CCY2_38_0_,
this_.STANDARD_PCT as STANDARD3_38_0_,
this_.HOLIDAY_PCT as HOLIDAY4_38_0_,
this_.STAMP as STAMP38_0_
from
T_MTS_CCY_CATEGORY this_
where
(
1=1
)
I can't figure out why this query by example doesn't fetch TMtsCcyCategory for which the TMtsCcyCategoryId SCHEME_ID is equal to 2.
Do you know why?
BTW, I looked at using net.sf.expression.Expression but to be honest I don't really know why there is a seperate packaging structure (over and abobe the org.hibernate one).
Hibernate version: 3.1.2
Name and version of the database you are using: Oracle 10g