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.
The Owner Class :I decided to follow the process found in tutorials on the net and in the book.
- Analyze and identify the domain models.
- Create the class that would represent these objects in java.
- Create the OR mapping that would link the Java Models with the database Tables.
- Let Hibernate generate the DDL and worry about all related considerations.
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.
Best post thanks for sharing
ReplyDeletebest java training institute in chennai
This comment has been removed by a blog administrator.
ReplyDeleteThis comment has been removed by a blog administrator.
ReplyDeleteThis comment has been removed by a blog administrator.
ReplyDeleteThis comment has been removed by a blog administrator.
ReplyDeleteHi, Thanks for sharing nice articles....
ReplyDelete<a href="https://www.dnaglobal.in”>Best CA firms in india</a>
Thank for very nice this sharing.
ReplyDeleteThank you very nice sharing.
ReplyDeleteDiscover the power of #ITIO white label payment gateway services, designed to elevate your business transactions. Our cutting-edge technology ensures secure and efficient payment processing, tailored to match your brand seamlessly. Say goodbye to integration hassles and hello to a fully customizable, reliable payment solution.
ReplyDeleteThank you very nice sharing.
ReplyDelete