All posts tagged ejb

  • Create JSF 2.1, Primefaces 4.0, EJB, Restful Project, Using Maven, with JBoss AS 7, Postgres DB and Eclipse.

    In this step by step “tutorial”, I will try to demonstrate how to create a new JSF 2.1 Project and add Primefaces 4.0 to it, alongside with EJB, JPA, REST.

    I will use in this “tutorial”:

    used-toolkit

    and Ubuntu as OS.

    Contents:

    • Create Project Using Maven. (1)
    • Import and configure Eclipse Project. (2)
    • Add Primefaces.(3)

    Read more

  • Hibernate LazyInitializationException And p:selectCheckboxMenu

    I had a simple Entity with a @oneToMany relationship.

    Let’s call the Entity “Store” and the Entity of the relationship “Product”, obviously a Store has many Products.
    When it comes to use selectCheckboxMenu with the input (update) of products, a bug arise “org.hibernate.LazyInitializationException: could not initialize proxy – no Session”.
    For some reason using the primefaces selectCheckboxMenu (or h:selectmanycheckbox of the slandered JSF components) would end the Session of the current transaction.

    to solve this issue I had to supply the type of my collection to the component with attribute name=”collectionType”.

    <p:selectCheckboxMenu > 	 	 
     <f:attribute name="collectionType" value="java.util.ArrayList" />	 	 
     <f:selectItems value="#{bean.getStoreProducts()}" 	 	 
     var="product" itemLabel="#{product.name}"	 	 
     itemValue="#{product}" />	 	 
    </p:selectCheckboxMenu>

    it depends on your type of List if it’s a Set for example you can use java.util.HashSet instead of java.util.ArrayList.

  • JPA Criteria and JPQL

    JPA2 introduced its “Criteria” queries, providing an API for typesafe query generation without the need to hardcode field names etc in queries; it built on the approach of Hibernate Criteria.
    In these examples we have two classes, Inventory and Product, where Inventory has a set of products.

    Select of persistable objects with simple filter

    JPQL single-string would be

    SELECT p FROM Product p WHERE p.name = 'MP3 Extra'

    JPA Criteria would be

    CriteriaQuery criteria = builder.createQuery(Product.class);
    Root productRoot = criteria.from(Product.class);
    criteria.select(productRoot);
    criteria.where(builder.equal(productRoot.get(Product_.name), "MP3 Extra"));
    List products = em.createQuery(criteria).getResultList();

    JPQL single-string would be

    SELECT p.value, p.manufacturer FROM Product p WHERE p.name = 'MP3 Extra'

    JPA Criteria would be

    CriteriaQuery criteria = builder.createQuery();
    Root productRoot = criteria.from(Product.class);
    criteria.multiselect(productRoot.get(Product_.value),
    productRoot.get(Product_.manufacturer);
    criteria.where(builder.equal(productRoot.get(Product_.name), "MP3 Extra"));
    List <Object[]> results = em.createQuery(criteria).getResultList();

    Select of aggregate of attribute of persistable objects

    JPQL single-string would be
    Read more