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.

25 comments:

  1. Your blog is really useful for me. Thanks for sharing this informative blog. If anyone wants to get real time Oracle Training in Chennai reach FITA Oracle Training Institutes in Chennai. They give professional and job oriented training for all students.

    ReplyDelete


  2. The information you posted here is useful to make my career better keep updates..If anyone want to become an oracle certified professional reach FITA Oracle Training Institutes in Chennai, which offers Best Oracle Training in Chennai with years of experienced professionals.

    ReplyDelete

  3. Your posts is really helpful for me.Thanks for your wonderful post. I am very happy to read your post.
    PHP Training in chennai | PHP Training chennai | PHP course in chennai | PHP course chennai

    ReplyDelete
  4. There are lots of information about latest technology and how to get trained in them, like Hadoop Training Chennai have spread around the web, but this is a unique one according to me. The strategy you have updated here will make me to get trained in future technologies(Hadoop Training in Chennai). By the way you are running a great blog. Thanks for sharing this. FITA chennai reviews

    ReplyDelete
  5. Iam really thankful to you for sharing this codes, Very nice seo training in vijayawada

    ReplyDelete
  6. It’s too informative blog and I am getting conglomerations of info’s. Thanks for sharing; I would like to see your updates regularly so keep blogging. If anyone looking car just get here
    Regards,
    sas training in Chennai|sas course in Chennai|sas institutes in Chennai

    ReplyDelete
  7. Hi Dude,
    Awesome Post!!! With unique content, I really get reading interest when I am following your article, I hope I ll help many of them who looking this pretty information.
    Regards,
    Oracle DBA Training in Chennai|Oracle Training Institute in Chennai|Best Oracle Training Institute in Chennai

    ReplyDelete
  8. 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

  9. Thanks for posting useful information.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...Really it was an awesome article...very interesting to read..
    please sharing like this information......
    Android training in chennai
    Ios training in chennai

    ReplyDelete
  10. It's interesting that many of the bloggers your tips helped to clarify a few things for me as well as giving.. very specific nice content. And tell people specific ways to live their lives.Sometimes you just have to yell at people and give them a good shake to get your point across.
    Web Design Company
    Web Development Company
    Mobile App Development Company

    ReplyDelete

  11. Really it was an awesome article...very interesting to read..You have provided an nice article....Thanks for sharing..
    Web Design Company
    Web Development Company

    ReplyDelete
  12. 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
  13. great and nice blog thanks sharing..I just want to say that all the information you have given here is awesome...Thank you very much for this one.
    Web Design Development Company
    Web design Company in Chennai
    Web development Company in Chennai

    ReplyDelete
  14. it is really amazing...thanks for sharing....provide more useful information...
    Mobile app development company

    ReplyDelete
  15. Being new to the blogging world I feel like there is still so much to learn. Your tips helped to clarify a few things for me as well as giving..
    Fitness SMS
    Salon SMS
    Investor Relation SMS

    ReplyDelete
  16. 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...
    iOS App Development Company

    ReplyDelete
  17. These ways are very simple and very much useful, as a beginner level these helped me a lot thanks fore sharing these kinds of useful and knowledgeable information.
    Texting API
    Text message marketing
    Digital Mobile Marketing
    Sms API
    Sms marketing

    ReplyDelete
  18. Through your programming I have got some clarification. Thanks to shared your knowledge with us.
    Oracle dba training syllabus | DBA training

    ReplyDelete
  19. These ways are very simple and very much useful, as a beginner level these helped me a lot thanks fore sharing these kinds of useful and knowledgeable information.

    snapho

    ReplyDelete
  20. Excellent article. Very interesting to read. I really love to read such a nice article. Thanks! keep rocking.

    Python Training in Chennai
    AngularJS Training in Chennai
    Best AngularJS Training in Chennai

    ReplyDelete