Featured FREE Whitepapers

What's New Here?


REST + Spring Security session problem

REST, sessions .. wait. There are no sessions in REST application, right? Well, thats true. If we can avoid sessions we should do that. REST is stateless. The main concern about statelessness is authentication. In usual web applications we were used to store user data in session after authentication. How to solve that if we don’t want to use sessions? We should authenticate every request. Thanks to that we can scale our application, add new nodes, remove nodes without care about session replication and also about consumed Java heap memory. Recently I’ve been working on high load REST application. Actually we didn’t expect to have high traffic there but surprisingly we had much much higher that we have been prepared for (it’s so called “happy problem”). Application is based on Spring Framework and its secured with Spring Security deployed on Apache Tomcat 7. All resources are totally stateless – HttpSession is not touched by any piece of my code. Unfortunately used Java heap space was increasing all the time until: java.lang.OutOfMemoryError: Java heap space was thrown. In order to analyze Java heap dump and runtime usage of heap I used VisualVM.Heap dump analysis shown that big part of memory was used by ConcurrentHashMaps used by Tomcat to store sessions. Those sessions object were almost empty but there were so many of them that they consumed ~50% of reserved Java heap space.Add parameter to Tomcat startup script: -XX:+HeapDumpOnOutOfMemoryError to get Java heap dump on java.lang.OutOfMemoryError: Java heap space First thing that I’ve done was to limit session timeout from default 30 minutes to 1 minute (lowest possible option) in web.xml: <?xml version='1.0' encoding='UTF-8'?> <web-app xmlns='http://java.sun.com/xml/ns/javaee' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xsi:schemaLocation='http://java.sun.com/xml/ns/javaeehttp://java.sun.com/xml/ns/javaee/web-app_3_0.xsd'version='3.0'> <!-- ... --><session-config> <session-timeout>1</session-timeout> </session-config> </web-app> That solved the problem – heap was cleaned by GC with better results and OutOfMemoryError was not thrown anymore. But more important thing is where those sessions were coming from? The answer is: Spring Security. By default Spring Security creates sessions if required – which means that if user has authenticated successfully then session is created. In my case it meaan – always. In order to prevent Spring Security from creating sessions create-session='never' needs to be added to http: <http create-session='never'> <!-- ... --> </http> You might think – empty session objects should not be a problem. I can tell you that for application handling few hundreds of requests per second it does a real change. Especially when its not running in the cloud or has few GB or RAM memory dedicated for Java heap. That’s how Java heap usage look like after those modifications:Reference: Solving REST + Spring Security session problem from our JCG partner Maciej Walkowiak at the Software Development Journey blog....

How to analyze Thread Dump – Thread Stack Trace

This article is part 5 of our Thread Dump analysis series. So far you have learned the basic principles of Threads and their interactions with your Java EE container & JVM. You have also learned different Thread Dump formats for HotSpot and IBM Java VM’s. It is now time for you to deep dive into the analysis process. In order for you to quickly identify a problem pattern from a Thread Dump, you first need to understand how to read a Thread Stack Trace and how to get the “story” right. This means that if I ask you to tell me what the Thread #38 is doing; you should be able to precisely answer; including if Thread Stack Trace is showing a healthy (normal) vs. hang condition. Java Stack Trace revisited Most of you are familiar with Java stack traces. This is typical data that we find from server and application log files when a Java Exception is thrown. In this context, a Java stack trace is giving us the code execution path of the Thread that triggered the Java Exception such as a java.lang.NoClassDefFoundError, java.lang.NullPpointerException etc. Such code execution path allows us to see the different layers of code that ultimately lead to the Java Exception. Java stack traces must always be read from bottom-up:The line at the bottom will expose the originator of the request such as a Java / Java EE container Thread.  The first line at the top of the stack trace will show you the Java class where that last Exception got triggered.Let’s go through this process via a simple example. We created a sample Java program simply executing some Class methods calls and throwing an Exception. The program output generated is as per below: JavaStrackTraceSimulator Author: Pierre-Hugues Charbonneauhttp://javaeesupportpatterns.blogspot.comException in thread "main" java.lang.IllegalArgumentException: at org.ph.javaee.training.td.Class2.call(Class2.java:12) at org.ph.javaee.training.td.Class1.call(Class1.java:14) at org.ph.javaee.training.td.JavaSTSimulator.main(JavaSTSimulator.java:20)Java program JavaSTSimulator is invoked (via the “main” Thread)  The simulator then invokes method call() from Class1 Class1 method call() then invokes Class2 method call() Class2 method call() throws a Java Exception: java.lang.IllegalArgumentException  The Java Exception is then displayed in the log / standard outputAs you can see, the code execution path that lead to this Exception is always displayed from bottom-up. The above analysis process should be well known for any Java programmer. What you will see next is that the Thread Dump Thread stack trace analysis process is very similar to above Java stack trace analysis. Thread Dump: Thread Stack Trace analysis Thread Dump generated from the JVM provides you with a code level execution snapshot of all the “created” Threads of the entire JVM process. Created Threads does not mean that all these Threads are actually doing something. In a typical Thread Dump snapshot generated from a Java EE container JVM: Some Threads could be performing raw computing tasks such as XML parsing, IO / disk access etc.  Some Threads could be waiting for some blocking IO calls such as a remote Web Service call, a DB / JDBC query etc.   Some Threads could be involved in garbage collection at that time e.g. GC Threads  Some Threads will be waiting for some work to do (Threads not doing any work typically go in wait() state)  Some Threads could be waiting for some other Threads work to complete e.g. Threads waiting to acquire a monitor lock (synchronized block{}) on some objectsI will get back to the above with more diagrams in my next article but for now let’s focus on the stack trace analysis process. Your next task is to be able to read a Thread stack trace and understand what it is doing, on the best of your knowledge. A Thread stack trace provides you with a snapshot of its current execution. The first line typically includes native information of the Thread such as its name, state, address etc. The current execution stack trace has to be read from bottom-up. Please follow the analysis process below. The more experience you get with Thread Dump analysis, the faster you will able to read and identify very quickly the work performed by each Thread:Start to read the Thread stack trace from the bottom  First, identify the originator (Java EE container Thread, custom Thread ,GC Thread, JVM internal Thread, standalone Java program “main” Thread etc.)  The next step is to identify the type of request the Thread is executing (WebApp, Web Service, JMS, Remote EJB (RMI), internal Java EE container etc.)  The next step is to identify form the execution stack trace your application module(s) involved e.g. the actual core work the Thread is trying to perform. The complexity of analysis will depend of the layers of abstraction of your middleware environment and application  The next step is to look at the last ~10-20 lines prior to the first line. Identify the protocol or work the Thread is involved with e.g. HTTP call, Socket communication, JDBC or raw computing tasks such as disk access, class loading etc.  The next step is to look at the first line. The first line usually tells a LOT on the Thread state since it is the current piece of code executed at the time you took the snapshot  The combination of the last 2 steps is what will give you the core of information to conclude of what work and / or hanging condition the Thread is involved withNow find below a visual breakdown of the above steps using a real example of a Thread Dump Thread stack trace captured from a JBoss 5 production environment. In this example, many Threads were showing a similar problem pattern of excessive IO when creating new instances of JAX-WS Service instances.As you can see, the last 10 lines along with the first line will tell us what hanging or slow condition the Thread is involved with, if any. The lines from the bottom will give us detail of the originator and type of request. I hope this article has helped you understand the importance of proper Thread stack trace analysis. I will get back with much more Thread stack trace examples when we cover the most common Thread Dump problem patterns in future articles. The next article will now teach you how to breakdown the Thread Dump threads in logical silos and come up with a potential list of root cause “suspects”. Reference: How to analyze Thread Dump – Part 5: Thread Stack Trace from our JCG partner Pierre-Hugues Charbonneau at the Java EE Support Patterns & Java Tutorial blog....

Mobicents Diameter has a new home.. powered by git!

Due to the increasingly number and complexity of sub-projects, Mobicents has been splitting it’s projects under independent project homes. We are now at a transition stage, where each project lead will migrate his project at the appropriate time. For Diameter, after our major release (1.4.0.FINAL), it seemed the best time to do this migration. The new home of the Mobicents Diameter project is located at http://code.google.com/p/jdiameter/. Feel free to visit us! With this change, we have also taken the chance to move to a “better” and more powerful version control system, so we have changed from SVN to Git. We hope this will ease the contributions, which have been happening more often, thanks to out thriving community! Despite being a allegedly better VCS, Git also has it’s own shortcomings, such as not being able to checkout a single folder. The lack of this feature has impacted our structure, since it makes it impossible to independently release sub-components in the same Git repository. While digging for a solution, I’ve came across the “sparse checkout” feature. While this may work for checking out only some folder(s), it is not supported by maven release plugin, thus not fixing the main problem. Another possible (and probably more correct) solution would be to use the “sub-repository” approach, where we’d split the components into different sub-repositories. We actually did this on a first phase, but after realizing the big changes it would imply and being aware that google code messes with the automatic revision links ( http://code.google.com/p/support/issues/detail?id=3245) we have decided to revert back to the single repository approach and abdicate from the release independency for each component. We have barely used it, anyway. The only exception is for the Jopr RHQ Plugin, so it got it’s own sub-repository. As a reference for someone going through the same process of moving from a [googlecode] SVN repository to a [googlecode] git repository, I’ll leave a summary of the commands used for the move (please keep in mind that I’m totally new to git, so these may not be the optimal way, feel free to comment): 0. (optional) Since SVN only lists the username and git uses the email as well, the migration will cause weird authors such as “brainslog <brainslog@bf0df8d0-2c1f-0410-b170-bd30377b63dc>” it may be good to sanitize the authors. For that I’ve used the following script in the SVN repository: svn log -q | awk -F '|' '/^r/ {sub("^ ", "", $2); sub(" $", "", $2); print $2" = "$2" <"$2">"}' | sort -u > authors.txtAnd then I’ve fixed the outcome in authors.txt to the correct: username = Full Name . 1. Clone the new (and probably empty) repository git clone https://@code.google.com/p/jdiameter/ git-jdiameter2. Clone from SVN (with authors fixed) to the cloned git repository, with full history, tags and branches git svn clone -A authors.txt -Ttrunk/servers/diameter -ttags/servers/diameter -bbranches/servers/diameter https://mobicents.googlecode.com/svn git-jdiameterWhere -A points to the authors file, -T to the trunk of the SVN repository, -t to the tags and -b to the branches. Use only what you need, I didn’t needed the branches part as we don’t have any. 3. Enter the local git repository cd git-jdiameter4. Push! git push --all5. Create the git tags from SVN tags (I know this can be automatized but I needed to make some customizations to existing tags, as not all were correct) git tag "1.0.0.BETA1" "refs/remotes/tags/jdiameter-1.0.0.BETA1" ...git tag "1.0.0.FINAL" "refs/remotes/tags/1.0.0.FINAL"6. Push.. again! git push --tagsThese are the 7 magical steps! In case that you don’t care about history, simply do a svn export --force http://mobicents.googlecode.com/svn/trunk/servers/diameter/ git-jdiameterand push it, nothing else. Also, as mentioned, I did not had branches but I suppose they’d make it in the #4 push. Mobicents Diameter users working with trunk master, must now switch to this new repository as the SVN will not be updated anymore! Do a ” git clone https://code.google.com/p/jdiameter/” and hack away! Reference: Mobicents Diameter has a new home.. powered by git! from our JCG partner Alexandre Mendonca at the Alexandre Mendonca blog blog....

Apache Thrift with Java quickstart

Apache Thrift is a RPC framework founded by facebook and now it is an Apache project. Thrift lets you define data types and service interfaces in a language neutral definition file. That definition file is used as the input for the compiler to generate code for building RPC clients and servers that communicate over different programming languages. You can refer Thrift white paper also. According to the official web site Apache Thrift is a, software framework, for scalable cross-language services development, combines a software stack with a code generation engine to build services that work efficiently and seamlessly between C++, Java, Python, PHP, Ruby, Erlang, Perl, Haskell, C#, Cocoa, JavaScript, Node.js, Smalltalk, OCaml and Delphi and other languages. Image courtesy wikipediaInstalling Apache Thrift in Windows Installation Thrift can be a tiresome process. But for windows the compiler is available as a prebuilt exe. Download thrift.exe and add it into your environment variables. Writing Thrift definition file (.thrift file) Writing the Thrift definition file becomes really easy once you get used to it. I found this tutorial quite useful to begin with. Example definition file (add.thrift) namespace java com.eviac.blog.samples.thrift.server // defines the namespacetypedef i32 int //typedefs to get convenient names for your typesservice AdditionService { // defines the service to add two numbers int add(1:int n1, 2:int n2), //defines a method }Compiling Thrift definition file To compile the .thrift file use the following command. thrift --gen <language> <Thrift filename>For my example the command is, thrift --gen java add.thriftAfter performing the command, inside gen-java directory you’ll find the source codes which is useful for building RPC clients and server. In my example it will create a java code called AdditionService.java Writing a service handler Service handler class is required to implement the AdditionService.Iface interface. Example service handler (AdditionServiceHandler.java) package com.eviac.blog.samples.thrift.server;import org.apache.thrift.TException;public class AdditionServiceHandler implements AdditionService.Iface {@Override public int add(int n1, int n2) throws TException { return n1 + n2; }}  Writing a simple server Following is an example code to initiate a simple thrift server. To enable the multithreaded server uncomment the commented parts of the example code. Example server (MyServer.java) package com.eviac.blog.samples.thrift.server;import org.apache.thrift.transport.TServerSocket; import org.apache.thrift.transport.TServerTransport; import org.apache.thrift.server.TServer; import org.apache.thrift.server.TServer.Args; import org.apache.thrift.server.TSimpleServer;public class MyServer {public static void StartsimpleServer(AdditionService.Processor<AdditionServiceHandler> processor) { try { TServerTransport serverTransport = new TServerSocket(9090); TServer server = new TSimpleServer( new Args(serverTransport).processor(processor));// Use this for a multithreaded server // TServer server = new TThreadPoolServer(new // TThreadPoolServer.Args(serverTransport).processor(processor));System.out.println("Starting the simple server..."); server.serve(); } catch (Exception e) { e.printStackTrace(); } } public static void main(String[] args) { StartsimpleServer(new AdditionService.Processor<AdditionServiceHandler>(new AdditionServiceHandler())); }}Writing the client Following is an example java client code which consumes the service provided by AdditionService. Example client code (AdditionClient.java) package com.eviac.blog.samples.thrift.client;import org.apache.thrift.TException; import org.apache.thrift.protocol.TBinaryProtocol; import org.apache.thrift.protocol.TProtocol; import org.apache.thrift.transport.TSocket; import org.apache.thrift.transport.TTransport; import org.apache.thrift.transport.TTransportException;public class AdditionClient {public static void main(String[] args) {try { TTransport transport;transport = new TSocket("localhost", 9090); transport.open();TProtocol protocol = new TBinaryProtocol(transport); AdditionService.Client client = new AdditionService.Client(protocol);System.out.println(client.add(100, 200));transport.close(); } catch (TTransportException e) { e.printStackTrace(); } catch (TException x) { x.printStackTrace(); } }}Run the server code(MyServer.java). It should output following and will listen to the requests. Starting the simple server...Then run the client code(AdditionClient.java). It should output following. 300Reference: Apache Thrift with Java quickstart from our JCG partner Pavithra Siriwardena at the EVIAC blog....

Four solutions to the LazyInitializationException – Part 1

In the post today we will talk about the common LazyInitializationException error. We will see four ways to avoid this error, the advantage and disadvantage of each approach and in the end of this post, we will talk about how the EclipseLink handles this exception. To see the LazyInitializationException error and to handle it, we will use an application JSF 2 with EJB 3. Topics of the post:Understanding the problem, Why does LazyInitializationException happen? Load collection by annotation Load collection by Open Session in View (Transaction in View) Load collection by Stateful EJB with PersistenceContextType.EXTENDED Load collection by Join Query EclipseLink and lazy collection initializationAt the end of this post you will find the source code to download. Attention: In this post we will find an easy code to read that does not apply design patterns. This post focus is to show solutions to the LazyInitializationException. The solutions that you will find here works for web technology like JSP with Struts, JSP with VRaptor, JSP with Servlet, JSF with anything else. Model Classes In the post today we will use the class Person and Dog: package com.model;import javax.persistence.*;@Entity public class Dog {@Id @GeneratedValue(strategy = GenerationType.AUTO) private int id;private String name;public Dog() {}public Dog(String name) { this.name = name; }//get and set } package com.model;import java.util.*;import javax.persistence.*;@Entity public class Person {@Id @GeneratedValue(strategy = GenerationType.AUTO) private int id;private String name;@OneToMany @JoinTable(name = 'person_has_lazy_dogs') private List<Dog> lazyDogs;public Person() {}public Person(String name) { this.name = name; }// get and set } Notice that with this two classes we will be able to create the LazyInitializationException. We have a class Person with a Dog list. We also will use a class to handle the database actions (EJB DAO) and a ManagedBean to help us to create the error and to handle it: package com.ejb;import java.util.List;import javax.ejb.*; import javax.persistence.*;import com.model.*;@Stateless public class SystemDAO {@PersistenceContext(unitName = 'LazyPU') private EntityManager entityManager;private void saveDogs(List<Dog> dogs) { for (Dog dog : dogs) { entityManager.persist(dog); } }public void savePerson(Person person) { saveDogs(person.getLazyDogs()); saveDogs(person.getEagerDogs()); entityManager.persist(person); }// you could use the entityManager.find() method also public Person findByName(String name) { Query query = entityManager.createQuery('select p from Person p where name = :name'); query.setParameter('name', name);Person result = null; try { result = (Person) query.getSingleResult(); } catch (NoResultException e) { // no result found }return result; } } package com.mb;import javax.ejb.EJB; import javax.faces.bean.*;import com.ejb.SystemDAO; import com.model.*;@ManagedBean @RequestScoped public class DataMB {@EJB private SystemDAO systemDAO;private Person person;public Person getPerson() { return systemDAO.findByName('Mark M.'); } } Why does LazyInitializationException happen? The Person class has a Dog list. The easier and fattest way to display a person data would be, to use the entityManager.find() method and iterate over the collection in the page (xhtml). All that we want was that the code bellow would do this… // you could use the entityManager.find() method also public Person findByName(String name) { Query query = entityManager.createQuery('select p from Person p where name = :name'); query.setParameter('name', name);Person result = null; try { result = (Person) query.getSingleResult(); } catch (NoResultException e) { // no result found }return result; } public Person getPerson() { return systemDAO.findByName('Mark M.'); } <!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.0 Transitional//EN' 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd'> <html xmlns='http://www.w3.org/1999/xhtml' xmlns:f='http://java.sun.com/jsf/core' xmlns:h='http://java.sun.com/jsf/html' xmlns:ui='http://java.sun.com/jsf/facelets'> <h:head></h:head> <h:body> <h:form> <h:dataTable var='dog' value='#{dataMB.personByQuery.lazyDogs}'> <h:column> <f:facet name='header'> Dog name </f:facet> #{dog.name} </h:column> </h:dataTable> </h:form> </h:body> </html> Notice that in the code above, all we want to do is to find a person in the database and display its dogs to an user. If you try to access the page with the code above you will see the exception bellow: [javax.enterprise.resource.webcontainer.jsf.application] (http– Error Rendering View[/getLazyException.xhtml]: org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: com.model.Person.lazyDogs, no session or session was closedat org.hibernate.collection.internal.AbstractPersistentCollection. throwLazyInitializationException(AbstractPersistentCollection.java:393) [hibernate-core-4.0.1.Final.jar:4.0.1.Final]at org.hibernate.collection.internal.AbstractPersistentCollection. throwLazyInitializationExceptionIfNotConnected (AbstractPersistentCollection.java:385) [ hibernate-core-4.0.1.Final.jar:4.0.1.Final]at org.hibernate.collection.internal.AbstractPersistentCollection. readSize(AbstractPersistentCollection.java:125) [hibernate-core-4.0.1.Final.jar:4.0.1.Final]To understand better this error let us see how the JPA/Hibernate handles the relationship. Every time we do a query in the database the JPA will bring to all information of that class. The exception to this rule is when we talk about list (collection). Image that we have an announcement object with a list of 70,000 of emails that will receive this announcement. If you want just to display the announcement name to the user in the screen, imagine the work that the JPA would have if the 70,000 emails were loaded with the name. The JPA created a technology named Lazy Loading to the classes attributes. We could define Lazy Loading by: “the desired information will be loaded (from database) only when it is needed”. Notice in the above code, that the database query will return a Person object. When you access the lazyDogs collection, the container will notice that the lazyDogs collection is a lazy attribute and it will “ask” the JPA to load this collection from the database. In the moment of the query (that will bring the lazyDogs collection) execution, an exception will happen. When the JPA/Hibernate tries to access the database to get this lazy information, the JPA will notice that there is no opened collection. That is why the exception happens, the lack of an opened database connection. Every relationship that finishes with @Many will be lazy loaded by default: @OneToMany and @ManyToMany. Every relationship that finishes with @One will be eagerly loaded by default: @ManyToOne and @OneToOne. If you want to set a basic field (E.g. String name) with lazy loading just do: @Basic(fetch=FetchType.LAZY). Every basic field (E.g. String, int, double) that we can find inside a class will be eagerly loaded if the developer do not set it as lazy. A curious subject about default values is that you may find each JPA implementation (EclipseLink, Hibernate, OpenJPA) with a different behavior for the same annotation. We will talk about this later on. Load collection by annotation The easier and the fattest way to bring a lazy list when the object is loaded is by annotation. But this will not be the best approach always. In the code bellow we will se how to eagerly load a collection by annotation: @OneToMany(fetch = FetchType.EAGER) @JoinTable(name = 'person_has_eager_dogs') private List<Dog> eagerDogs; <h:dataTable var='dog' value='#{dataMB.person.eagerDogs}'> <h:column> <f:facet name='header'> Dog name </f:facet> #{dog.name} </h:column> </h:dataTable> Pros and Cons of this approach:ProsConsEasy to set upIf the class has several collections this will not be good to the server performanceThe list will always come with the loaded objectIf you want to display only a basic class attribute like name or age, all collections configured as EAGER will be loaded with the name and the ageThis approach will be a good alternative if the EAGER collection have only a few items. If the Person will only have 2, 3 dogs your system will be able to handle it very easily. If later the Persons dogs collection starts do grow a lot, it will not be good to the server performance. This approach can be applied to JSE and JEE. Load collection by Open Session in View (Transaction in View) Open Session in View (or Transaction in View) is a design pattern that you will leave a database connection opened until the end of the user request. When the application access a lazy collection the Hibernate/JPA will do a database query without a problem, no exception will be threw. This design pattern, when applied to web applications, uses a class that implements a Filter, this class will receive all the user requests. This design pattern is very easy to apply and there is two basic actions: open the database connection and close the database connection. You will need to edit the “web.xml” and add the filter configurations. Check bellow how our code will look like: <filter> <filter-name>ConnectionFilter</filter-name> <filter-class>com.filter.ConnectionFilter</filter-class> </filter> <filter-mapping> <filter-name>ConnectionFilter</filter-name> <url-pattern>/faces/*</url-pattern> </filter-mapping> package com.filter;import java.io.IOException;import javax.annotation.Resource; import javax.servlet.*; import javax.transaction.UserTransaction;public class ConnectionFilter implements Filter {@Override public void destroy() {}@Resource private UserTransaction utx;@Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { try { utx.begin(); chain.doFilter(request, response); utx.commit(); } catch (Exception e) { e.printStackTrace(); }}@Override public void init(FilterConfig arg0) throws ServletException {} } <h:dataTable var='dog' value='#{dataMB.person.lazyDogs}'> <h:column> <f:facet name='header'> Dog name </f:facet> #{dog.name} </h:column> </h:dataTable> Pros and Cons of this approach:ProsConsThe model classes will not need to be editedAll transaction must be handled in the filter classThe developer must be very cautious with database transaction errors. A success message can be sent by the ManagedBean/Servlet, but when the database commits the transacion an error may happenN+1 effect may happen (more detail bellow)The major issue of this approach is the N+1 effect. When the method returns a person to the user page, the page will iterate over the dogs collection. When the page access the lazy collection a new database query will be fired to bring the dog lazy list. Imagine if the Dog has a collection of dogs, the dogs children. To load the dogs children list other database query would be fired. But if the children has other children, again the JPA would fire a new database query… and there it goes… This is the major issue of this approach. A query can create almost a infinity number of other queries. This approach can be applied to JSE and JEE. Continue to the second part of this tutorial. Reference: Four solutions to the LazyInitializationException from our JCG partner Hebert Coelho at the uaiHebert blog....

Four solutions to the LazyInitializationException – Part 2

This article continues from part 1 of the tutorial. Load collection by Stateful EJB with PersistenceContextType.EXTENDED This approach can be applied only to applications that works with Full JEE environments: to use a EJB with PersistenceContextType.EXTENDED. Check the code below how the DAO would look like: package com.ejb;import javax.ejb.Stateful; import javax.persistence.*;import com.model.Person;@Stateful public class SystemDAOStateful { @PersistenceContext(unitName = 'LazyPU', type=PersistenceContextType.EXTENDED) private EntityManager entityManager;public Person findByName(String name) { Query query = entityManager.createQuery('select p from Person p where name = :name'); query.setParameter('name', name);Person result = null; try { result = (Person) query.getSingleResult(); } catch (NoResultException e) { // no result found }return result; } } public class DataMB { // other methods and attributes@EJB private SystemDAOStateful daoStateful;public Person getPersonByStatefulEJB() { return daoStateful.findByName('Mark M.'); } } <h:dataTable var='dog' value='#{dataMB.personByStatefulEJB.lazyDogs}'> <h:column> <f:facet name='header'> Dog name </f:facet> #{dog.name} </h:column> </h:dataTable> Pros and Cons of this approach:ProsConsThe container will control the database transactionWorks only for JEEThe model classes will not need to be editedN+1 effect may happenA great amount of Stateful EJBs may affect the container memory.This approach may create the N+1 effect and the Stateful EJB has a characteristic of not being removed/destroyed while the its session is not expired or until it lost its reference. Warning: It is not a good practice to hold a reference to an injected EJB in objects that remain in a Pool. The JSF will create a ManagedBean pool to handle better the user requests; when it is necessary the container will increase or decrease the number of ManagedBeans in the pool. In the code of this post imagine if the container create 100 instances of ManagedBeans in the pool, the server will hold 100 Stateful EJBs in memory. The solution to this problem would be a JNDI LookUp to the Stateful EJB. Load collection by Join Query This solution is easy to understand and to apply. See the code below: public Person findByNameWithJoinFech(String name) { Query query = entityManager.createQuery('select p from Person p join fetch p.lazyDogs where p.name = :name'); query.setParameter('name', name);Person result = null; try { result = (Person) query.getSingleResult(); } catch (NoResultException e) { // no result found }return result; } public Person getPersonByQuery() { return systemDAO.findByNameWithJoinFech('Mark M.'); } <h:dataTable var='dog' value='#{dataMB.personByQuery.lazyDogs}'> <h:column> <f:facet name='header'> Dog name </f:facet> #{dog.name} </h:column> </h:dataTable> Pros and Cons of this approach:ProsConsJust one query will be fired in the databaseIt would be necessary one query for each accessed collection/lazy attributeThe model classes will not need to be editedWill bring only the desired dataThe N+1 effect will not happenThis approach has the disadvantage of the need of a new query to access each model class collection/lazy attribute. If we need to query only the Person dogs we would need of a specific query. Imagine that we would need to query for the Person emails, it would be necessary a different query. This approach can be applied to JSE and JEE. EclipseLink and lazy collection initialization The relationships default values are:RelationshipFetch@OneToOneEAGER@OneToManyLAZY@ManyToOneEAGER@ManyToManyLAZYBut the JPA Spec* says that: The EAGER strategy is a requirement on the persistence provider runtime that data must be eagerly fetched. The LAZY strategy is a hint to the persistence provider runtime that data should be fetched lazily when it is first accessed. The implementation is permitted to eagerly fetch data for which the LAZY strategy hint has been specified. In particular, lazy fetching might only be available for Basic mappings for which property-based access is used. As you can see in the text above, the JPA implementation may ignore the hint strategy if it wants to. The EclipseLink has a behavior with JEE and other behavior to JSE. You can see each behavior here: http://wiki.eclipse.org/Using_EclipseLink_JPA_Extensions_%28ELUG%29#What_You_May_Need_to_Know_About_EclipseLink_JPA_Lazy_Loading We can find in the internet some people saying that even with a lazy collection the EclipseLink does the n+1 queries when the entity is loaded. And we can find this behavior of users with Glassfish and EJB. Below you will see some tips to use correctly lazy load with EclipseLink:http://stackoverflow.com/questions/8490532/eclipselink-lazy-loading http://stackoverflow.com/questions/3932623/eclipselink-dont-fetch-some-fields-by-default https://forums.oracle.com/forums/thread.jspa?messageID=1706796* JSR-000220 Enterprise JavaBeans 3.0 Final Release (persistence) 9.1.18 and will repeat to the otters JPA relationships. The end! In my opinion the best solution is the Join Fetch Query. It is up to you to choose the best solution to your application. Click here to download the source code of this post. If you want to run the code of this post you will need to create a database named LazyExceptionDB and the JBoss module. Attached to the source code is the Postgres module. If you want to see how to set up the datasource and the Postgres or MySQL module you can see it here: Full WebApplication JSF EJB JPA JAAS. I hope this post might help you. If you have any comment or doubt just post it. See you soon. Reference: Four solutions to the LazyInitializationException from our JCG partner Hebert Coelho at the uaiHebert blog....

Architects Need a Pragmatic Software Development Process

I have been a non-stop software architect since 2006. During my experience, I realized that it’s really hard to perform the role of architect in an organization that doesn’t have a software development process or have it too simplified. When the development is not fairly organized, project managers don’t find a room in their schedule to implement architectural recommendations. They probably have time and resources, but since they don’t have a precise idea of the team’s productivity, they feel afraid of accepting new non-functional requirements or changing existing ones. I’ve noticed that, in a chaotic environment, people become excessively pragmatic, averse to changes. Architects expect that the organization adopts a more predictable and transparent software process. This way, it’s possible to visualize the impact of recommendations and negotiate when they are going to be implemented. They minimally expect a process that has iterations inspired on the classical PDCA (Plan, Do, Check and Act) cycle because it has loops with feedback, which are the foundation for continuous improvement. The figure below depicts what could be considered as a pragmatic software process.Iterations are overlapped in time in order to optimize the use of resources and guarantee the feedback from previous iterations. Each iteration is performed in a fixed period of time. This time depends on the context and it tends to fall as the organization gains more maturity. An iteration is composed of 4 phases (plan, do, check and act) and 5 events that may occur according to the planning. They are:T1: It represents the beginning of the iteration, starting with its planning. The scope of the planning covers only the period of the current iteration. It should not be mixed with the general project planning, which is produced in one of the initial iterations to plan all other iterations. All members of the team participate in the planning. T2: The execution of what was planned for the iteration starts. All members of the team must have something to do within the scope of the iteration. Nothing planned for future iterations should be done in the current iteration. People may produce all sort of output, such as documents, code, reports, meeting minutes, etc. T3: Everything that is produced should be checked. Documents should be reviewed, code should be tested, user interfaces and integrations with other systems should be tested, etc. All found issues must be registered to be solved in due time. T4: Solve all issues found during the check phase and release all planned deliverables. Everybody should deliver something. In case time is not enough to solve some found issues, they must be included in the planning of the next iteration with the highest priority. Statistics should be produced during this phase in order to compare the planning with the execution. The planning of the next iteration also starts at this point, taking advantage of the experience from the previous iteration. T5: Once everything is released, the current iteration finishes. T2 of the next iteration immediately starts because most of resources are already available.T1 to T5 repeats several times, in a fixed period of time, until the end of the project. This suggestion is process-agnostic, thus it can be implemented no matter what software process we claim to have in place or any other modern process we can think of. In addition to the process, there are also some good practices:Consider everything that describes how the system implements business needs as use cases. It can also be functional and nonfunctional requirements, user stories, scenarios, etc; but there must be only one sort of artefact to describe business needs. Write use cases in a way that the text can be reused to: a) help business people to visualize how their needs will work; b) guide testers on the exploratory tests; and c) help the support team to prepare the user manual. Avoid technical terms in use cases. If really needed, technical details may be documented in another artefact, such as use case realizations. If needed, create use case realizations using UML models only. Representing use case realisations as documents implies on a huge overhead. Any necessary textual information can be added in the comments area of UML’s elements. Fix the size of use cases according to the effort to realize it. For example: we can fix that the maximum size of a use case is 1 week. If the estimation is higher than that, then the use case must be divided in two others. If the estimation is far lower than that, then the use case must be merged with another closely related use case. By simply counting the number of use cases we immediately know the effort and the resources required to execute the project. This fixed number is a parameter to compare the planning with the execution. By performing this comparison after every iteration, we gradually know how precise our estimations are becoming. Use a wiki to document use cases and other required documentations, such as test cases, release notes, etc. Create a wiki page for each use case and use signs to indicate what is still pending to be released. The advantages of the wiki are: a) use cases are immediately available for all stakeholders as they are gathered; b) stakeholders can follow the evolution of the use cases by following updates in the page; c) it’s possible to know everyone who contributed to the use case and what exactly they did; and d) it’s possible to add comments to use cases, preserving all the discussion around it. If the organization has business processes, which is another thing that architects also love, then put references in the business process’ activities pointing to the use cases that implement them. A reference is a link to the page where the use case is published on the wiki. Follow up use cases using an issue tracking system, such as Jira. Each use case must have a corresponding Jira ticket and every detail of the use case’s planning, execution, checking and delivery must be registered in that ticket. The advantages of linking Jira tickets with use cases are: a) Jira tickets represent the execution of the planning and their figures can be compared with the planning, generating statistics on which managers can rely on; b) we know exactly every person who contributed to the use case, what they did, and for how long; and c) it’s an important source of lessons learned. Test, test, test! It must be an obsessive compulsive behaviour. Nothing goes out without passing through the extensive test session. Constantly train and provide all needed bibliography to the team on the technologies in use. The more technical knowledge we have inside of the team, the highest is our capability to solve problems and increase productivity.Working this way, everything becomes quantifiable, predictable, comparable and traceable. From the practices above we can extract the traceability flow from business to the lowest IT level, as depicted in the figure below.Business process elements such as swimlanes and activities may inspire actors and use cases. Use cases and actors are documented on wiki pages. Each use case and actor has a page on the wiki, which has a unique URL and can be used to refer the element on email messages, documents, Jira tickets and so on. An Jira ticket is created for each use case and it contains a link to the use case’s wiki page. This wiki page can also have a link to the ticket since it also has a unique URL. Jira tickets can be automatically linked the source code through the version control system (SVN) and declaratively linked to system’s features and user interfaces. Since it’s possible to create mock-ups in wiki pages, then we also link those wiki pages with user interfaces to compare the mock-ups with the final user interface. We finally have actors linked to security roles. I admit that architects are not qualified to define and implement a software development process in the organization (they actually believe more in the Programming Motherfucker philosophy :D), but they are constantly willing to contribute to have one in place. As they have instruments to monitor servers, releases, tests, performance and so on, they also want project managers having instruments to estimate effort, predict events, anticipate problems and, therefore, produce better planning and results. Warning: Whatever we put in our software processes that is not quantifiable or measurable will become an expensive overhead. Reference: Architects Need a Pragmatic Software Development Process from our JCG partner Hildeberto Mendonca at the Hildeberto’s Blog blog....

JavaFX 2.0 Layout Panes – HBox and VBox

If you want an overview on all different layout panes in JavaFX 2.0 or if you want to know yome basic facts about them, please see my previous post Layout Panes in JavaFX 2.0.The layout panes HBox and VBox are definitely the most basic layout containers in JavaFX 2.0. As you can already tell by their name, their purpose is to layout all their children in one horizontal row (HBox) or in one vertical column (VBox). Because they´re very easy to use and very useful regarding minor layout issues, you´ll probably use them a lot. I´ll give two examples on how you can use them. As in the other examples, first of all the code and afterwards the explanations. HBox and VBox – Example 1   import javafx.application.Application; import javafx.geometry.Pos; import javafx.scene.Scene; import javafx.scene.control.Button; import javafx.scene.layout.HBox; import javafx.scene.layout.VBox; import javafx.stage.Stage;/** * * Created on: 20.03.2012 * @author Sebastian Damm */ public class HBoxandVBoxExample extends Application { @Override public void start(Stage primaryStage) throws Exception { HBox hbox = new HBox(50); hbox.setAlignment(Pos.CENTER); // default TOP_LEFT VBox vbox1 = new VBox(); vbox1.setAlignment(Pos.BOTTOM_CENTER); vbox1.setStyle("-fx-border-style: solid;" + "-fx-border-width: 1;" + "-fx-border-color: black"); VBox vbox2 = new VBox(10); vbox2.setAlignment(Pos.CENTER); vbox2.setStyle("-fx-border-style: solid;" + "-fx-border-width: 1;" + "-fx-border-color: black"); VBox vbox3 = new VBox(20); vbox3.setAlignment(Pos.TOP_CENTER); vbox3.setStyle("-fx-border-style: solid;" + "-fx-border-width: 1;" + "-fx-border-color: black"); for (int i = 0; i < 5; i++) { Button bt = new Button("Button " + (i+1)); Button bt2 = new Button("Button " + (i+1)); // unfortunately there´s no "clone" or "copy" method Button bt3 = new Button("Button " + (i+1));vbox1.getChildren().add(bt); vbox2.getChildren().add(bt2); vbox3.getChildren().add(bt3); } hbox.getChildren().addAll(vbox1, vbox2, vbox3); Scene scene = new Scene(hbox, 350, 250); // the hbox is the root node primaryStage.setTitle("HBox and VBox Example"); primaryStage.setScene(scene); primaryStage.show(); }public static void main(String[] args) { Application.launch(args); } } Basically we create three different VBoxes and put them into one HBox. In both classes you can define a spacing value either directly in the constructor or via the setSpacing method. This value will be used as the gap between the individual children in the pane. The line HBox hbox = new HBox(50); therefore creates a HBox to hold three VBoxes with a gap of 50 pixel between each of them.We use the setAlignment method to specify, how the individual VBoxes should arrange and layout all their childen.With setStyle you can apply custom CSS styles to any Node. I don´t want to go into much detail yet, because I´ll cover CSS styles in JavaFX 2.0 in one of my next posts, but if you´re already familiar with CSS you´ll probably already have noticed that the JavaFX 2.0 team fortunately decided to follow the CSS standards defined by W3C (http://www.w3.org) very closely. If you´re not familiar with CSS you just need to know that theses lines of CSS create a 1px wide black border around the component. I use them here to show you the size of the individual VBoxes.The next few lines should be pretty ordinary for you by now: We create five buttons for each VBox, put the different VBoxes into our HBox, declare a Scene object (with the HBox as root) and show our application.  Your application should look like this now:You can see that each VBox lays out their children buttons with the defined spacing value and that the HBox lays out the three VBoxes with a gap of 50 pixels. Because we set a different alignment for each VBox you can see that that they arrange the buttons inside their bounds in a specific way. Note: We also specified a alignment for the HBox, otherwise the three VBoxes would not have been layed out in the center! If you resize the window of your application it should look like this:You can see that the VBoxes grow and fill the space provided by their parent and that they still arrange their children according to their set alignment.HBox and VBox – Example 2The next example will show how to use the static setMargin and setHgrow (respectively setVgrow) methods: import javafx.application.Application; import javafx.geometry.Insets; import javafx.scene.Scene; import javafx.scene.control.*; import javafx.scene.layout.*; import javafx.stage.Stage;/** * * Created on: 20.03.2012 * @author Sebastian Damm */ public class HBoxandVBoxExample2 extends Application { @Override public void start(Stage primaryStage) throws Exception { StackPane root = new StackPane(); HBox hbox = new HBox(30); // create a HBox to hold 2 vboxes // create a vbox with a textarea that grows vertically VBox vbox = new VBox(10); Label lbName = new Label("I´m a label!"); TextField textField = new TextField(); TextArea textArea = new TextArea(); textArea.setPrefWidth(100); VBox.setVgrow(textArea, Priority.ALWAYS); vbox.getChildren().addAll(lbName, textField, textArea); // create a vbox that grows horizontally inside the hbox VBox vbox2 = new VBox(10); Label lbName2 = new Label("I´m also a label!"); TextField tf2 = new TextField(); tf2.setPromptText("type here"); TextArea textArea2 = new TextArea(); textArea2.setPrefWidth(100); vbox2.getChildren().addAll(lbName2, tf2, textArea2); HBox.setHgrow(vbox2, Priority.ALWAYS);// the next two lines behave equally - try to comment the first line out and use the 2nd line hbox.setPadding(new Insets(20)); // StackPane.setMargin(hbox, new Insets(20));hbox.getChildren().addAll(vbox, vbox2); root.getChildren().add(hbox); Scene scene = new Scene(root, 500, 300); // the stack pane is the root node primaryStage.setTitle("HBox and VBox Example 2"); primaryStage.setScene(scene); primaryStage.show(); }public static void main(String[] args) { Application.launch(args); } } In this example we create two VBoxes that are children of a HBox. Inside the VBoxes there are one label, one textfield and one textarea.The first remarkable line is VBox.setVgrow(textArea, Priority.ALWAYS). With this line we define, that the TextArea object should always grow vertically if it is contained by a VBox (otherwise there won´t be any effects). Next, put a focus on HBox.setHgrow(vbox2, Priority.ALWAYS). Here we tell the second VBox to grow horizontally inside the HBox.Finally with hbox.setPadding(new Insets(20)); or StackPane.setMargin(hbox, new Insets(20)) we give the whole HBox some padding. The two lines behave equally here because on the one side we specify a padding for the pane itself that should be used by the pane inside its bounds to layout its children and on the other side we tell the parent to lay out the pane with the given margin around it.Note: Margin is the outer distance/gap, padding the inner one. Here is a picture showing you the ‘CSS Box Model’ that demonstrates the relation between the content, the padding, the border, and the margin of an element.Source: w3Schools.comBecause the setPadding method is defined in the Region class, every layout pane can use this method. The Insets class is used a lot for those purposes and provides two different constructors: One that takes one double value and defines the same padding for each side and one constructor that takes four double values and defines the padding clockwise from the top to the left side.Hint: If you apply a border to the HBox and switch between the two lines for setting padding/margin, you will see the difference more clearly.Your application should look like this:Note the 10px padding around the HBox. If you now resize you window it should look similar to this:As you can see the TextArea in the left VBox grows vertically and the whole right VBox grows horizontally inside the HBox. Reference: JavaFX 2.0 Layout Panes – HBox and VBox from our JCG partner Sebastian Damm at the Just my 2 cents about Java blog....

Test JPQL / HQL without a deploy

Have you ever wanted to test your JPQL / HQL without doing a full deploy of your application? What we will see here today is simple solution that works for any JPA implementation: Hibernate, OpenJPA, EclipseLink and others. The base source code found in this post came from this book: “Pro JPA 2: Mastering the Java™ Persistence API – Mike Keith, Merrick Schincariol”. This post will add to the original code: query parameters and NamedQuery test. Model classes and data Generation Bellow are the model classes’ code: package com.model;import java.util.*;import javax.persistence.*;@Entity @NamedQueries({ @NamedQuery(name="Person.findByName", query="select p from Person p where p.name = :name"), @NamedQuery(name="Person.findByAge", query="select p from Person p where p.age = :age", hints={@QueryHint(name="org.hibernate.timeout", value="1000")}) })public class Person {public static final String FIND_BY_NAME = "Person.findByName"; public static final String FIND_BY_AGE = "Person.findByAge";@Id @GeneratedValue(strategy = GenerationType.AUTO) private int id;private String name; private int age;public Person() {}public Person(int id) { this.id = id; }public Person(String name, int age) { this.name = name; this.age = age; }@OneToMany(mappedBy = "person", cascade = CascadeType.ALL) private List<Dog> dogs;@OneToOne(cascade = CascadeType.ALL) @JoinColumn(name="address_id") private Address address;public int getId() { return id; }public void setId(int id) { this.id = id; }public String getName() { return name; }public void setName(String name) { this.name = name; }public int getAge() { return age; }public void setAge(int age) { this.age = age; }public List<Dog> getDogs() { if (dogs == null) { dogs = new ArrayList<Dog>(); }return dogs; }public void setDogs(List<Dog> dogs) { this.dogs = dogs; }public Address getAddress() { return address; }public void setAddress(Address address) { this.address = address; }@Override public int hashCode() { return getId(); }@Override public boolean equals(Object obj) { if (obj instanceof Person) { Person person = (Person) obj; return person.getId() == getId(); }return false; }@Override public String toString() { return "Person name: " + name; } } package com.model;import java.util.Date;import javax.persistence.*;@Entity @NamedQueries(value={@NamedQuery(name="Dog.FindByDateOfBirth", query="select d from Dog d where d.dateOfBirth = :dateOfBirth"), @NamedQuery(name="Dog.FindByPerson", query="select d from Dog d where d.person = :personObject")}) public class Dog {public static final String FIND_BY_DATE_OF_BIRTH = "Dog.FindByDateOfBirth";@Id @GeneratedValue(strategy = GenerationType.AUTO) private int id;private String name; private double weight;@Temporal(TemporalType.TIMESTAMP) private Date dateOfBirth;public Dog() {}public Dog(String name, double weight, Date dateOfBirth) { this.name = name; this.weight = weight; this.dateOfBirth = dateOfBirth; }public static void main(String[] args) {System.out.println(new Date()); }@ManyToOne private Person person;public int getId() { return id; }public void setId(int id) { this.id = id; }public String getName() { return name; }public void setName(String name) { this.name = name; }public double getWeight() { return weight; }public void setWeight(double weight) { this.weight = weight; }public Date getDateOfBirth() { return dateOfBirth; }public void setDateOfBirth(Date dateOfBirth) { this.dateOfBirth = dateOfBirth; }public Person getPerson() { return person; }public void setPerson(Person person) { this.person = person; }@Override public int hashCode() { return getId(); }@Override public boolean equals(Object obj) { if (obj instanceof Dog) { Dog dog = (Dog) obj; return dog.getId() == getId(); }return false; }@Override public String toString() { return "Dog name: " + name; } } package com.model;import javax.persistence.*;@Entity @NamedQuery(name="Address.FindAll", query="select a from Address a") public class Address {@Id @GeneratedValue(strategy = GenerationType.AUTO) private int id;private String streetName; private int houseNumber;public Address() {}public Address(String streetName, int houseNumber) { this.streetName = streetName; this.houseNumber = houseNumber; }public int getId() { return id; }public void setId(int id) { this.id = id; }public String getStreetName() { return streetName; }public void setStreetName(String streetName) { this.streetName = streetName; }public int getHouseNumber() { return houseNumber; }public void setHouseNumber(int houseNumber) { this.houseNumber = houseNumber; }@Override public int hashCode() { return getId(); }@Override public boolean equals(Object obj) { if (obj instanceof Address) { Address address = (Address) obj; return address.getId() == getId(); }return false; }@Override public String toString() { return "Adress street name: " + streetName; } } In the code above we got several JPA relationships as a sample. Bellow is the class that will handle the transaction and the data that will be written into the HSQLDB database: package com.main;import java.text.*; import java.util.Date;import javax.persistence.*;import com.model.*;public class CodeGenerator { private static EntityManagerFactory emf; private static EntityManager em;public static final String PERSON01_NAME = "John"; public static final String PERSON02_NAME = "Mary"; public static final String PERSON03_NAME = "Anna"; public static final String PERSON04_NAME = "Joseph"; public static final String PERSON05_NAME = "Mark"; public static final String PERSON06_NAME = "I will not have any relationship";public static void startConnection() { emf = Persistence.createEntityManagerFactory("QueryTester"); em = emf.createEntityManager(); em.getTransaction().begin(); }public static void closeConnection() { em.getTransaction().commit(); emf.close(); }public static void generateData() { int year = 1995; int month = 1; int day = 10;Dog dog01 = new Dog("Yellow", 3.5d, createNewDate(day, month, year)); Dog dog02 = new Dog("Brown", 8.5d, createNewDate(++day, ++month, ++year)); Dog dog03 = new Dog("Dark", 15.5d, createNewDate(++day, ++month, ++year)); Dog dog04 = new Dog("Kaka", 4.3d, createNewDate(++day, ++month, ++year)); Dog dog05 = new Dog("Pepe", 8.2d, createNewDate(++day, ++month, ++year)); Dog dog06 = new Dog("Casillas", 6.1d, createNewDate(++day, ++month, ++year)); Dog dog07 = new Dog("Fish", 6.7d, createNewDate(++day, ++month, ++year)); Dog dog08 = new Dog("Lion", 3.1d, createNewDate(++day, ++month, ++year)); Dog dog09 = new Dog("Cat", 5.5d, createNewDate(++day, ++month, ++year)); Dog dog10 = new Dog("Java", 21.7d, createNewDate(++day, ++month, ++year)); Dog dog11 = new Dog("JSF", 23.65d, createNewDate(++day, ++month, ++year)); Dog dog12 = new Dog("VRaptor", 24.0d, createNewDate(++day, ++month, ++year)); Dog dog13 = new Dog("Ferrari", 3.7d, createNewDate(++day, ++month, ++year)); Dog dog14 = new Dog("Porshe", 1.33d, createNewDate(++day, ++month, ++year)); Dog dog15 = new Dog("Bike", 4.44d, createNewDate(++day, ++month, ++year)); Dog dog16 = new Dog("Rambo", 5.44d, createNewDate(++day, ++month, 2015)); Dog dog17 = new Dog("Terminator", 3.88d, createNewDate(++day, ++month, 2016)); Dog dog18 = new Dog("John McClan", 3.88d, createNewDate(++day, ++month, 2016));Person person01 = new Person(PERSON01_NAME, 33); person01.getDogs().add(dog01); person01.getDogs().add(dog02); person01.getDogs().add(dog03); person01.setAddress(new Address("Street A", 30)); dog01.setPerson(person01); dog02.setPerson(person01); dog03.setPerson(person01);Person person02 = new Person(PERSON02_NAME, 27); person02.getDogs().add(dog04); person02.getDogs().add(dog05); person02.getDogs().add(dog06); person02.setAddress(new Address("Street B", 60)); dog04.setPerson(person02); dog05.setPerson(person02); dog06.setPerson(person02);Person person03 = new Person(PERSON03_NAME, 7); person03.getDogs().add(dog07); person03.getDogs().add(dog08); person03.getDogs().add(dog09); person03.setAddress(new Address("Street B", 90)); dog07.setPerson(person03); dog08.setPerson(person03); dog09.setPerson(person03);Person person04 = new Person(PERSON04_NAME, 43); person04.getDogs().add(dog10); person04.getDogs().add(dog11); person04.getDogs().add(dog12); person04.setAddress(new Address("Street C", 120)); dog10.setPerson(person04); dog11.setPerson(person04); dog12.setPerson(person04);Person person05 = new Person(PERSON05_NAME, 70); person05.getDogs().add(dog13); person05.getDogs().add(dog14); person05.getDogs().add(dog15); person05.getDogs().add(dog16); person05.setAddress(new Address("Street D", 150)); dog13.setPerson(person05); dog14.setPerson(person05); dog15.setPerson(person05); dog16.setPerson(person05);Person person06 = new Person(PERSON06_NAME, 45);em.persist(person01); em.persist(person02); em.persist(person03); em.persist(person04); em.persist(person05); em.persist(person06);em.persist(dog17); em.persist(dog18);em.flush(); }private static Date createNewDate(int day, int month, int year) { SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy"); try { return formatter.parse("" + day + "/" + month + "/" + year); } catch (ParseException e) { e.printStackTrace(); return null; } }public static EntityManager getEntityManager() { return em; } } The “persistence.xml” file can be found in the “src/META-INF” folder with the code bellow: <?xml version="1.0" encoding="UTF-8"?><persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"><persistence-unit name="QueryTester" transaction-type="RESOURCE_LOCAL"> <provider>org.hibernate.ejb.HibernatePersistence</provider><properties> <property name="javax.persistence.jdbc.driver" value="org.hsqldb.jdbcDriver" /> <property name="javax.persistence.jdbc.url" value="jdbc:hsqldb:mem:." /> <property name="javax.persistence.jdbc.user" value="sa" /> <property name="javax.persistence.jdbc.password" value="" /> <property name="hibernate.dialect" value="org.hibernate.dialect.HSQLDialect" /> <property name="hibernate.connection.shutdown" value="true" /> <property name="hibernate.hbm2ddl.auto" value="update" /> <property name="hibernate.show_sql" value="false" /> <property name="hibernate.format_sql" value="false"/> </properties> </persistence-unit> </persistence> Abstract test class package com.main;import javax.persistence.Query;import org.apache.commons.lang3.builder.ReflectionToStringBuilder; import org.apache.commons.lang3.builder.ToStringStyle;import com.model.Person;/** * @author Pro JPA 2 book * @Empowered by uaiHebert * */ public abstract class AbstractQueryTester {protected static void populateParameters(Query query, String parameters) { for (String parameter : parameters.split(";")) { String parameterKey = parameter.split("-")[0]; String parameterValue = parameter.split("-")[1]; String parameterType = parameter.split("-")[2];query.setParameter(parameterKey, configureParameterValue(parameterValue, parameterType)); } }private static Object configureParameterValue(String parameterValue, String parameterType) { if (parameterType.equalsIgnoreCase("integer")) { try { return Integer.parseInt(parameterValue); } catch (Exception e) { throw new IllegalArgumentException("Invalid parameter value as number: " + parameterValue); } }if (parameterType.equalsIgnoreCase("string")) { return parameterValue; }if (parameterType.equalsIgnoreCase("person")) { int personId;try { personId = Integer.valueOf(parameterValue); } catch (Exception e) { throw new IllegalArgumentException("Invalid parameter value as number: " + parameterValue); }return new Person(personId); }throw new IllegalArgumentException("Invalid parameter type: " + parameterType); }protected static void printResult(Object result) throws Exception { if (result == null) { System.out.print("NULL"); } else if (result instanceof Object[]) { Object[] row = (Object[]) result; System.out.print("["); for (int i = 0; i < row.length; i++) { printResult(row[i]); } System.out.print("]"); } else if (result instanceof Long || result instanceof Double || result instanceof String) { System.out.print(result.getClass().getName() + ": " + result); } else { System.out.print(ReflectionToStringBuilder.toString(result, ToStringStyle.SHORT_PREFIX_STYLE)); }System.out.println(); } } About the above code:The populateParameters method will populate all query parameters automatically. The Eclipse console will require that the developer type the Query information and the Query parameters if any is required. The Query parameters should follow the syntax requirements bellow:0 parameters: just press “Enter” 1 parameter: id-4-integer 2 or more parameters: name-John-string;age-33-integer The developer may use class as parameter: dog-33-DogThe configureParameterValue method will “cast” the parameterValue to the correct type required by the Query. It allows primitive values and classes. The printResult method will display the Query result.Dynamic Query test package com.main;import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.List;import javax.persistence.EntityManager; import javax.persistence.Query;/** * @author Pro JPA 2 book * @Empowered by uaiHebert * */ public class DynamicQueryTester extends AbstractQueryTester { public static void main(String[] args) throws IOException { CodeGenerator.startConnection();CodeGenerator.generateData();EntityManager em = CodeGenerator.getEntityManager();BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));for (;;) { System.out.print("Type your JPQL and press Enter > "); String dynamicQuery = reader.readLine();if (dynamicQuery.equals("quit")) { break; }if (dynamicQuery.length() == 0) { continue; }System.out.println("Type the namedQuery parameters."); System.out.println("All paramters should be like: id-2-integer;name-John-string"); System.out.println("Or just press enter for 0 parameters"); String parameters = reader.readLine();try { Query query = em.createQuery(dynamicQuery);if (parameters.length() > 0) { populateParameters(query, parameters); }@SuppressWarnings("rawtypes") List result = query.getResultList();if (result.size() > 0) { int count = 0; for (Object o : result) { System.out.print(++count + " "); printResult(o); } } else { System.out.println("0 results returned"); } } catch (Exception e) { e.printStackTrace(); } }CodeGenerator.closeConnection(); } } The code above will start a transaction, create the database data in runtime memory and allow a developer to test any kind of dynamic query. To test a JPQL / HQL the developer just need to type the Query code in the console. To finish the loop do not type any Query data, just type “quit” and press “Enter”. NamedQuery test   package com.main;import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.List;import javax.persistence.EntityManager; import javax.persistence.Query;/** * @author Pro JPA 2 book * @Empowered by uaiHebert * */ public class NamedQueryTester extends AbstractQueryTester { public static void main(String[] args) throws IOException { CodeGenerator.startConnection();CodeGenerator.generateData();EntityManager em = CodeGenerator.getEntityManager();BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));for (;;) { System.out.print("Type the NamedQuery name > "); String namedQueryName = reader.readLine();if (namedQueryName.equals("quit")) { break; }if (namedQueryName.length() == 0) { continue; }System.out.println("Type the namedQuery parameters."); System.out.println("Press enter for 0 parameters"); System.out.println("Or type all paramters like: id-2,name-4"); String parameters = reader.readLine();try { Query query = em.createNamedQuery(namedQueryName);if (parameters.length() > 0) { populateParameters(query, parameters); }@SuppressWarnings("rawtypes") List result = query.getResultList();if (result.size() > 0) { int count = 0; for (Object o : result) { System.out.print(++count + " "); printResult(o); } } else { System.out.println("0 results returned"); } } catch (Exception e) { System.err.println(e.getClass() + e.getMessage()); } }CodeGenerator.closeConnection(); } } The code above will start a transaction, create the database data in runtime memory and allow the test of NamedQueries that are configured in the model classes. To test the NamedQueries just type their name in the console. To finish the loop do not type any NamedQuery name, just type “quit” and press “Enter”. Running the application Run the class DynamicQueryTester and type the following text in the console: “select p from Person p”. Press enter twice and the following text will appear in the console:At the first “Enter” key hit a message will be displayed asking for parameters if there are any. Type in the console: “select p from Person p where p.age > :age” and press “Enter”. Type the parameter: “age-69-integer”. Press enter and the result bellow will be displayed:To finish the code that is running type the word “quit” and press the “Enter” button. Run now the code of the class NamedQueryTester. Type the NamedQuery name “Dog.FindByPerson” in the console and press enter. Type “personObject-1-person” parameter and the result bellow will be displayed:Using this post code with your application code You can use the code of this post in two ways: add your application model class to the project of this post or use the code found in the main package of this project in your application. Add your model class to the project of this post:Copy the model class to the “com.model” package. Set up the persistence.xml to access the databaseUse the code found in the main package in your application:Set up the PersistenceUnit found in the CodeGenerator class. Use the Apache library found in the libs folder: “commons-lang3-3-1.jar”.For both approaches the step bellow are required:Edit the method “AbstractQueryTester.configureParameterValue” to accept all attributes values/types/classes that will be used with the queries. Edit the “hibernate.hbm2ddl.auto” configuration to “none” or “validate”. This configuration is found in the “persistence.xml” file. Only invoke the methods to start/close the connection in the CodeGenerator class.Proposals Bellow you will find some proposals to do with this code:Use the Reflection technique to create the classes in the “AbstractQueryTester.configureParameterValue” method. Apply Jenkins to validate if some of the JPQLs are with the right syntax after a commit.The End I hope this post might help you. Click here to download the source code of this post. If you have any doubt/suggestion just post it. Reference: How to test your JPQL / HQL without a Deploy from our JCG partner Hebert Coelho at the uaiHebert blog....

Android – Using Jelly Bean notifications

Last post was long ago! I’ve been very busy lately… but learning a lot from Android! (and Node js too… I’m in love with this platform!)Other things that I decided for those who are following me is that, since now, articles will only be in English (sorry =/ ) and all the source code of my examples will be available on github.Obviously, being an Android developer, Google I/O has been a great source of new things to learn… And one of the was Jelly Bean (Android 4.1 for those that don’t know it yet).One of the top new things Jelly Bean brings us is new notification functionalities. You can see them in the Google I/O Keynote or some of the articles all over the Internet (1, 2, 3, 4)So, Where do we have to begin? First of all, you’ll have to create an Android Project with Jelly Bean SDK version.Probably, most of you had used NotificationManager to push notifications on Android devices… For new Jelly Bean notifications it must be used almost in the same way. You can use something like this: NotificationManager notificationManager = getNotificationManager(); Notification notification = new Notification(android.R.drawable.ic_menu_camera, "Hello camera!", System.currentTimeMillis()); notification.flags |= Notification.FLAG_AUTO_CANCEL; notification.number += 1;Intent intent = new Intent(this, MainActivity.class); PendingIntent activity = PendingIntent.getActivity(this, 0, intent, 0); notification.setLatestEventInfo(this, "title", "content", activity);notificationManager.notify(0, notification);What do we have to do to use new Jelly Bean notifications? It’s pretty easy…. and I must admit that I prefer this way using the Builder pattern. This is an example of the Big picture style: Builder build = new Notification.Builder(this) .setContentTitle("New mail from me") .setContentText("subject") .setTicker("New email with photo") .setSmallIcon(R.drawable.ic_action_search) .addAction( android.R.drawable.ic_btn_speak_now, "Speak!", PendingIntent.getActivity(getApplicationContext(), 0, getIntent(), 0, null)) .addAction( android.R.drawable.ic_dialog_map, "Maps", PendingIntent.getActivity(getApplicationContext(), 0, getIntent(), 0, null)) .addAction( android.R.drawable.ic_dialog_info, "Info", PendingIntent.getActivity(getApplicationContext(), 0, getIntent(), 0, null));Notification notification = new Notification.BigPictureStyle(build) .bigPicture( BitmapFactory.decodeResource(getResources(), R.drawable.jellybean)).build();Intent notificationIntent = new Intent(this, MainActivity.class);notificationManager.notify(0, notification);All the code is available at my github account (where you can find the beggining of a new ORM solution for Android which I hope someday will be finished =/) on the following link. you can find in that project other example of the new notifications too (inbox style and big text).Here is how the three notifications will show up in your Android devices…. (if you have Jelly Bean on them )I hope you like the article, and you can ask me any doubt, follow me on twitter or write me and email.Reference: Using Jelly Bean notifications from our JCG partner Javier Manzano at the Javier Manzano’s Blog blog....
Java Code Geeks and all content copyright © 2010-2014, Exelixis Media Ltd | Terms of Use | Privacy Policy
All trademarks and registered trademarks appearing on Java Code Geeks are the property of their respective owners.
Java is a trademark or registered trademark of Oracle Corporation in the United States and other countries.
Java Code Geeks is not connected to Oracle Corporation and is not sponsored by Oracle Corporation.

Sign up for our Newsletter

20,709 insiders are already enjoying weekly updates and complimentary whitepapers! Join them now to gain exclusive access to the latest news in the Java world, as well as insights about Android, Scala, Groovy and other related technologies.

As an extra bonus, by joining you will get our brand new e-books, published by Java Code Geeks and their JCG partners for your reading pleasure! Enter your info and stay on top of things,

  • Fresh trends
  • Cases and examples
  • Research and insights
  • Two complimentary e-books