Formatting XML using Java XML APIs

Formatting XML

Hello Readers ,

There are cases when an application receives an XML which is not formatted . A structured XML with proper indentation is useful for proper understanding of XML . Here is a small code snippet for formatting XML with proper indentation . The formatting XML code uses java xml APIs with default indentation of 4 spaces which is identified by INDENT_NUMBER.

merge function application in Hibernate

merge usage

The below example shows usage of Hibernate Session.merge () API. Suppose you have a scenario like below :

Since session is closed, userA object is in a detached state .

Now here, userB object represents the same persistent row as userA object When an attempt to reattach userA occurs, an exception is thrown

Exception when an attempt to reattach a detached object, userA .

This is because Hibernate is enforcing that only a single instance of a Persistent object exists in memory. To get around the above problem, merge() is used, as shown below :

userA is now detached as session is closed.

So, merge is useful when we want reattach a detached object but equivalent object is already present in session.

JSON with Spring REST – Basics

Spring JSON

Hello Readers ,
In this article we will how to produce JSON out from Spring REST web services. The article uses a simple example to demonstrate how easy it is to map the data from Spring REST to JSON format. All it takes is some simple tweaking of spring configuration file to map data to JSON format.

Lets code !!!

Our example has as Order object composed of Product and Customer . The REST application will take orderId as input and give out the corresponding order object in JSON format . Here are classes :

Product.java

Customer.java

OrderController.java

The OrderController class is Spring MVC Controller class which takes request with orderId and returns Order object . Note that this class does not has any JSON related configuration or code to map the order response to JSON format data.

Spring Configuration – orderInformation-servlet.xml

The real magic happens in this spring mvc configuration file . This file has the necessary configuration to map the response to JSON format.

What we have done is that configured RequestMappingHandlerAdapter bean with jsonMessageConvertor. This bean does all the work to map the response to JSON format.

Here is the pom used :

 

That’s it !!! . This is all you need to configure your Spring MVC application to send JSON formatted data.

As always, please send your feedback to codereq1@gmail.com

Strategy Design Pattern in Java

Strategy Design Pattern

Strategy Design Pattern is useful when there are family of algorithms and each algorithm is used in different scenarios. Strategy design pattern enables changing the algorithm independent of the client application that is using it.

The basic idea behind Strategy design pattern is to encapsulate each algorithm and use interfaces .

Let us assume that there is a client application which uses number of ways to store some data. It could use file system, some database or in-memory.  Here algorithm is storing data in different repositories like file system, database or in-memory.  We will how strategy design pattern can be used so that client can use different strategy to store data . Here is the code .

Define an interface, Repository ,which defines one method :

Next we encapsulate different algorithms to store data in each class :

InMemoryRepository :

FileRepository:

We have two implementations of Repository interface :

  • InMemoryRepository : This class encapsulates method to store data in HashMap.
  • FileRepository : This class encapsulates method to store data in some file.

Here we have two strategies for storing data and client application would use them at run time.

The client application code is given below :

Here the client application creates to sets of data and uses different strategy to persist them. In other scenarios the client could use some logic to decide which strategy to use. For example :

In this client uses run time logic to check if the data is more than 10000 characters long and then uses strategy accordingly.

Lambda Expression without Formal Paramaters

Java Lambda Expression with No Formal Parameters

Interface

Implementation Class :

Note the use ()-> for lambda expression without formal parameters.

Hibernate Collection Mapping – Mapping a Set

Overview

This post describes how Hibernate Collection mapping to a table can be achieved for a class having Set of attributes. The domain object is a Product which has various features modeled as Set . Our objective is to map this domain object to tables in relational database.

Hibernate Collection Mapping – Define domain object

Our domain object , Product, has prod_feature which is modeled as Set .

Product.java

Hibernate Collection Mapping : Mapping Product to table

To map Product class to a table in database we will configure a product.hbm.xml file. This configuration file will have all the information needed by hibernate to map Product to Product table in database.

 


hibernate.cfg.xml


ProductService.java

spring-xml

Marshalling and Unmarshalling XML using Spring

Introduction – Marshalling and Unmarshalling XML using Spring

This article illustrates Spring’s support for Object/XML mapping. Mapping XML to Object is called unmarshalling and mapping an object to XML is called Marshalling. Marshalling and Unmarshalling XML using Spring is supported by Spring’s OXM module. Spring provides an extensible , easy and simple way to use various O/X framework. By using Spring provided interfaces, one can switch between different O/X framework by a simple change in configuration.

 
Continue reading

Command Design Pattern in Java

Command Design Pattern

In this article we will see the application of command design pattern and problem domain it tries to solve.

Definition :

Command design pattern is a behavioral design pattern which encapsulates a request as an object. In command design pattern all the information required to invoke a method is encapsulated.

Continue reading