at.thinkingco2.databasemanager.CarAccess.java Source code

Java tutorial

Introduction

Here is the source code for at.thinkingco2.databasemanager.CarAccess.java

Source

package at.thinkingco2.databasemanager;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;

import at.thinkingco2.databasemanager.databeans.CarBean;

/**
 * AccessClass for accessing the CarBean in Database.
 * 
 * @author lukashaidacher
 * 
 */
public class CarAccess extends DatabaseManager {
    private static final CarAccess INSTANCE;

    static {
        INSTANCE = new CarAccess();
    }

    private CarAccess() {
        super();
    }

    /**
     * This function returns a static INSTANCE of CarAccess.
     * 
     * @return a static INSTANCE of AddressAccess.
     */
    public static CarAccess getInstance() {
        return INSTANCE;
    }

    /**
     * This function saves an CarBean to Database.
     * 
     * @param entity
     *            the CarBean
     */
    public void addCar(CarBean entity) {
        addObject(entity);
    }

    /**
     * This function deletes an CarBean from Database.
     * 
     * @param entity
     *            the CarBean
     */
    public void deleteCar(CarBean entity) {
        deleteObject(entity);
    }

    /**
     * This function updates an CarBean in Database.
     * 
     * @param entity
     *            the CarBean
     */
    public void updateCar(CarBean entity) {
        updateObject(entity);
    }

    /**
     * This function deletes all CarBeans from Database.
     */
    public void deleteAllCares() {
        deleteTableData(CarBean.class);
    }

    /**
     * This function returns all CarBeans from Database
     * 
     * @return List of CarBeans
     */
    @SuppressWarnings("unchecked")
    public List<CarBean> getAllCarBeans() {
        return (List<CarBean>) getAllObjectsFromBean(CarBean.class);
    }

    /**
     * This function returns a CarBean specified by its LicenseNumber
     * 
     * @param licenseNumber
     *            the LicenseNumber of the searched Car
     * @return the CarBean
     */
    public CarBean getCarByLicenseNumber(String licenseNumber) {
        Session session = getSession();
        CarBean bean = null;
        Query q = null;

        q = session.createQuery("FROM CarBean WHERE licenseNumber = :licenseNumber");
        q.setParameter("licenseNumber", licenseNumber);

        bean = (CarBean) q.uniqueResult();
        session.close();

        return bean;
    }
}