Posts

JPA Part2: Identifier Generation (PK)

Image
There are four primary key generators. GenerationType.AUTO GenerationType.IDENTITY GenerationType.SEQUENCE GenerationType.TABLE AUTO In this case provider will use whatever the strategy support by the database. For example, MySQL 5.6 use the SEQUENCE table as follows CREATE TABLE \ ` sequence\ ` ( \ ` SEQ\_NAME\ ` varchar ( 50 ) NOT NULL , \ ` SEQ\_COUNT\ ` decimal ( 38 , 0 ) DEFAULT NULL , PRIMARY KEY ( \ ` SEQ\_NAME\ ` ) ) ENGINE = InnoDB DEFAULT CHARSET = utf8 ; As such, provider can decide the strategy. The above table is auto generated. IDENTITY Some databases support identity column referred to as auto-number where row is inserted in to the table, the identity column will get a unique identifier. Databases like MySQL 5.6 support this but not all databases. However, this is not efficient way to identity generation because identifier is not available until after the commit, therefore provider need to reread the record again after the p...

JPA Part 1: Embedded Classes

Image
Embedded classes are important because they are reusable via composition. For example in the following diagram, I have two Embeddable classes Contact Address Here, Contact is embedded in the Employee, Address is embedded in the Contact. That means embeddable classes can embed other classes. As shown in the above diagram, there is only one table is mapped to the entire class hierarchy. Here the person class package au . com . ojitha . blogspot . jpaex1 . domain ; import javax . persistence . Column ; import javax . persistence . MappedSuperclass ; / \ * \ * \ * \ * @author Ojitha \ * / @MappedSuperclass public class Person { @Column ( name = "FIRST\_NAME" ) private String firstName ; @Column ( name = "LAST\_NAME" ) private String lastName ; public String getFirstName ( ) { return firstName ; } public void setFirstName ( String firs...

Spring 3 Part 8: Spring Hibernate open session in view

Image
This is the 8th part of the Spring 3 Series . In this blog I am going to use Hibernate 3.5.2 to explain the use of Spring Transaction which fulfil the requirements of open session in view pattern. First understand some important concepts in Hibernate. First of all, it is very important to understand the object states in the Hibernate. Transient object will become a persistent object in the context of active session. This persistent object represent one row of the data table. There can be two transient object references which are not refer to the same object but can hold same data. These objects are identical only in the persistence context (persistent objects are identical) because persistence objects are identified based on the database primary key. Therefore, persistence object identifier is the same as data table identifier.  For the transient and detached object, equal() and hashcode() are the methods to be implemented to define the identifier. Figur...

Spring 3 Part 7: Spring with Databases

Image
This is the 7 th part of the Spring 3 Series . Create MySQL 5.6 database ‘payroll’, CREATE DATABASE payroll; Next create a user for example, user ‘ojitha’ CREATE USER 'ojitha'@'localhost' IDENTIFIED BY 'ojitha'; Need to grant the ‘payroll’ access to user ‘ojitha’ as shown in the following. GRANT SELECT, INSERT, UPDATE, DELETE, CREATE, DROP ON payroll.* TO 'ojitha'@'localhost'; You can download freeware of TOAD for MySQL from here . Let create a first table, that contains all the states in Australia. You can create a table using visually using the icon given directly below the Tables tab in the Object Explorer as shown in the above diagram. CREATE TABLE payroll.STATE ( CODE CHAR(3) ASCII COMMENT 'State code', STATE VARCHAR(30), PRIMARY KEY (CODE) ) ENGINE = InnoDB COMMENT = 'Australian states' ROW_FORMAT = DEFAULT; In the my sql console you can the newly added table using the following command SHOW ...

How To: GitHub projects in Spring Tool Suite

Image
The first step is sign up in the GitHub and create a  new project. For example I’ve created a new project Spring3part7 in the GitHub. Before add the projects you need to configure STS for GitHub access. For example, you need to add configuration as shown in the following picture Git local repository also important Now you are ready to pull the project from the GitHub, in the STS import menu select the following Now type the project name and click the ‘Search’ as shown in the following. However, when you select the found project click ‘Finish’ nothing will change in the STS interface. Now you are going to create real project. Here I am going to create simple utility project. This template project need to be created inside the imported GitHub project You have to give the same project name of the GitHub project as shown in the following project Now your project is ready to push. Before that you need to add ignore flag to all the folders and files except pom...

Spring 3 part 6: Spring AOP

Image
This is the 6th part of the Spring 3 Series . AOP Concepts Joinpoint : Joinpoint is the point in an application at which additional logic can be inserted. For example method invocation that is the only Spring support joinpoint type. Advice : Advice is executed at a particular joinpoint in an application. NOTE: no advices for final classes. All the advisors need to implement the Advisor interface. There are two kind of advisors available in the Spring: IntrodcutionAdvisor and PointcutAdvisor. Spring defines 6 difference advices: before(MethodBeforeAdvice),  after returning (AfterReturningAdvice), after(AfterAdvice), around (MethodInterceptor), Throws (ThrowsAdvice), and Introduction (IntroductionInterceptor). Pointcuts : A pointcut is a collection of joinpoints that define when advice should be executed. For example, collection of all method invocations in a particular class. Aspect : An aspect is the combination of advice and pointcuts. This combination immerge t...