I want to understand how can i implement the generic methods like add, edit, delete and search on my database, i have already made the connection (hibernate) and works fine
I do have this method, that works
Class: GenericDAO
public <T> T save(final T o){
Session session=HibernateUtil.getSessionFactory().openSession();
Transaction trans=session.beginTransaction();
Object object = (T) session.save(o);
trans.commit();
return (T) object;
}
and in Main
GenericDAO gen = new GenericDAO();
gen.save(object);
also i have others methods that i dont know how to use them
Class: GenericDAO
public void delete(final Object object){
Session session=HibernateUtil.getSessionFactory().openSession();
Transaction trans=session.beginTransaction();
session.delete(object);
trans.commit();
}
/***/
public <T> T get(final Class<T> type, final int id){
Session session=HibernateUtil.getSessionFactory().openSession();
Transaction trans=session.beginTransaction();
Object object = (T) session.get(type, id);
trans.commit();
return (T) object;
}
public <T> List<T> getAll(final Class<T> type) {
Session session=HibernateUtil.getSessionFactory().openSession();
Transaction trans=session.beginTransaction();
final Criteria crit = session.createCriteria(type);
List<T> list = crit.list();
trans.commit();
return list;
}
Thank you
I think
GenericDAOclass is base class. It's not for using directly. Did you check this article ? I checked this article and created a sample project.Example
GitHub - generic-dao-hibernate sample
For example, you might want to create an API to retrieve all employees list according to MySQL first step example.
Employees table schema is like following:
Base SQL
O/R Mapping
Hibernate require you to configure mapping object-relation settings. After that, you will enjoy converting object-to-sql and sql-to-object.
Entity class based on SQL
@Entity, @Table, @Id, @Column, @GeneratedValueare from Hibernate@Data, @NoArgsConstructorare from lombok, it reduces getter/setter code@XmlRootElement, @XmlAccessorTypeare from jaxb, you might don't need to use itResource Class for Frontend
You always need to write DAO(Data Access Object) for accessing the database.
GenericDAOis a method to reduce boilerplate sources codes.EmployeesResource class
#create,#read,#updateor#deleteshould be equivalent with
INSERT,SELECT,UPDATEandDELETEYou need to identify a record or records with key. In this case,
idis sample primary key.GenericDao interface & implementation
Interface ( as is from ibm's post )
According to the post, we can declare dao interface. Then we should implement that interface's methods.
Implementation
If you use only simple CRUD operations in the project, you don't need to append any code for SQL operations. For example, you can create another simple SQL tables like
divisions_tableorpersonnel_tablewith usingextends GenericDao<Division, Integer>orextends GenericDao<Personnel, Integer>.EDIT
To instantiate real dao class related with each table, you need to configure
applicationContext.xmland beans.example
P.S.
You need to remember this article was written a decade ago. And, you should think seriously about which O/R mapper is really good or not. I think O/R mapper is slightly declining now. Instead of Hibernate, you can find MyBatis , JOOQ