All posts in Java

  • 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”:


    and Ubuntu as OS.


    • 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="#{}"	 	 
     itemValue="#{product}" />	 	 

    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.

  • Netbeans won’t work on Mac OS X since apple dropt support for Java updates

    netbeans java 7 mac

    Apple, which has previously included Java with installations of Mac OS X, announced the move on its support site. It said that customers need to obtain Java directly from Oracle if they want to access web content written the widely used programming language.

    And now Oracle releases Java SE Development Kit 7 (JDK 1.7u4) for Mac OS X. It is the first official Oracle release for Mac OS X. See Release Notes.

    In case you type

    java -version

    you are going to notice that your mac still has 1.6 JVM.

    Too many forums suggest to edit your java environment variables, but this is a very bad solution and maybe a temp one.
    As I searched I noticed that Oracle has released a Netbeans 7.3 along with JDK 7 which would solve the problem.

    Here’s the link: JDK 7u17 with NetBeans 7.3 – x64

    After you install the new JDK all your previous Netbeans versions will work normally.

  • Exit Button In Android App

    Android Exit

    A lot of developers wondering about the exit button and how they can apply it efficiently, well there are many discussions on stackoverflow or other forums suggest to use this piece of code.


    This code will only “finish” the current activity and never finishes the rest of the activities or take them to the finalize stage.

    So is there a way to exit an android app completely ?

    the answer is very simple: never and never allow the user to exit the app on his own.

    WHY ?

    That’s against Android nature and best practices, and yet the system handles this automatically. That’s what the activity lifecycle (especially onPause/onStop/onDestroy) is for. No matter what you do, do not put a “close” or “exit” application button.

    Do Users Really Want It?

    Let me posit this assertion: no they don’t.

    Of the apps that ship with the device (Gmail, Contacts, Maps, Gallery, etc.) exactly none of them include an exit button, and most users are comfortable with that. Nonetheless many developers are adamant, “I added it after users demanded it!”.
    if you have to add this button, try not to, just explain why to whom ever demands this functionality, as I’m trying to do always when this case comes around, and it always works.

    Here’s a good video which explains the case:

  • 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 = 'MP3 Extra'

    JPA Criteria would be

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

    JPQL single-string would be

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

    JPA Criteria would be

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

    Select of aggregate of attribute of persistable objects

    JPQL single-string would be
    Read more

  • EJB 3.0, is it really enough ?

    After daily dealing with EJB 3.0 and after a long “experience” in the core field which it is EJB basically. it turned out that sometimes I have to do some work arounds (In some cases) mostly EJB-QL. When we compare JBoss Hibernate-QL and EJB-QL we can see this missing gap in the EJB-QL specification. Let’s have an example which shows one of the missing gaps in EJB-QL.

    EJB-QL date comparison

    when we say date comparison in the most cases it’s like this

    [sql]SELECT p FROM Person p WHERE p.birthday = :date[/sql]

    OR even if we want to know today’s users birthday !!

    [sql]SELECT p FROM Person p WHERE p.birthday = CURRENT_DATE[/sql]

    so in this case if our p.birthday is a timestamp for some reasons we are in a big problem, because the previous case won’t work for us.
    some documentations say “after a little search on the internet”, when you are passing the parameter “date” pass it with a costume setParameter method, so the EJB-QL knows how it will convert it to the native SQL daah !!!

    [java]query.setParameter(&quot;date&quot;,calendar.getTime(), TemporalType.DATE)[/java]

    alright we get it know it’s for the parameter but the problem is in the object property itself, isn’t ?
    so we had to work around this case by passing two dates the first one is like this 2010-11-6:00:00:00 and the second is 2010-11-6:23:59:59
    and use the between keyword. come on guys it’s a silly move.

    [sql]SELECT p FROM Person p WHERE p.birthday BETWEEN date1 AND date2[/sql]

    OK in this case you succeed with ignoring the HH:mm:ss bravo !!!
    otherwise in the JBoss Hibernate you would easily do this by using the day function.

    [sql]SELECT p FROM Person p WHERE p.birthday = day(current_date())[/sql] .

    After all a good point is we can use Hibernate with our EJBs .

    To Be Continued.