This ValueListAdapter returns a ValueList of pojos delegating the data access to Hibernate.
Name | Required | Default | Description |
---|---|---|---|
adapterType | N | 0 |
A bitwise or of the following:
|
sessionFactory | Y | N/A | An instance of Hibernate SessionFactory. |
hsql | DEPRECATED | Instead, please use hql | |
hql | Y | One of the two are required. | The Hibernate query is defined in the Spring config file. (image) |
namedQuery |
The name of the Hibernate query that is defined in your mapping file.
(image)
We recommend to not use this, while you want to use features such are focusing and sorting. | ||
defaultNumberPerPage | N | Integer.MAX_VALUE | The number if items per page. This can be overriden by a value in the ValueListInfo. |
defaultSortColumn | N | N/A | The column to sort the results upon. This can be overridden by a value in the ValueListInfo. |
defaultSortDirection | N | N/A | The direction (asc or desc) to sort the results in. This can be overridden by a value in the ValueListInfo. |
allowCreate | N | true | If a new Session should be created if no thread-bound found. |
maxRowsForFocus | N | Long.MAX_VALUE; | The max rows in ResulSet to do focus search. |
defaultFocusPropertyObjectAlias | N | "" | The name of object use to get focus property in hibernate sql syntax SELECT defaultFocusPropertyObjectAlias.focusProperty FROM ... |
removeEmptyStrings | N | false | Enable or Disable String length checking of given filters values. If filter value is null or empty is removed from query. |
validator | N | null | Validator add possibility to do any postprocessing of selected records. |
focusOptimalization | N | true | Enable or Disable optimalization of statement for focus query. |
statementBuilder | N | Implicit object with default String setter for all keys. | Supports Long, Timestamps and other hibernate types to set to "preparedStatement" of hibernate query. It is used with conjuction of filters. |
You will notice the hql in the <hql> tag is not valid hql. The line: /~sortColumn: ORDER BY [sortColumn] [sortDirection]~/ contains chars '/', '~', '[' and ']'; all invalid chars in thier context. Let me explain what is happening here.
You can limit records (objects) shown in a valuelist to
those matching certain criteria thanks to something that is called "filters".
The Hibernate20Adapter uses the values in the ValueListInfo.filters
to
dynamically build the query.
The following rules are applied.
ValueListInfo.filters
map contains the key 'sortColumn'.
ValueListInfo.filters
map. For example:
'ORDER BY [sortColumn] DESC' = 'ORDER BY my_sorted_column DESC'
ValueListInfo.filters
map is set on the PreparedStatement.
For example:
'WHERE user.name = {name}' = 'WHERE user.name = :name'
NOTE: The Hibernate20Adapter set your bind varable as string to PreparedStatement as default.
(It is something like this:Query.setString("name",(String) valueListInfo.getFilters().get("name"));
)
Of course Hibernate20Adapter can set also other types such as Timestamps.
To do that, you need to declare map with a cuple of the name of your key and a type of your key.
The manager for this map is called StatementBuilder.
<property name="statementBuilder"> <bean class="net.mlw.vlh.adapter.hibernate.util.StatementBuilder"> <property name="setters"> <map> <entry key="name"> <bean class="net.mlw.vlh.adapter.hibernate.util.setter.StringSetter" /> </entry> <entry key="myLongId"> <bean class="net.mlw.vlh.adapter.hibernate.util.setter.LongSetter" /> </entry> </map> </property> </bean> </property>
Query.setLong("myLongId",(Long) valueListInfo.getFilters().get("myLongId"));
)
NOTE: StatemntBuilder is used analogical in jdbc based adapters. See war example adapters config file.
If you like to use the focus feature, you must tell Hibernate20Adapter what to focus. You can do it directly to set focusValue, focusProperty. And you have to also enable it every time you want to focus. Focus will be automatically turned off, when you will press next page button in your html page for example.
ValueListInfo info = valueListHelper.getValueListInfo(request, "MyTable"); info.setFocusValue(player.getId()); //We assume that you have object Player with the property 'id'. info.setFocusProperty("id"); info.setDoFocus(true); //do focus when retrieving! valueListHelper.backupInfoFor(request, info, "MyTable"); ...
In this example is disabled sorting for the first column. You can enabled it with sortable="desc".
The next tag that is usefull is focusStatus. It represents what happened during the focus searching. This tag create a table only if the focus is enabled. The focus will be enabled if you set non null value to the focus property (in service or in jsp). For more detail see taglibdoc for focus status tag.
Focusing for other adpaters or in JVM is work in progress. :))
Sometimes is your solution too complex to retrieve needed objects from a persistance tier using hibernate.
In this case you appreciate usage of so called ObjectValidator
s.
Validators help you with filtering of reselt set in JVM and still support paging and other features of this Hibernate20Adapter
.
Before use of validators, you need to:
ObjectValidator
public class PlayersObjectValidator implements ObjectValidator { /** * Logger for this class */ private static final Log LOGGER = LogFactory .getLog(PlayersObjectValidator.class); /** * @see net.mlw.vlh.adapter.util.ObjectValidator#isAcceptable(java.lang.Object) */ public boolean isAcceptable(Object objectToBeChecked) { Player p = (Player) objectToBeChecked; boolean accepted; if ( p.getLastName()!=null && p.getLastName().equalsIgnoreCase("Williams") ){ LOGGER.info("Accepted player : " + p); accepted = true; } else{ LOGGER.debug("Rejected player : " + p); accepted = false; } return accepted; } /** * @see net.mlw.vlh.adapter.util.ObjectValidator#setValueListInfo(net.mlw.vlh.ValueListInfo) */ public void setValueListInfo(ValueListInfo info) { // This method is called by adapter! // In this example is it not used, you can use it as additional way how // to pass some additional parameters to method isAcceptable. } }
PlayersObjectValidator
and set it to property validator
of Hibernate20Adapter
.
<bean id="Players1Validator" class="net.mlw.data.PlayersObjectValidator"/> ... <bean id="valueListHandler" singleton="true" class="net.mlw.vlh.DefaultValueListHandlerImpl"> <property name="config.adapters"> <map> ... <entry key="players1Validator"> <bean class="net.mlw.vlh.adapter.hibernate.Hibernate20Adapter"> <property name="validator"><ref bean="Players1Validator"/></property> <property name="focusOptimalization"><value>false</value></property> <!-- the rest --> <property name="sessionFactory"><ref bean="mySessionFactory"/></property> <property name="defaultNumberPerPage"><value>2</value></property> <property name="defaultSortColumn"><value>lastName</value></property> <property name="defaultSortDirection"><value>asc</value></property> <property name="hql"> <value> FROM net.mlw.data.Player AS vo /~name: WHERE vo.lastName LIKE {name} ~/ /~sortColumn: ORDER BY vo.[sortColumn] [sortDirection]~/ </value> </property> <property name="defaultFocusPropertyObjectAlias"><value>vo</value></property> <property name="maxRowsForFocus"><value>160000</value></property> <property name="removeEmptyStrings"><value>true</value></property> </bean> </entry> ... </map> </bean>
focusOptimalization
in Hibernate20Adapter
to false.
For more information see war file example (version 0-1-7Beta9 or higher).