Vu que 80% des actions effectuées sur un projet tiennent du CRUD, il m' est apparu utile de créer un EJB générique qui pourrait être utilisé ou étendu dans d'autres EJB session

Voici la bête. Elle réutilise le findByExample que j'ai crée avant

C'est simple mais vachement utile

@Stateless
public class CRUDBean<T> implements CRUDLocal<T> {

    @PersistenceContext
    EntityManager em;

    public T add(T entity) {
        em.persist(entity);
        return entity;
    }

    public T update(T entity) {
        return em.merge(entity);
    }

    public void delete(T entity) {
        em.remove(entity);
    }

    public List<T> findAll(Class<T> entityDescription) {
            return em.createQuery("select o from " + entityDescription.getName() + " o ").getResultList();
    }

    public T findByPrimaryKey(Class<T> entityDescription, Long id) {
        return em.find(entityDescription, id);
    }

    public List<T> findByExample(T example, OPERATOR operator) {
        List<T> results = new ArrayList<T>();
        StringBuffer queryByExample =
                new StringBuffer("select A from ").append(
                example.getClass().getSimpleName()).append(" A where ");
        Field[] fields = example.getClass().getDeclaredFields();
        // on remplit la requete
        int numberOfParameters = 0;
        final String CLAUSE = " ".concat(String.valueOf(operator)).concat(" ");
        try {
            for (Field current : fields) {
                current.setAccessible(TRUE);
                if (!Modifier.isStatic(current.getModifiers())) {
                    if (current.get(example) != null) {
                        queryByExample = queryByExample.append(" A.").append(
                                current.getName()).
                                append(" = ").
                                append(":").
                                append(current.getName()).append(CLAUSE);
                        numberOfParameters++;

                    }
                }
            }
            if (queryByExample.substring(queryByExample.length() - CLAUSE.length()).equalsIgnoreCase(CLAUSE)) {
                queryByExample =
                        queryByExample.delete(queryByExample.length() - CLAUSE.length() + 1, queryByExample.length());
            }
            // on peuple la requete
            Query query = em.createQuery(queryByExample.toString());

            for (Field current : fields) {
                current.setAccessible(TRUE);
                if (!Modifier.isStatic(current.getModifiers())) {
                    if (current.get(example) != null) {
                        if (current != null) {
                            query.setParameter(current.getName(), current.get(example));
                        }
                    }
                }
            }
            results = query.getResultList();
        } catch (Exception e) {

        }
        return results;
    }
}


Pour exécuter ce code à forte valeur ajoutée, il vous faudra un vrai serveur JAVA EE5 ( donc vous pouvez oublier WEBLOGIC 10 qui ne supporte pas les EJB SESSION GENERIQUES :-D)

Ajouter à cet EJB, vous pouvez coupler un validateur appelé par un AroundInvoke et le tour est joué :-)