Hibernate Framework

Get Started. It's Free
or sign up with your email address
Hibernate Framework by Mind Map: Hibernate Framework

1. Config

1.1. hibernate.cfg

1.1.1. <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory> <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property> <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/nvgthuy?zeroDateTimeBehavior=convertToNull</property> <property name="hibernate.connection.username">root</property> <property name="hibernate.show_sql">true</property> <property name="hibernate.current_session_context_class">thread</property> </session-factory> </hibernate-configuration>

1.2. hibernate.reveng

1.2.1. <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate-reverse-engineering PUBLIC "-//Hibernate/Hibernate Reverse Engineering DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-reverse-engineering-3.0.dtd"> <hibernate-reverse-engineering> <schema-selection match-catalog="bookstore"/> <table-filter match-name="books"/> <table-filter match-name="categories"/> <table-filter match-name="users"/> </hibernate-reverse-engineering>

1.3. create mapping file in entity package

1.4. Then, create HibernateUtil.java in model package

2. CRUD

2.1. private final SessionFactory sf = HibernateUtil.getSessionFactory();

2.2. //get all object sf.getCurrentSession().beginTransaction(); return sf.getCurrentSession().createCriteria(Categories.class).list();

2.3. //delete object sf.getCurrentSession().beginTransaction(); sf.getCurrentSession().delete(cat); sf.getCurrentSession().getTransaction().commit(); !!! sf.getCurrentSession().getTransaction().rollback();

2.4. //get object by id sf.getCurrentSession().beginTransaction(); return (Categories) sf.getCurrentSession().get(Categories.class, id);

2.5. //add or update object sf.getCurrentSession().beginTransaction(); sf.getCurrentSession().saveOrUpdate(cat); sf.getCurrentSession().getTransaction().commit(); !!! sf.getCurrentSession().getTransaction().rollback();