A CRUD project using hibernate ORM tool
- Create from scratch , hibernate n maven based Java SE application.
Steps to create Hibernate projct.
-
Change perspective to Java
-
Create Maven Project ---skip archetype selection -- grp id,artifact id , packaging option : jar --finish
-
Creates default pom.xml . Add n tags from text file - pom file.
-
Update the project . R click on the project --> Maven --> Update Project -->select Force update checkbox -->Finish
-
Edit hibernate.cfg.xml , as per your DB settings.
-
Take help of hib-fgg-xml text file
-
Create HibernateUtils class to create singleton , immutable , inherently thrd safe SessionFactory instance.
-
Create a class TestHibernate under package. Add following code.
import static utils.HibernateUtils.; import org.hibernate.;
public class TestHibernate {
public static void main(String[] args) {
try(SessionFactory sf=getSf())
{
System.out.println("Hibernate booted.....");
}catch (Exception e) {
e.printStackTrace();
}
}
}
- Run this as "java application" , check console to see , sf created & hib booted .
Above confirms bootstrapping of hibernate framework.
- Create a POJO n test auto table creation 9.1 Create a User POJO Add these Data members userId (PK) ,name,email,password,confirmPassword,role(enum), regAmount; LocalDate/Date regDate; byte[] image; Add JPA annotations (javax.persistence) Confirm auto table creation.
9.2 Add entry per POJO in hibernate.cfg.xml Run TestHibernate to confirm auto table creation.
-
Create Hibernate based DAO layer , to insert a record. 10.1 DAO layer i/f String registerUser(User user); 10.2 Hibenrate based DAO implementation class no data mebers, no constr,no clean up CRUD method
-
Create a main(...) based tester to test entire application , for user registration.