Overview

Converters do the amazing job of take an input value in UIInput component and then return an specific object to be used in you back beans, actually we have many kinds of converters, we are introducing now a new kind of converter that can takes an input value from some UIInput components and the call a generic persistence layer stuff to return the desired entity instance.

Entity Converter using EntityLoader

This approach lets the user create you own entity loader stuff that load the entity using the persistence layer of your preference. The only thing that you need to configure in your application is your custom EntityLoader interface implementation, to do this, you must add the following web.xml context param:

<context-param>
    <param-name>br.eti.faces.entityLoader</param-name>
    <param-value>app.util.EntityLoader</param-value>
</context-param>

and the configuration below on facelets tag definition:

<tag>
    <tag-name>entityConverter</tag-name>
    <converter>
        <converter-id>br.eti.faces.morefaces.component.entityconverter.EntityConverter</converter-id>
    </converter>
</tag>  

and your app.util.EntityLoader can be somethinkg like this:

package app.util;
import java.io.Serializable;

import org.hibernate.Session;


public class EntityLoader implements br.eti.faces.entityconverter.EntityLoader {

        public Object getEntity(Class type, Serializable id) {
                
                Session session = HibernateUtil.sessaoAtual();
                Object object = sessao.get(type, id);
                return object;
        }
}

and then you can use in your pages in this way:

<h:selectOneMenu value="#{bean.selectedEntity}" 
converter="#{entityConverter.entity['package.Entity;id']}">
   <t:selectItems value="#{bean.entities}"
      var="entity" itemLabel="#{entity.name}"
      itemValue="#{entity}" />
</h:selectOneMenu>

or

<h:selectOneMenu value=“#{bean.selectedEntity}”>
   <fbr:entityConverter entity="package.Entity"
      idProperty="id"/>
   <t:selectItems value="#{bean.entities}"
      var="entity" itemLabel="#{entity.nome}"
      itemValue="#{entity}" />
</h:selectOneMenu>

reading the submited value in your bean:

Entity entity = selectedEntity; 

right now it's only tested with UISelectOne, but should works with other UIInputs except UISelectMany.

You can download the binaries here:

http://faces.eti.br/wp-content/uploads/2006/07/entityconverter.jar

Entity_Converters (last edited 2009-09-20 23:00:53 by localhost)