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.

52 comments:

  1. Thanks a lot.Great article for your useful information.hadoop training in chennai
    oracle training in chennai

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


  3. 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

  4. 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
  5. 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
  6. Iam really thankful to you for sharing this codes, Very nice seo training in vijayawada

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

  10. 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
  11. 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

  12. 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
  13. 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
  14. 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
  15. it is really amazing...thanks for sharing....provide more useful information...
    Mobile app development company

    ReplyDelete
  16. 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
  17. 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
  18. 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
  19. Through your programming I have got some clarification. Thanks to shared your knowledge with us.
    Oracle dba training syllabus | DBA training

    ReplyDelete
  20. 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
  21. Hi there I am so thrilled I found your website, I really found you by mistake, while I was browsing on Yahoo for something else, Anyhow I am here now and would just like to say thanks a lot for a tremendous post and an all-round exciting blog (I also love the theme/design), I don’t have time to go through it all at the minute but I have saved it and also added in your RSS feeds, so when I have time I will be back to read more, Please do keep up the awesome job. Digital Marketing Training in chennai

    ReplyDelete
  22. Those guidelines additionally worked to become a good way to recognize that other people online have the identical fervor like mine to grasp great deal more around this condition.AWS Training in chennai

    ReplyDelete
  23. Thank you Admin!
    1 crore projects center specializes in Android Training in chennai

    ReplyDelete
  24. Really Great and useful piece of information.Thanks for sharing best Java Training in Chennai | Android Training in chennai

    ReplyDelete
  25. I feel really happy to have seen your webpage and look forward to so many more entertaining times reading here. Thanks once more for all the details.
    Python online training

    ReplyDelete
  26. It seems you are so busy in last month. The detail you shared about your work and it is really impressive that's why i am waiting for your post because i get the new ideas over here and you really write so well.

    java training in annanagar | java training in chennai

    java training in marathahalli | java training in btm layout

    java training in rajaji nagar | java training in jayanagar

    ReplyDelete
  27. When I initially commented, I clicked the “Notify me when new comments are added” checkbox and now each time a comment is added I get several emails with the same comment. Is there any way you can remove people from that service? Thanks.

    AWS Training in Bangalore | Amazon Web Services Training in Bangalore

    Amazon Web Services Training in Pune | Best AWS Training in Pune

    AWS Online Training | Online AWS Certification Course - Gangboard

    ReplyDelete
  28. Great Article...Thanks for sharing the best information.It was so good to read and useful to improve my knowledge as updated one.

    Android Training
    Android Training in Chennai

    ReplyDelete
  29. I just needed to record a speedy word to express profound gratitude to you for those magnificent tips and clues you are appearing on this site.
    fire and safety course in chennai

    ReplyDelete
  30. Great post and informative blog.it was awesome to read, thanks for sharing this great content to my vision.
    Good discussion.
    Six Sigma Training in Abu Dhabi
    Six Sigma Training in Dammam
    Six Sigma Training in Riyadh

    ReplyDelete
  31. This is most informative and also this post most user friendly and super navigation to all posts... Thank you so much for giving this information to me.. 
    python online training
    python training in OMR
    python training course in chennai

    ReplyDelete
  32. Your very own commitment to getting the message throughout came to be rather powerful and have consistently enabled employees just like me to arrive at their desired goals.
    fire and safety course in chennai

    ReplyDelete
  33. Thanks for the good words! Really appreciated. Great post. I’ve been commenting a lot on a few blogs recently, but I hadn’t thought about my approach until you brought it up. 
    Devops training in sholinganallur
    Devops training in velachery
    Devops training in annanagar
    Devops training in tambaram

    ReplyDelete
  34. Amazon has a simple web services interface that you can use to store and retrieve any amount of data, at any time, from anywhere on the web. Amazon Web Services (AWS) is a secure cloud services platform, offering compute power, database storage, content delivery and other functionality to help businesses scale and grow.For more information visit aws online training

    ReplyDelete
  35. Amazing Article ! I have bookmarked this article page as i received good information from this. All the best for the upcoming articles. I will be waiting for your new articles. Thank You ! Kindly Visit Us @ Coimbatore Travels | Ooty Travels | Coimbatore Airport Taxi

    ReplyDelete
  36. Hello! This is my first visit to your blog! We are a team of volunteers and starting a new initiative in a community in the same niche.
    nebosh course in chennai

    ReplyDelete
  37. This is most informative and also this post most user friendly and super navigation to all posts... Thank you so much for giving this information to me.. 

    best rpa training in chennai | rpa online training |
    rpa training in chennai |
    rpa training in bangalore
    rpa training in pune
    rpa training in marathahalli
    rpa training in btm

    ReplyDelete
  38. Really I Appreciate The Effort You Made To Share The Knowledge. This Is Really A Great Stuff For Sharing. Keep It Up . Thanks For Sharing.

    React Training
    React Training in Chennai

    ReplyDelete
  39. Information from this blog is very useful for me, am very happy to read this blog Kindly visit us @ Luxury Watch Box | Shoe Box Manufacturer | Luxury Cosmetics Box

    ReplyDelete
  40. Amazing article. Your blog helped me to improve myself in many ways thanks for sharing this kind of wonderful informative blogs in live. I have bookmarked more article from this website. Such a nice blog you are providing ! Kindly Visit Us @ Best Travels in Madurai | Tours and Travels in Madurai | Madurai Travels

    ReplyDelete