I have 4 tables
- Titles having the following fields, id (PK), Name, AuthorId(FK) & PublishedId(FK)
- Author having id(FK), Author Name & Contact Details
- Publisher having, id(PK), Publisher Name & RegionId(FK)
- Region having, id(PK), Region Name, Published Date & Published Cost.
Now in my View, I am accepting
-
Author Name, and my code looks like
SearchTemplate _tempTemplate = new SearchTemplate();
Criterion criterion = null;if (rcValue.getOperator().equalsIgnoreCase("like")) { criterion = Restrictions.like(rcValue.getDisplayName(), rcValue.getOperandValue()); } else if (rcValue.getOperator().equalsIgnoreCase("is")) { criterion = Restrictions.eq(rcValue.getDisplayName(), rcValue.getOperandValue()); } else if (rcValue.getOperator().equalsIgnoreCase("not")) { criterion = Restrictions.ne(rcValue.getDisplayName(), rcValue.getOperandValue()); } _tempTemplate.addCriterion(criterion);
When I search for Name, I get the right results, but when I search for my Author Name, what do I need to change in my above code?
2.When I search of Published Date, what do I need to change, because its a multi-table search, if its SQL I can easily write it, but using Hibernate now and with an implementation of ‘SearchTemplate’
/*The SearchTemplate is used to pass search parameters to the DAO layer.
*
* By having all the search parameters in one place (the SearchTemplate),
* we prevents combinatory explosion of 'find' method signatures in the DAO/Service layer.
* This leads to more stable DAO/Service interfaces as you can add search parameters to the
* SearchTemplate without adding new methods to your interfaces.
*
* A SearchTemplate helps you drive your search in the following areas:
*/
Can someone help with this?
First question:
You need to make a join (assuming the root entity is title):
Second question:
same answer: you need to make joins:
This SearchTemplate is a really bad idea, IMO. Instead of having well-defined methods with a clear contract like :
You’ll have a single method
What must you put in the criteria? What’s the type of the criteria? What will the method do with these criteria? Which ordering will be applied? Which related entities will be fetched or lazily loaded?
An “explosion” of find methods is a good thing, because each find method has its own contract, optimizations, and goal. Each can also be unit-tested. That’s the goal of a DAO. Without these methods, you might as well have a unique method such as
But I fail to see the goal of such a DAO.