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="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0" xsi:schemalocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<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>${springVersion}</version>
 </dependency>
</dependencies>
</project>
Dependencies are added, so that once the project install goal is being executed the, needed libraries are added to the project. So we can access the spring and hibernate API's inside our project.

Now right click the project. Select Run As and then Select maven install. Or go to project location through command line and execute mvn install command. If no errors were found, and you got BUILD SUCCESSFULL, then you have added the dependencies successfully.

Let's add domain classes to the project. The domain is consist of Person and Address classes.
In maven project directory structure, all the source files will be located in src/main/java directory.

Right click on the src/main/java directory and clike New--Class

Give Person as the class name and org.test.testapp.domain as the package name.



Now create the Address class inside the same package.


Put the following code inside Address.java.


package org.test.testapp.domain;

public class Address {
    private Long AddressID;
    private String Street;
    private String City;
    private String AreaCode;
    private Person NewPerson;
   
    public Long getAddressID() {
        return AddressID;
    }

    public void setAddressID(Long addressID) {
        AddressID = addressID;
    }
    public Person getNewPerson() {
        return NewPerson;
    }

    public void setNewPerson(Person newPerson) {
        NewPerson = newPerson;
    }

    public String getStreet() {
        return Street;
    }
   
    public void setStreet(String street) {
        Street = street;
    }
   
    public String getCity() {
        return City;
    }
   
    public void setCity(String city) {
        City = city;
    }
   
    public String getAreaCode() {
        return AreaCode;
    }
   
    public void setAreaCode(String areaCode) {
        AreaCode = areaCode;
    }
  
}

Now put the following code inside Person.java

package org.test.testapp.domain;

import java.util.List;

public class Person {
    private Long PersonID;
    private String Name;
    private String TelNo;
    private List AddressList;
   
    public Long getPersonID() {
        return PersonID;
    }
    public void setPersonID(Long personID) {
        PersonID = personID;
    }
   
    public String getName() {
        return Name;
    }
    public void setName(String name) {
        Name = name;
    }
    public String getTelNo() {
        return TelNo;
    }
    public void setTelNo(String telNo) {
        TelNo = telNo;
    }
    public List
getAddressList() {
        return AddressList;
    }
    public void setAddressList(ListaddressList) {
        AddressList = addressList;
    }
   
}
 



Note: Here the both classes consist of their attributes and the getter setter methods for each attribute. For implemet the hibernate connction to thses classes you must follow this coding standard where attributes kept private and getter setter methods fr each attribute kept public.  


Here is the new structural view of the project after adding 2 domain classes.






In the next part let's see how to implement the hibernate mapping classes for these domain classes.
Post a Comment

Popular posts from this blog

Yield Price Sri Lanka - Android Application

Manage SOAPAction of the Out Message

How to clean Registry log (REG_LOG) table