Showing posts from January, 2010

Spring-Hibernate Maven project- Part 3

Hibernate Configuration and Project Deployment

In the previous post I created the hibernate mapping files for the Person-Address domain classes. In here we are going to create the hibernate configuration file which creates the database connection according to the defined URL, credentials and other properties.

On the deplyment of the project, the hibernate configuration file is executed through the commands in pom.xml.

We have to put the hibernate configuration file inside the src/main/resources directory. For that right cick on the directory, select new, select other. From there expand the hibernate icon and select Hibernate Configuration file (cfg.xml).

Click Next

Keep the default name "hibernate.cfg.xml" same and click Next
Give the values in the appeared window give the values as follows.

Make sure to verify your mysql username and password, and enter the corret one according to your mysql configuration.
<hibernate-configuration> <session-fa…

Spring-Hibernate Maven project- Part 2

Hibernate Mapping

Here the two domain classes have one-to-many relationship. One Person can have many Addresses while one Adress can have only one person. So there is a one-to-many relationship between Person and Address.

In hibernate we have to create the mapping file for each domain class. The mapping file represents the database table which is mapped with the domain class. The table will be created according to things indicated inside mapping classes.

Mapping file for - Person.hbm.xml
Mapping file for - Address.hbm.xml

Mapping files are located inside src/main/resources. If you have configured Hibernate to be used inside Eclipse you can simply use it. Otherwise let's configure eclpse for hibrenate.

Hibernate-Eclipse Configuration 

Download Hibernate tools from here.
Extract the file and put all the files inside the features folder into the features folder of the eclipse installation directory and put all the files inside the plu…

Spring-Hibernate Maven project- Part 1

Here the maven project created in last post, will be extended to add new Persons in to system who has multiple Addresses.

Add the Spring Hibernate dependencies into pom.xml file inside project tags, after the version tag.
<project xmlns:xsi="" xmlns="" xsi:schemalocation=""> <modelVersion>4.0.0</modelVersion> <groupId>org.test.testapp</groupId> <artifactId>testapp</artifactId> <version>0.0.1-SNAPSHOT</version> <dependencies> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-core</artifactId> <version>3.3.1.GA</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring</artifactId> <version>${springVer…

Create a Simple Maven Project

Open Eclipse.
Create a new maven project. File--New--Other
New Wizard will open. There go to maven icon and expand it. Then Select Maven Project item.

Click Next.
In the New Maven Project wizard, select the create a simple project (skip archetype selection) check box.

Click Next
In the New Maven Project wizard enter the following values.

Group Id: org.test.testapp
Artifact Id: testapp
Version: 0.0.1-SNAPSHOT
Packaging: jar

Installing Apache Maven and Configuring Eclipse with Maven

Installing Maven into your PC - Windows

Download Maven from here.MavenDownload Unzip the file and put into Program files. So the Maven path would be C:\Program Files\apache-maven-2.2.1
Setting the Maven Environment Varable

Right click My Computer Go to advanced tab Click “Environment Variables” button In the System variables click new button. Set the path to Maven directory and give M2_HOME as variable name as follows.
Click ok. Now from the System Variable set select “Path” variable and click edit. Add the ;% M2_HOME%/bin; at the end of path value as follows.
Click ok. Verify the Installation Open command line and type mvn command.

Installing Maven into your PC - Ubuntu

Unzip maven archive in to what ever location and set the path using one of below option.

Using Command permanent:

sudo ln -s /your/maven/location/bin/mvn /usr/bin/mvn

Option Two Temporary:

export M2_HOME=/your/maven/location/
export M2=%M2_HOME%\bin

Eclipse-Maven Configuration

Note that for Eclipse Update for Maven does not require Ap…

Thread Synchronization

When multiple threads are accessing a common method through their run methods, the output of that particular program is unpredictable, because while one thread is gone half way through the common method, another thread would gain the common method.

Similarly the common method execution result of multiple threads can have a variety and also may attack to the accuracy of the expected result.

If you need to let the common method accessible by one thread at one time you have to done it through synchronization.

Imagine you expect to increase the value of number in Thread A, and the value of number Thread B by one via calling the common method count().

Examine the following code and see the result by executing it.

class SynchDemo{ int common=7; public static void main(String args[]){ SynchDemo demo=new SynchDemo(); A a =new A();; a.start(); B b=new B();; b.start(); } public int count(in…

Thread join() method

method-void join() class-java.lang.Thread non-static throws Interrupted Exception
The method join can be used to make one thread joined into the end of another thread. This is a guranteed method.

Examine the following code and see the result.

class Demo{ public static void main(String args[]){ A a=new A(); B b=new B(); a.start(); b.start(); } } class A extends Thread{ public void run(){ System.out.println("Thread A started"); System.out.println("Thread A running"); System.out.println("Thread A ending"); } } class B extends Thread{ public void run() { System.out.println("Thread B started"); System.out.println("Thread B running"); System.out.println("Thread B ending"); } } Result can not be guessed, as two threads execute separately.

Imagine you want to execute Thread-B after the complete execution of Thread-A…