Hi,
I'm new to hibernate so please bear with me.
My question is not exception/bug related but rather feature/functionality related.
Specifically, I want to leverage the power of the QBE capabilities inherent within Hibernate...and yet not return the full collections of objects, but rather only a simple collection of Strings. Almost a highbred of Criteria/QBE and direct SQL. (Is there a possiblity of using hSql and QBE to achieve this? For example to be able to obtain the WHERE clause generated via the QBE functionality but to isolate the SELECT to only return a single column of type String -- and ideally as a List or Set)
I'm sure there's an answer that I simply havent' stumbled upon.
I have gone through the posted FAQs and tried to read through the documention and other reference materials, but nothing as of yet has explicitly defined what I'm looking to do.
Does anyone have any suggestions? Below is sample code that I'me currently using that works exactly as desired. However, I want to reduce the overhead and only need the collection of company IDs.
(Note: I have not investigated Hibernate 3.0 since we are currently using an existing deployment using 2.1.6 and migrating isn't an option at this point. So if 3.0 addresses this issue via a new feature set I may not be aware of it. Sorry in advance if this be the case).
Thanks,
Regards,
Todd
Hibernate version: 2.1.6
Mapping documents: Not Applicable In this Context
Name and version of the database you are using: MySQL
The Following code is the framework we'ld like the change into:
Code:
...
...
Company company = (Company) object;
Address address = (Address) company.getAddressSet().iterator().next();
Contact contact = (Contact) company.getContactSet().iterator().next();
Mailcode mailCode = (Mailcode) company.getMailCode();
State state = (State) address.getState();
Country country = (Country) address.getCountry();
//
Example companyExample = Example.create(company);
companyExample.excludeZeroes();
companyExample.ignoreCase();
companyExample.enableLike();
//
Example mailCodeExample = null;
if (mailCode != null) {
mailCodeExample = Example.create(mailCode);
mailCodeExample.ignoreCase();
mailCodeExample.enableLike();
}
//
Example addressExample = Example.create(address);
addressExample.excludeZeroes();
addressExample.ignoreCase();
addressExample.enableLike();
//
Example contactExample = Example.create(contact);
contactExample.excludeZeroes();
contactExample.ignoreCase();
contactExample.enableLike();
//
Example stateExample = null;
if (state != null) {
stateExample = Example.create(state);
stateExample.ignoreCase();
}
//
Example countryExample = null;
if (country != null) {
countryExample = Example.create(country);
countryExample.ignoreCase();
}
//
Criteria criteria = session.createCriteria(Company.class);
criteria.add(companyExample);
if (mailCodeExample != null) {
criteria.createCriteria("mailCode").add(mailCodeExample);
}
Criteria addressCriteria = criteria.createCriteria("addressSet").add(addressExample);
if (stateExample != null) {
addressCriteria.createCriteria("state").add(stateExample);
}
if (countryExample != null) {
addressCriteria.createCriteria("country").add(countryExample);
}
criteria.createCriteria("contactSet").add(contactExample);
criteria.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
criteria.addOrder(Order.asc("id"));
//
List list = criteria.list();
if (list != null) {
/*
* Temporary hack until we sort how how to use hibernate to just
* retrieve a set of strings...
*/
LinkedList ids = new LinkedList();
ListIterator listIterator = list.listIterator();
while (listIterator.hasNext()) {
Company nextCompany = (Company) listIterator.next();
// We simply want a lightweight set for the cust id...
ids.addLast(nextCompany.getId()); // Just a java.lang.String
}
//
idResultSet = (QBEResultSet) new QBEResultSetImpl(ids);
}
...
...
return idResultSet;