Search This Blog

Monday, 5 September 2011

Fleshing out the Models (The java ones :P) - 1

The way people work is that create the database separately and then create their hibernate model classes to work with the tables in the sql.
I decided to follow the process found in tutorials on the net and in the book.
  1. Analyze and identify the domain models.
  2. Create the class that would represent these objects in java.
  3. Create the OR mapping that would link the Java Models with the database Tables.
  4. Let Hibernate generate the DDL and worry about all related considerations.
This frees me from having to write any CREATE ( and most surely ALTER) statements and simply focus on the completeness of my Domain Models. (This is actually based on the database we saw in the previous post)
The Owner Class :
package com.menagerie.domain.entity;

import java.util.Date;
import java.util.HashSet;
import java.util.Set;

public class Owner {
    // This is used by hibernate as the Identifier
    private Long id; 
    private String name;

    // This can represent a user uniquely (and cannot be changed)
    private String userId;

    // Every owner has a collection of pets ( One To Many Relation)
    private Set<Pet> pets; 
    
    //The below are audit fields
    private long modifiedBy;
    private long createdBy;
    private Date createdDate;
    private Date modifedDate;
    
    /*
     * As this could be null, the wrapper object has been used
     * 
     */
    public Long getId() {
        return id;
    }
    
    /*
     * As this will be set by Hibernate and not generated in the application,
     * the method has been marked private
     */
    private void setId(Long id) {
        this.id = id;
    }
    
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getUserId() {
        return userId;
    }
    public void setUserId(String userId) {
        this.userId = userId;
    }
    public long getModifiedBy() {
        return modifiedBy;
    }
    public void setModifiedBy(long modifiedBy) {
        this.modifiedBy = modifiedBy;
    }
    public long getCreatedBy() {
        return createdBy;
    }
    public void setCreatedBy(long createdBy) {
        this.createdBy = createdBy;
    }
    public Date getCreatedDate() {
        return createdDate;
    }
    public void setCreatedDate(Date createdDate) {
        this.createdDate = createdDate;
    }
    public Date getModifedDate() {
        return modifedDate;
    }
    public void setModifedDate(Date modifedDate) {
        this.modifedDate = modifedDate;
    }
    
    public void setPets(Set<Pet> pets) {
        this.pets = pets;
    }

    public Set<Pet> getPets() {
        return pets;
    }    
    
    public void addPet(Pet pet) {
        if ( null == pet) {
            throw new IllegalArgumentException("Null Pet not supported !!");
        } 
        pet.setOwner(this);
        if (null == this.pets) {
            this.pets = new HashSet<Pet>();
        }
        this.getPets().add(pet);
    }
    
    @Override
    public String toString() {
        return "(Owner : [name: " + this.getName() + " ], [id:  " + this.getId() + "])";
    }
    
    @Override
    public boolean equals(Object obj) {
        //The equals method here does not depend on the Db Identifier
        boolean equal = false;
        if (obj instanceof Owner) {
            Owner otherOwner = (Owner) obj;
            equal = this.getUserId().equals(otherOwner.getUserId());
        }
        return equal;
    }
    
    @Override
    public int hashCode() {        
        return this.getUserId().hashCode();
    }
    
}
Next is the Pet class.
The Pet Class:
package com.menagerie.domain.entity;

import java.util.Date;

public class Pet {
    private Long id; // the Db identifier
    private String name;
    private int age;
    private String tagId; // a unique identifier (that can be changed)
    private Owner owner; // a reference to the owning record
    
    //The below are audit fields
    private long modifiedBy;
    private long createdBy;
    private Date createdDate;
    private Date modifedDate;
    
    private void setId(Long id) {
        this.id = id;
    }
    public Long getId() {
        return id;
    }
    
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
    public String getTagId() {
        return tagId;
    }
    public void setTagId(String tagId) {
        this.tagId = tagId;
    }
    public Owner getOwner() {
        return owner;
    }
    public void setOwner(Owner owner) {
        this.owner = owner;
    }
    public long getModifiedBy() {
        return modifiedBy;
    }
    public void setModifiedBy(long modifiedBy) {
        this.modifiedBy = modifiedBy;
    }
    public long getCreatedBy() {
        return createdBy;
    }
    public void setCreatedBy(long createdBy) {
        this.createdBy = createdBy;
    }
    public Date getCreatedDate() {
        return createdDate;
    }
    public void setCreatedDate(Date createdDate) {
        this.createdDate = createdDate;
    }
    public Date getModifedDate() {
        return modifedDate;
    }
    public void setModifedDate(Date modifedDate) {
        this.modifedDate = modifedDate;
    }
    
    @Override
    public String toString() {
        //avoided any reference to Owner here as that could result in extra queries
        return "(Pet : [name: " + this.getName() + " ], [age:  " + this.getId()+ "])";
    }
    
    @Override
    public boolean equals(Object obj) {
        //The equals method here does not depend on the DB Identifier
        boolean equal = false;
        if (obj instanceof Pet) {
            Pet otherPet = (Pet) obj;
            equal = this.getTagId().equals(otherPet.getTagId());
        }
        return equal;
    }
    
    @Override
    public int hashCode() {        
        return this.getTagId().hashCode();
    }
    
}
This completes the Java side.

4 comments:

  1. I have read your blog its very attractive and impressive. I like it your blog.

    Java Training in Chennai Java Training in Chennai | Core Java Training in Chennai

    Online Java Training Online Java Training | Java J2EE Online Training | JavaEE Training Institute in Chennai

    ReplyDelete
  2. You have provided an nice article, Thank you very much for this one. And i hope this will be useful for many people.. and i am waiting for your next post keep on updating these kinds of knowledgeable things...

    Android App Development Company

    ReplyDelete
  3. I am always eager to catch hold of the new posts being published on your website, because of this i use to updated, thanks for sharing this wonderful article.
    Digital Marketing Company in Delhi
    SEO Company in Delhi
    Mobile App Development Company in Lucknow

    ReplyDelete
  4. Thanks for providing such a great Information, you can see, we also provide
    Peptide Synthesis

    ReplyDelete