Posts

Showing posts from 2013

Hibernate Tutorial - Part V

For previous posts, refer:
Hibernate tutorial Part - I
Hibernate tutorial Part - II
Hibernate tutorial Part - III

Hibernate tutorial Part - IV

In this post, we are going to take a look at ' Table per subclass' approach for inheritance.


Table per subclass:This strategy, represents inheritance relationships as relational foreign key associations.Every class/subclass that declares persistent properties, including abstract classes and even interfaces has its own table.
Unlike the table per concrete class strategy, the table here contains columns only for each non inherited property (each property declared by the subclass itself) along with a primary key that is also a foreign key of the super class table.
Lets take a look at the class definitions and annotations:
User Class:@Entity @Table(name = "USER") @Inheritance(strategy=InheritanceType.JOINED) public class User implements Serializable{ @Id @Column(name = "USER_ID") private String userId; …

Hibernate Tutorial - Part IV

For previous posts refer:
Hibernate tutorial Part - I
Hibernate tutorial Part - II
Hibernate tutorial Part - III

In this post, we will take a look at 'Table per class hierarchy' strategy of inheritance.


Table Per Class hierarchy:In this approach, an entire class hierarchy can be mapped to a single table. This table includes columns for all properties of all classes in the hierarchy. The concrete subclass represented by a particular row is identified by the value of a type discriminator column.

Lets take a look at the classes and their annotations:
User Class:@Entity @Table(name = "USER") @Inheritance(strategy=InheritanceType.SINGLE_TABLE) @DiscriminatorColumn( name="USER_TYPE", discriminatorType=DiscriminatorType.STRING ) @DiscriminatorValue(value="U") public class User implements Serializable{ @Id @Column(name = "USER_ID") private String userId; @Column(name = "USERNAME") private String use…

Hibernate Tutorial - Part III

For previous parts refer:
Hibernate tutorial Part - I
Hibernate tutorial Part - II


Inheritance Mapping:In this post, I'm going to write about Inheritance mapping in Hibernate.
Essentially, there are 3 different types of inheritance mapping which could be achieved:
Table per concrete class Table per class hierarchyTable per subclass1. Table per concrete class In this type of mapping, we will have a table mapped to every concrete class in the hierarchy. Lets say we have the following class hierarchy for representing different users of the system.
Abstract class, UserConcrete subclass, CustomerConcrete subclass, InternalUserLets take a look at the class definitions and the the annotations for each of these classes.

User Class:@Entity @Table(name = "USER") @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS) public class User implements Serializable{ @Id @Column(name = "USER_ID") private String userId; @Column(name = "USERNAME"…

Hibernate Tutorial - Part II

For part I refer:
Hibernate tutorial Part - I

In this post we will see how the entity mapping is done. Lets take an example of a library management application for simplicity and take a look at the various possible mappings.

Lets say we need to create an entity 'Book'. We will create a Book POJO. This will be mapped to a table named 'Book'. The book table will have the following columns.

BOOK_ID (Primary Key, Type: Number) driven by sequence book_id_seqBOOK_NAME (Type: Varchar2(50)PUBLISH_DATE (Type: Date)BOOK_PREVIEW (Type: clob)The Book class with entity annotations will be as below:

@Entity @Table(name="book") @org.hibernate.annotations.Entity( dynamicUpdate = true ) public class Book implements Serializable{ private static final String BOOK_SEQUENCE = "book_id_seq"; @Id @Column(name="BOOK_ID") @SequenceGenerator( name=BOOK_SEQUENCE, sequenceName=BOOK_SEQUENCE) @GeneratedValue(strategy=Ge…

Hibernate Tutorial - Part I

Overview:Hibernate is an Object relational mapping (ORM) tool which will help us map Java objects to the underlying database objects. It will help us perform various operations on objects and hibernate will translate it to the DBMS operations and saves us from writing sql queries.
An equally important aspect is the importance of object orientated principles which need not be compromised when we use Hibernate. If we go with plain old sql style of design, then due to the disconnect between the object oriented design and the DBMS design, often the object oriented design is compromised.
For instance, concepts like inheritance, composition do not have a corresponding representation in the DBMS world. Similarly, we cannot truly represent a bi-drectional navigation and mapping between two entities (say for eg, Author and Book) in the DBMS world. These short comings could be overcome if we use Hibernate and we can minimize the influence of DBMS design on the object oriented design.
When we use H…

Building Webservices using Spring

In this post, I'm going to explain how to write a contract first webservice using Spring.

We are going to create a service for creating a person record. We are going to call the service as 'CreatePersonService'. It will accept first name, middle name and last name as parameters and return a id parameter back as a response.

We need to first define the XSD representing the person record. Also, we need an XSD to represent the request to create the Person record. 

Let us first start with the maven dependencies required:

pom.xml:We will be using jaxb for generating classes from xsd.
<dependency> <groupId>org.springframework.ws</groupId> <artifactId>spring-ws-core</artifactId> <version>2.1.3.RELEASE</version> <type>jar</type> <scope>compile</scope> </dependency> <dependency> <groupId>org.dom4j</groupId> <artifactId>dom4j</artifactId> <ve…

Popups using Javascript and CSS

Image
In this post, I'm writing about an interesting way of displaying popups using javascript and css.

These popups do not show up in a new window, but rather in the same page as a different box. You might have seen these kind of popups while logging in or during display of your profile details in various social networking sites or email websites like gmail.

The idea here is to use css and div to display a box which embeds a separate html form of its own.
Lets first create a the form elements for login:

<div id="popupbox"> <form name="login" action="" method="post"> <div id="centertag">Username:</div> <div id="centertag"><input name="username" size="14" /></div> <div id="centertag">Password:</div> <div id="centertag"><input name="password" type="password" size="14" /></div> <a href="…

Site Map

Spring Beans Autowiring

In this post I'm going to write about Autowiring feature in Spring.


Overview:Generally, in Spring, you will define the beans and inject other beans into their properties by configuring them in xml. For eg, if we have a DAO, UserDAO and a service class UserService, which has a property reference for the DAO, then, the bean configuration would look like this: <bean id="userDAO" class="com.myorg.service.dao.UserDAO"> </bean> <bean id="userSvc" class="com.myorg.service.UserService"> <propertyname="userDAO" ref="userDAO"/> </bean>
Spring provides a feature called Autowiring, wherein the decision to wire the bean properties could be left to the Spring framework and this saves us a lot of configuration hassle.

There are four types of autowiring:
byNamebyTypeconstructorautodetect1. Autowiring - byName:In this type of wiring, Spring attempts to find the beans which has the same name/id as that of t…

Spring AOP - Part III

For part I, refer to: Spring AOP - Part I
For part II, refer to: Spring AOP - Part II

Continuing further on AOP examples, in this post, we will see how can we pass arguments to the Aspect classes.


Passing parameters to Aspect classes:In this example, we will write a very basic auditing service, which will store audit details of who accessed a particular service.
The audit details could be stored in database. But that part is not shown here to keep the details simple.

Let us say we have an Account service, which offers CRUD on the account. The username parameter which is the first parameter for all the methods, indicates the user who will perform a CRUD on the account.
public interface AccountService { public long createAccount(String username, Account account); public void updateAccount(String username, Account account); public void deleteAccount(String username, Account account); }
Define a corresponding AccountServiceImpl which will actually perform the CRUD. (not shown here).

Spring AOP - Part II

For Part -I, refer blog: Spring AOP-Part I

Let me give a simple example of AOP to add logging service.
Pom.xml<dependency> <groupId>cglib</groupId> <artifactId>cglib</artifactId> <version>2.2</version> </dependency> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version>1.6.11</version> </dependency> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjrt</artifactId> <version>1.6.8</version> </dependency> <!-- logging --> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>1.7.5</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>jcl-over-slf4j</artifactId> <version>1.7.5</version> …

Spring AOP - Part I

In this blog I'm going to write about AOP using Spring. 
Overview:AOP which stands for Aspect Oriented Programming aims at separating out cross cutting concerns from the normal programming logic. This makes way for cleaner application code can focus on business logic and the other concerns like logging, security etc will be handled by the aspects.

Illustration:To give a perspective of how AOP works, let us consider the following example:
Let us say, in a big city, there are no proper sign boards. So the city Mayor decides to take up a project to install sign boards. This project is like a cross cutting concern in some way. 

The city corporation will define certain rules to install the sign boards. 
Sign board rules:
A sign board indicating 'school nearby' should be placed 5 meters from the school in both directions. A sign board indicating a sharp curve should be placed 5 meters before the curve. A sign board 'Thank you. Visit again' should be placed after the city exit.…

Proxy Pattern

In this blog, I'm going to write about Proxy pattern and it's applications.


Definition:Proxy is a Structural design pattern which works 'on behalf of' or 'in place of' another object in order to access the later.
Here proxy objects are used to perform the work on somebody's behalf. We will use proxy objects in the following scenarios:
Lot of boilerplate code is required in order to invoke the real object.The access to the real object is limited and all the access needs to go through the proxy.The real object is a remote objectIllustration:Let us take an example.
Lets say there is a King who lives in a palace and he has all kind of antique jewelry, paintings etc. John has a hobby of collecting antique items and he wants to purchase some antique items from the king. 
But the king will not part with his antique pieces so easily. He misses his good old glorious days in this modern era and hence the person who wants to purchase has to sing and praise the qualities of…