I wrote a generic DAO and two DAOs extending from it. I'm new at this pattern so I'm not sure if I'm doing this correctly. It seems to work fine though. I am using Hibernate for database access.
Here is User
class:
@Entity
@Table(name="tbluser")
public class User {
private long userID;
private String email, password, firstName, lastName, middleName, addressNo, street, city, telNo, mobileNo;
private int gender, userType;
private LocalDate birthday;
private List<Reservation> reservations = new ArrayList<>();
private int status = 0;
public User() { }
public User(String email, String password, String firstName, String lastName, String middleName, int gender,
String addressNo, String street, String city, int userType, String telNo, String mobileNo, LocalDate birthday) {
this.email = email;
this.password = password;
this.firstName = firstName;
this.lastName = lastName;
this.middleName = middleName;
this.gender = gender;
this.addressNo = addressNo;
this.street = street;
this.city = city;
this.userType = userType;
this.telNo = telNo;
this.mobileNo = mobileNo;
this.birthday = birthday;
}
@Id @GeneratedValue
public long getUserID() {
return userID;
}
private void setUserID(long userID) {
this.userID = userID;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getMiddleName() {
return middleName;
}
public void setMiddleName(String middleName) {
this.middleName = middleName;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public int getGender() {
return gender;
}
public void setGender(int gender) {
this.gender = gender;
}
public String getTelNo() {
return telNo;
}
public void setTelNo(String telNo) {
this.telNo = telNo;
}
public String getMobileNo() {
return mobileNo;
}
public void setMobileNo(String mobileNo) {
this.mobileNo = mobileNo;
}
public String getAddressNo() {
return addressNo;
}
public void setAddressNo(String addressNo) {
this.addressNo = addressNo;
}
public String getStreet() {
return street;
}
public void setStreet(String street) {
this.street = street;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public int getUserType() {
return userType;
}
public void setUserType(int userType) {
this.userType = userType;
}
public LocalDate getBirthday() {
return birthday;
}
public void setBirthday(LocalDate birthday) {
this.birthday = birthday;
}
public int getStatus() {
return status;
}
public void setStatus(int status) {
this.status = status;
}
@OneToMany(cascade=CascadeType.ALL,mappedBy="user")
@OrderBy("checkInDate")
public List<Reservation> getReservations() {
return reservations;
}
public void setReservations(List<Reservation> reservations) {
this.reservations = reservations;
}
}
Here's the Reservation class:
@Entity
@Table(name="tblreservation")
public class Reservation {
private long reservationID;
private LocalDate checkInDate, checkOutDate;
private String roomType;
private int status = 2;
private User user;
public Reservation() { }
public Reservation(LocalDate checkInDate, LocalDate checkOutDate, String roomType, User user) {
this.checkInDate = checkInDate;
this.checkOutDate = checkOutDate;
this.roomType = roomType;
this.user = user;
}
@Id @GeneratedValue
public long getReservationID() {
return reservationID;
}
public void setReservationID(long reservationID) {
this.reservationID = reservationID;
}
public LocalDate getCheckInDate() {
return checkInDate;
}
public void setCheckInDate(LocalDate checkInDate) {
this.checkInDate = checkInDate;
}
public LocalDate getCheckOutDate() {
return checkOutDate;
}
public void setCheckOutDate(LocalDate checkOutDate) {
this.checkOutDate = checkOutDate;
}
public String getRoomType() {
return roomType;
}
public void setRoomType(String roomType) {
this.roomType = roomType;
}
public int getStatus() {
return status;
}
public void setStatus(int status) {
this.status = status;
}
@Transient
public String getStrCheckInDate() {
return getStringDate(checkInDate);
}
@Transient
public String getStrCheckOutDate() {
return getStringDate(checkOutDate);
}
@ManyToOne
@JoinColumn(name="userID")
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
}
I did not write an interface because the code seems to do fine without it. I could be wrong though.
Here is the generic DAO class.
// SessionFactory is initialised from a HibernateUtil class
// session management is in a servlet filter
public abstract class GenericDao<T> {
public void insert(T t) {
getSessionFactory().getCurrentSession().save(t);
}
public T get(Class<T> t, Long id) {
return (T)getSessionFactory().getCurrentSession().load(t, id);
}
}
Here is the UserDao
class:
public class UserDao extends GenericDao<User> {
public User findUser(String email, String password) {
return ((User)getSessionFactory().getCurrentSession().createQuery("from User u where u.email like :email and u.password like :password").setParameter("email", email).setParameter("password", password).uniqueResult());
}
public boolean userExist(String email) {
return ((User)getSessionFactory().getCurrentSession().createQuery("from User u where u.email like :email").setParameter("email", email).uniqueResult()) != null;
}
}
And finally the ReservationDao
class.
public class ReservationDao extends GenericDao<Reservation> {
public List<Reservation> viewReservations(User user) {
return getSessionFactory().getCurrentSession().createFilter(user.getReservations(), "where this.status > 0 order by this.checkInDate").list();
}
public List<Reservation> viewReservations() {
return getSessionFactory().getCurrentSession().createQuery("select r from Reservation r join r.user u where r.status = 2 order by r.checkInDate").list();
}
}
The methods in the DAO classes are not yet complete, I would like to make sure I'm doing it correctly before I implement the rest of the methods. Is this a correct DAO pattern? How can I improve this overall code?
EntityManagerFactory
andEntityManager
instead ofSessionFactory
andSession
? You can avoid the castings... (Not saying casting is wrong, but to me, code looks better without them). Also check this question: Hibernate SessionFactory vs. EntityManagerFactory. – donsenior Oct 2 '15 at 16:58