I am a newbie to Hibernate.
I have a school project using MySQL, JDBC, JSP/Servlet, Tomcat technologies.
The way that I do the database connection in EVERY Servlet is:
- establish database connection in JDBC.
- create Query statement and execute it.
- close database connection.
That is very very tedious, so I want to use JPA with Hibernate to handle it.
I looked some tutorials and saw that there’s a JPA Project option in Eclipse, so went ahead and used it.
Now I am confused about how to add JSP/Servlet into this project. It seems like this type project is not meant for handling any front-end technologies.
Could anyone please help me clear this out? What type of eclipse project should I use in my case?
Thanks a lot.
You can use whatever Eclipse project templates you want, they just setup directory structure and libraries for convenience. You could start with a normal Java project if you wanted, it’s just a matter of understanding what libraries/dependencies to setup. My personal preference is to use the Dynamic Web Project so it set’s up my web directory structure and web.xml.
Adding JPA/Hibernate to a project consists of:
1) Mapping your classes (annotations or xml)
2) Creating the EntityManagerFactory/SessionFactory
3) Inject the Factory where you need database access
If this is your first non-trivial database application take a look at the DAO Pattern. It will help you understand how to abstract away database specific decisions (e.g. JDBC vs Hibernate vs JPA) from your application logic.