Thursday, January 28, 2010

How to connect Hibernate with Microsoft Access


1. Go to Control Panel
2. Go to Administrative Tools
3. Go to ODBC Data sources
4. Create a DSN (say mydb), give the path to .mdb file.
5. Get hibernate.jar

6. Extract contents of access.zip and get Access_JDBC30.jar

7. Create a new Java Project in NetBeans 6.7 or greater. and give the above 2 jars in classpath of the project





8. Create the employee class inside demo package with the following contents


9. Create hibernate.cfg.xml in the root directory of the application (outside all packages)


10. Create EmployeeClient.java file inside client package (Though not mandatory)


Please note the text in red in EmployeeClient class.
Here we have used AnnotationConfiguration because, we have used Annotations in Employee class to map the class fields with the table fields.
If Employee.hbm.xml is used for this mapping then here we would have used Configuration (which we normally do)
SessionFactory factory=new Configuration().configure().buildSessionFactory();