Pojos and Hibernate DAOs plugin (beta)

This plugin generates pojos (like the Hibernate plugin), a abstract persistence layer and an hibernate implementation of this layer.

The generated code is actually a implementation of the pattern of described at http://www.hibernate.org/328.html . This pattern suits well with the open-session in view pattern (i.e. web applications).

For each table the plugin generates the pojo, an abstract DAO class and a concrete hibernate dao.

In each dao there is a getById() method, a save() method, an update() method, a delete() method and a finder for each different column.

It generates hibernate mappping files as well (hbm files).

If you want something equivalent but with ejb3 annotations, have a look at the "EJB3 And Hibernate DAOs" plugin .

Example

Suppose we have two tables in a database :

  • Table CITY
  • Table PERSON

Here is the corresponding MySQL DDL :

CREATE TABLE city (
  id_city int(11) NOT NULL auto_increment,
  name varchar(50) NOT NULL,
  PRIMARY KEY (id_city)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
        
CREATE TABLE person (
  id_person int(11) NOT NULL auto_increment,
  name varchar(50) NOT NULL,
  first_name varchar(50) NOT NULL,
  age int(11) NOT NULL,
  id_city int(11) NOT NULL,
  PRIMARY KEY (id_person),
  KEY fk_person_city (id_city),
  CONSTRAINT fk_person_city FOREIGN KEY (id_city) REFERENCES city (id_city)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

A person lives in a city, and for each city there are many persons living in it. So we have a many-to-one relationship.

Once everything is generated, you can write one line of code to retrieve every persons in the database to print their name and the name of the city they live in :

// jdbc : Transaction t = HibernateUtil.getSessionFactory().getCurrentSession().beginTransaction();
List<Person> persons = DAOFactory.DEFAULT.buildPersonDAO().findAll();
for (Person person : persons)
        {
                System.out.println(person.getFirstName()+" "+person.getName()+" lives in "+person.getCity().getName());
        }

Here are the different generated files :

Interfaces

  • com.salto.pojodao.CityDAO : an interface for city daos
    package com.salto.pojodao;
    
    import java.util.Map;
    import java.util.List;
    import java.sql.Timestamp;
    
    import com.salto.pojodao.pojo.City;
    /**
     * <p>Hibernate DAO layer for Citys</p>
     * <p>Generated at Thu Feb 15 16:29:39 CET 2007</p>
     *
     * @author Salto-db Generator Ant v1.0.15 / Pojos + Hibernate mapping + Generic DAO
     */
    public interface CityDAO extends GenericDAO<City,Integer> {
    
            /*
             * TODO : Add specific businesses daos here.
             * These methods will be overwrited if you re-generate this interface.
             * You might want to extend this interface and to change the dao factory to return 
             * an instance of the new implemenation in buildCityDAO()
             */
             
            /**
             * Find City by criteria.
             * If a parameter is null it is not used in the query.
             */
            public List<City> findByCriteria(String name);
             
            /**
             * Find City by criteria.
             */
            public List<City> findByCriteria(Map criterias);
    
             
            /**
             * Find City by name
             */
            public List<City> findByName(String name);
    
    }
  • com.salto.pojodao.DAOFactory : a factory for daos. When you develop you use this class to build daos instance, this way you don't have any references to an implementation in your methods and it makes it easy to change of implementation.
    package com.salto.pojodao;
    
    import com.salto.pojodao.hibernate.HibernateDAOFactory;
    
    /**
     * Generated at Thu Feb 15 16:29:39 CET 2007
     *
     * @see http://www.hibernate.org/328.html
     * @author Salto-db Generator Ant v1.0.16 / Pojos + Hibernate mapping + Generic DAO
     */
    
    public abstract class DAOFactory {
    
            private static final DAOFactory HIBERNATE = new HibernateDAOFactory();
    
            public static final DAOFactory DEFAULT = HIBERNATE;
            
            /**
         * Factory method for instantiation of concrete factories.
         */
        public static DAOFactory instance(Class factory) {
            try {
                return (DAOFactory)factory.newInstance();
            } catch (Exception ex) {
                throw new RuntimeException("Couldn't create DAOFactory: " + factory);
            }
        }
            
            public abstract CityDAO buildCityDAO();
            
            public abstract PersonDAO buildPersonDAO();
            
    }
  • com.salto.pojodao.GenericDAO : an interface describing a generic dao. Other daos extend it.
    package com.salto.pojodao;
    
    import java.io.Serializable;
    import java.util.List;
    
    /**
     * Generated at Thu Feb 15 16:29:39 CET 2007
     *
     * @author Salto-db Generator Ant v1.0.15 / Pojos + Hibernate mapping + Generic DAO
     */
    public interface GenericDAO<T, ID extends Serializable> {
    
            T getById(ID id, boolean lock);
    
            T getById(ID id);
    
            List<T> findAll();
    
            void save(T entity);
    
            void update(T entity);
    
            void saveOrUpdate(T entity);
    
            void delete(T entity);
    
            void delete(T entity, Boolean doItNow);
            
            int deleteById(ID id);
            
    }
  • com.salto.pojodao.PersonDAO : an interface for person daos
    package com.salto.pojodao;
    
    import java.util.Map;
    import java.util.List;
    import java.sql.Timestamp;
    
    import com.salto.pojodao.pojo.Person;
    /**
     * <p>Hibernate DAO layer for Persons</p>
     * <p>Generated at Thu Feb 15 16:29:39 CET 2007</p>
     *
     * @author Salto-db Generator Ant v1.0.15 / Pojos + Hibernate mapping + Generic DAO
     */
    public interface PersonDAO extends GenericDAO<Person,Integer> {
    
            /*
             * TODO : Add specific businesses daos here.
             * These methods will be overwrited if you re-generate this interface.
             * You might want to extend this interface and to change the dao factory to return 
             * an instance of the new implemenation in buildPersonDAO()
             */
             
            /**
             * Find Person by criteria.
             * If a parameter is null it is not used in the query.
             */
            public List<Person> findByCriteria(String name, String firstName, Integer age, Integer idCity);
             
            /**
             * Find Person by criteria.
             */
            public List<Person> findByCriteria(Map criterias);
    
             
            /**
             * Find Person by name
             */
            public List<Person> findByName(String name);
    
            /**
             * Find Person by firstName
             */
            public List<Person> findByFirstName(String firstName);
    
            /**
             * Find Person by age
             */
            public List<Person> findByAge(Integer age);
    
            /**
             * Find Person by idCity
             */
            public List<Person> findByIdCity(Integer idCity);
    
    }

Hibernate implementation

  • com.salto.pojodao.hibernate.AbstractHibernateDAO : an abstract hibernate implementation of GenericDAO
    package com.salto.pojodao.hibernate;
    
    
    import com.GenericDAO;
    import java.io.Serializable;
    import java.lang.reflect.ParameterizedType;
    import java.util.List;
    
    import org.hibernate.Criteria;
    import org.hibernate.FlushMode;
    import org.hibernate.Query;
    import org.hibernate.LockMode;
    import org.hibernate.Session;
    import org.hibernate.criterion.Criterion;
    import org.hibernate.criterion.Example;
    
    /**
     * Generated at Sat Mar 03 20:24:04 CET 2007
     *
     * @author Salto-db Generator Ant v1.0.16 / EJB3 + Hibernate DAO
     * @see http://www.hibernate.org/328.html
     */
    public abstract class AbstractHibernateDAO<T, ID extends Serializable> implements GenericDAO<T, ID> {
    
            private Session session;
    
            private Class<T> persistentClass;
    
            public AbstractHibernateDAO() {
                    this.persistentClass = (Class<T>) ((ParameterizedType) getClass().getGenericSuperclass()).getActualTypeArguments()[0];
            }
    
        protected Session getSession() {
                    if (session == null)
               session = HibernateUtil.getSessionFactory().getCurrentSession();
           return session;
        }
    
        public Class<T> getPersistentClass() {
            return persistentClass;
        }
    
            public void setSession(Session session) {
                    this.session = session;
            }
    
            @SuppressWarnings("unchecked")
            public T getById(ID id) {
                    return (T) getSession().get(getPersistentClass(), id);
            }
    
            @SuppressWarnings("unchecked")
            public T getById(ID id, boolean lock) {
                    if (lock) {
                            return (T) getSession().get(getPersistentClass(), id,
                                            LockMode.UPGRADE);
                    } else
                            return getById(id);
            }
    
            public void save(T entity) {
                    getSession().save(entity);
            }
    
            public void update(T entity) {
                    getSession().update(entity);
            }
    
            public void saveOrUpdate(T entity) {
                    getSession().saveOrUpdate(entity);
            }
    
            public void delete(T entity) {
                    getSession().delete(entity);
            }
    
            public void delete(T entity, Boolean doItNow) {
                    if (doItNow) {
                            getSession().setFlushMode(FlushMode.ALWAYS);
                    }
                    delete(entity);
            }
            
            @SuppressWarnings("unchecked")
        public List<T> findAll() {
            return findByCriteria();
        }
            
            /**
         * Use this inside subclasses as a convenience method.
         */
        @SuppressWarnings("unchecked")
        protected List<T> findByCriteria(Criterion... criterion) {
            Criteria crit = getSession().createCriteria(getPersistentClass());
            for (Criterion c : criterion) {
                crit.add(c);
            }
            return crit.list();
       }
            
            /**
             * This method will execute an HQL query and return the number of affected entities.
             */
            protected int executeQuery(String query, String namedParams[],  Object params[]) {
                    Query q = getSession().createQuery(query);
                    
                    if (namedParams != null) {
                            for (int i = 0; i < namedParams.length; i++) {
                                    q.setParameter(namedParams[i], params[i]);
                            }
                    }
    
                    return q.executeUpdate();
            }
            
            protected int executeQuery(String query) {
                    return executeQuery(query, null, null);
            }
            
            /**
             * This method will execute a Named HQL query and return the number of affected entities.
             */
            protected int executeNamedQuery(String namedQuery, String namedParams[],        Object params[]) {
                    Query q = getSession().getNamedQuery(namedQuery);
                    
                    if (namedParams != null) {
                            for (int i = 0; i < namedParams.length; i++) {
                                    q.setParameter(namedParams[i], params[i]);
                            }
                    }
    
                    return q.executeUpdate();
            }
            
            protected int executeNamedQuery(String namedQuery) {
                    return executeNamedQuery(namedQuery, null, null);
            }
            
            @SuppressWarnings("unchecked")
        public List<T> findByExample(T exampleInstance, String[] excludeProperty) {
            Criteria crit = getSession().createCriteria(getPersistentClass());
            Example example =  Example.create(exampleInstance);
            for (String exclude : excludeProperty) {
                example.excludeProperty(exclude);
            }
            crit.add(example);
            return crit.list();
        }
            
    }
  • com.salto.pojodao.hibernate.CityHibernateDAO : the Hibernate implementation of CityDAO
    package com.salto.pojodao.hibernate;
    
    import java.util.Map;
    import java.util.List;
    import java.sql.Timestamp;
    
    import com.salto.pojodao.pojo.City;
    import com.salto.pojodao.CityDAO;
    
    import org.hibernate.Criteria;
    import org.hibernate.LockMode;
    import org.hibernate.Query;
    import org.hibernate.Session;
    import org.hibernate.criterion.Restrictions;
    
    /**
     * <p>Hibernate DAO layer for Citys</p>
     * <p>Generated at Thu Feb 15 16:29:39 CET 2007</p>
     *
     * @author Salto-db Generator Ant v1.0.15 / Pojos + Hibernate mapping + Generic DAO
     */
    public class CityHibernateDAO extends
                    AbstractHibernateDAO<City, Integer> implements
                    CityDAO {
            
            /**
             * Find City by criteria.
             * If a parameter is null it is not used in the query.
             * //FIXME:  there's certainly an issue with foreign keys...
             */
            @SuppressWarnings("unchecked")
            public List<City> findByCriteria(String name) {
                    
                    Criteria criteria = getSession().createCriteria(City.class);
    
                    if (name != null) {
                            criteria.add(Restrictions.eq("name", name));
                    }
            
                    return criteria.list();
            }
             
            /**
             * Find City by criteria.
             */
            @SuppressWarnings("unchecked")
            public List<City> findByCriteria(Map criterias) {
            
                    Criteria criteria = getSession().createCriteria(City.class);
                    criteria.add(Restrictions.allEq(criterias));
                    return criteria.list();
            }
            
            public int deleteById(Integer id)       {
                    Query q = getSession().createQuery("delete from City where id = :id");
                    q.setParameter("id",id);
                    return q.executeUpdate();
            }
    
            @SuppressWarnings("unchecked")
            public List<City> findAll() {
                    Query q = getSession().createQuery("from City");
                    q.setCacheable(true);
                    return q.list();
            }
    
            /**
             * Find City by name
             */
            @SuppressWarnings("unchecked")
            public List<City> findByName(String name) {
                    Query q = getSession().createQuery("from City where name = :name");
                    q.setParameter("name",name);
                    q.setCacheable(true);
                    return q.list();
            }
            
    
    }       
  • com.salto.pojodao.hibernate.HibernateDAOFactory : the Hibernate implementation of DAOFactory
    package com.salto.pojodao.hibernate;
    
    import com.salto.pojodao.DAOFactory;
    import com.salto.pojodao.CityDAO;
    import com.salto.pojodao.PersonDAO;
    
    /**
     * Generated at Thu Feb 15 16:29:39 CET 2007
     *
     * @see http://www.hibernate.org/43.html
     * @author Salto-db Generator Ant v1.0.16 / Pojos + Hibernate mapping + Generic DAO
     */
    public class HibernateDAOFactory extends DAOFactory {
    
            /* (non-Javadoc)
             * @see com.salto.pojodao.DAOFactory#buildCityDAO()
             */
            @Override
            public CityDAO buildCityDAO() {
                    return new CityHibernateDAO();
            }
            
            /* (non-Javadoc)
             * @see com.salto.pojodao.DAOFactory#buildPersonDAO()
             */
            @Override
            public PersonDAO buildPersonDAO() {
                    return new PersonHibernateDAO();
            }
            
    }
  • com.salto.pojodao.hibernate.HibernateUtil : utility methods for Hibernate
    package com.salto.pojodao.hibernate;
    
    import org.hibernate.Session;
    import org.hibernate.SessionFactory;
    import org.hibernate.Transaction;
    import org.hibernate.cfg.Configuration;
    
    /**
     * Generated at Thu Feb 15 16:29:39 CET 2007
     *
     * @author Salto-db Generator Ant v1.0.16 / Pojos + Hibernate mapping + Generic DAO
     */
    public final class HibernateUtil {
    
            private static SessionFactory sessionFactory;
    
            static {
                    try {
                            sessionFactory = new Configuration().configure().buildSessionFactory();         
                    } catch (Throwable ex) {
    
                            throw new ExceptionInInitializerError(ex);
                    }
            }
    
            private HibernateUtil() {
    
            }
    
            /**
             * Returns the SessionFactory used for this static class.
             * 
             * @return SessionFactory
             */
            public static SessionFactory getSessionFactory() {
                    return sessionFactory;
            }
    
    }
  • com.salto.pojodao.hibernate.PersonHibernateDAO : the Hibernate implementation of PersonDAO
    package com.salto.pojodao.hibernate;
    
    import java.util.List;
    import java.util.Map;
    
    import org.hibernate.Criteria;
    import org.hibernate.LockMode;
    import org.hibernate.Query;
    import org.hibernate.Session;
    import org.hibernate.criterion.Restrictions;
    
    import com.salto.pojodao.PersonDAO;
    import com.salto.pojodao.pojo.Person;
    
    /**
     * <p>Hibernate DAO layer for Persons</p>
     * <p>Generated at Thu Feb 15 16:29:39 CET 2007</p>
     *
     * @author Salto-db Generator Ant v1.0.16 / Pojos + Hibernate mapping + Generic DAO
     */
    public class PersonHibernateDAO extends
                    AbstractHibernateDAO<Person, Integer> implements
                    PersonDAO {
            
            /**
             * Find Person by criteria.
             * If a parameter is null it is not used in the query.
             * //FIXME:  there's certainly an issue with foreign keys...
             */
            @SuppressWarnings("unchecked")
            public List<Person> findByCriteria(String name, String firstName, Integer age, Integer idCity) {
                    
                    Criteria criteria = getSession().createCriteria(Person.class);
    
                    if (name != null) {
                            criteria.add(Restrictions.eq("name", name));
                    }
            
                    if (firstName != null) {
                            criteria.add(Restrictions.eq("firstName", firstName));
                    }
            
                    if (age != null) {
                            criteria.add(Restrictions.eq("age", age));
                    }
            
                    if (idCity != null) {
                            criteria.add(Restrictions.eq("idCity", idCity));
                    }
            
                    return criteria.list();
            }
             
            /**
             * Find Person by criteria.
             */
            @SuppressWarnings("unchecked")
            public List<Person> findByCriteria(Map criterias) {
            
                    Criteria criteria = getSession().createCriteria(Person.class);
                    criteria.add(Restrictions.allEq(criterias));
                    return criteria.list();
            }
            
            public int deleteById(Integer id)       {
                    Query q = getSession().createQuery("delete from Person where id = :id");
                    q.setParameter("id",id);
                    return q.executeUpdate();
            }
    
            /**
             * Find Person by name
             */
            @SuppressWarnings("unchecked")
            public List<Person> findByName(String name) {
                    Query q = getSession().createQuery("from Person where name = :name");
                    q.setParameter("name",name);
                    q.setCacheable(true);
                    return q.list();
            }
            
            /**
             * Find Person by firstName
             */
            @SuppressWarnings("unchecked")
            public List<Person> findByFirstName(String firstName) {
                    Query q = getSession().createQuery("from Person where firstName = :firstName");
                    q.setParameter("firstName",firstName);
                    q.setCacheable(true);
                    return q.list();
            }
            
            /**
             * Find Person by age
             */
            @SuppressWarnings("unchecked")
            public List<Person> findByAge(Integer age) {
                    Query q = getSession().createQuery("from Person where age = :age");
                    q.setParameter("age",age);
                    q.setCacheable(true);
                    return q.list();
            }
            
            /**
             * Find Person by idCity
             */
            @SuppressWarnings("unchecked")
            public List<Person> findByIdCity(Integer idCity) {
                    Query q = getSession().createQuery("from Person where city.idCity = :idCity");
                    q.setParameter("idCity",idCity);
                    q.setCacheable(true);
                    return q.list();
            }
            
    
    }

Pojos

  • com.salto.pojodao.pojo.City : Pojo mapping city table
    package com.salto.pojodao.pojo;
    
    import java.util.List;
    import java.io.Serializable;
    import java.sql.Timestamp;
    
    
    /**
     * <p>Pojo mapping table city</p>
     * <p></p>
     *
     * <p>Generated at Thu Feb 15 16:29:39 CET 2007</p>
     * @author Salto-db Generator Ant v1.0.15 / Hibernate pojos and xml mapping files.
     * 
     */
    public class City implements Serializable {
    
            /**
             * Attribute idCity.
             */
            private Integer idCity;
            
            /**
             * Attribute name.
             */
            private String name;
            
            /**
             * List of Person
             */
            private List<Person> persons = null;
    
            
            /**
             * @return idCity
             */
            public Integer getIdCity() {
                    return idCity;
            }
    
            /**
             * @param idCity new value for idCity 
             */
            public void setIdCity(Integer idCity) {
                    this.idCity = idCity;
            }
            
            /**
             * @return name
             */
            public String getName() {
                    return name;
            }
    
            /**
             * @param name new value for name 
             */
            public void setName(String name) {
                    this.name = name;
            }
            
            /**
             * Get the list of Person
             */
             public List<Person> getPersons() {
                    return this.persons;
             }
             
            /**
             * Set the list of Person
             */
             public void setPersons(List<Person> persons) {
                    this.persons = persons;
             }
    
    }
  • com.salto.pohodao.pojo.Person : Entity mapping person table
    package com.salto.pojodao.pojo;
    
    import java.util.List;
    import java.io.Serializable;
    import java.sql.Timestamp;
    
    
    /**
     * <p>Pojo mapping table person</p>
     * <p></p>
     *
     * <p>Generated at Thu Feb 15 16:29:39 CET 2007</p>
     * @author Salto-db Generator Ant v1.0.15 / Hibernate pojos and xml mapping files.
     * 
     */
    public class Person implements Serializable {
    
            /**
             * Attribute idPerson.
             */
            private Integer idPerson;
            
            /**
             * Attribute name.
             */
            private String name;
            
            /**
             * Attribute firstName.
             */
            private String firstName;
            
            /**
             * Attribute age.
             */
            private Integer age;
            
            /**
             * Attribute city
             */
             private City city;     
    
            
            /**
             * @return idPerson
             */
            public Integer getIdPerson() {
                    return idPerson;
            }
    
            /**
             * @param idPerson new value for idPerson 
             */
            public void setIdPerson(Integer idPerson) {
                    this.idPerson = idPerson;
            }
            
            /**
             * @return name
             */
            public String getName() {
                    return name;
            }
    
            /**
             * @param name new value for name 
             */
            public void setName(String name) {
                    this.name = name;
            }
            
            /**
             * @return firstName
             */
            public String getFirstName() {
                    return firstName;
            }
    
            /**
             * @param firstName new value for firstName 
             */
            public void setFirstName(String firstName) {
                    this.firstName = firstName;
            }
            
            /**
             * @return age
             */
            public Integer getAge() {
                    return age;
            }
    
            /**
             * @param age new value for age 
             */
            public void setAge(Integer age) {
                    this.age = age;
            }
            
            /**
             * get city
             */
            public City getCity() {
                    return this.city;
            }
            
            /**
             * set city
             */
            public void setCity(City city) {
                    this.city = city;
            }
    
    
    
    }

Configuration files

  • hibernate.cfg.xml
    <!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
        "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
    
    <!-- This file has been generated by Salto-db Generator Ant v1.0.15 at Thu Feb 15 16:29:39 CET 2007 -->
    <hibernate-configuration>
            <session-factory>
    
                    <!-- You need to complete the configuration here. This is just a sample, you should
                                     use a connection pool -->
                    <property name="connection.url">jdbc:mysql://localhost/sdb</property>
                    <property name="connection.username">root</property>
                    <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
                    <!-- Your database is MySQL-->
                    <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
                    <property name="connection.password">XXXXXXXX</property>
                    <property name="show_sql">true</property>
    
                    <!-- Remove this property if you use JTA -->
                    <property name="current_session_context_class">org.hibernate.context.ThreadLocalSessionContext</property>
            
            
                    <mapping resource="City.hbm.xml" />
                    <mapping resource="Person.hbm.xml" />
    
            </session-factory>
    </hibernate-configuration>
  • City.hbm.xml
    <!DOCTYPE hibernate-mapping PUBLIC
            "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
            "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> 
    
    <!-- This file has been generated by Salto-db Generator Ant v1.0.15 / Hibernate pojos 
                    and xml mapping files. at Thu Feb 15 16:29:39 CET 2007 -->
    <hibernate-mapping>
            <class name="com.salto.pojodao.pojo.City" table="city">
    
                    <id name="idCity">
                            <generator class="native"/>
                    </id>
    
                    <property name="name" column="name"/>
            
                    <bag name="persons" inverse="true">
                            <key column="id_city"/>
                            <one-to-many class="com.salto.pojodao.pojo.Person"/>
                    </bag>
    
            </class>
    </hibernate-mapping>
  • Person.hbm.xml
    <!DOCTYPE hibernate-mapping PUBLIC
            "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
            "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> 
    
    <!-- This file has been generated by Salto-db Generator Ant v1.0.15 / Hibernate pojos 
                    and xml mapping files. at Thu Feb 15 16:29:39 CET 2007 -->
    <hibernate-mapping>
            <class name="com.salto.pojodao.pojo.Person" table="person">
    
                    <id name="idPerson">
                            <generator class="native"/>
                    </id>
    
                    <property name="name" column="name"/>
            
                    <property name="firstName" column="first_name"/>
            
                    <property name="age" column="age"/>
            
                    <many-to-one name="city" column="id_city"/>
                    
            </class>
    </hibernate-mapping>

Usage

Retrieve every persons in database :

// jdbc : Transaction t = HibernateUtil.getSessionFactory().getCurrentSession().beginTransaction();
List<Person> persons = DAOFactory.DEFAULT.buildPersonDAO().findAll();
// jdbc: commit transaction and handle exceptions

Retrieve every persons that lives in a "Paris" :

// jdbc : Transaction t = HibernateUtil.getSessionFactory().getCurrentSession().beginTransaction();
City paris = DAOFactory.DEFAULT.buildCityDAO().findByName("Paris");
List<Person> parisCitizen = paris.getPersons();

// if you know what Paris's id is, you can use this code : 
// List<Person> parisCitizen = DAOFactory.DEFAULT.buildPersonDAO().findByIdCity(parisId);
// jdbc: commit transaction and handle exceptions

Save a new Person in database :

// jdbc : Transaction t = HibernateUtil.getSessionFactory().getCurrentSession().beginTransaction();
                
Person person = new Person();
person.setName("doe");
person.setFirstName("john");
person.setAge(20);

DAOFactory.DEFAULT.buildPersonDAO().save(person);

// jdbc: commit transaction and handle exceptions

As you can see, thanks to Hibernate and to the generic dao pattern, there is absolutely no reference to hibernate or even to database management in the code you have to write. You can focus on business logic instead of loosing time managing connections, transactions and so on. Have a look at the Open session in view pattern for more informations : http://www.hibernate.org/43.html .