Hibernate Flashcards
What is the hibernate framework?
The Hibernate Framework provides object/relational mapping and is concerned with data persistence
What is the impedance mismatch?
Impedance mismatch is the problem presented by persisting Java objects to relational databases.
Java objects are related graphically while RDBMS are tabular.
What are the 5 impedance mismatches?
DIGAS
Data Navigation - Walk obj network, DBs are joined toegether to limit queries
Identity - DB sameness, OO == & .equals
Granularity - > classes than tables
Associations - Obj unidirectional, DB foreign key
Subtypes (Inheritance) - RDBMS doesnt have similar concept
4 intentions behind Hibernate
- Domain object model to persist real world processes
- Prevent leakage of concerns - logic kept separate from persistence layer
- Transparent and Automatic persistence - Java programming to persist objects to a DB (no sql) & through an XML file persistence is provided automatically.
- XML meta data - human readable format, automatically maps.
What are the benefits of using Domain Objects over Data Transfer Objects?
Domain objects contain data and logic and so help separate concerns. DTOs only contain data.
What are the steps to achieving persistence in Hibernate?
- SessionFactory factory;
- //Get connection
- ManageCourse mc = new ManageCourse()
- Integer crs1 = mc.addCourse(“1001”);
- public Integer addCourse(int courseID){
- Session session = factory.openSession();
- Transaction trans = null;
- trans = session.beingTransaction();
- Course course = new Course(courseID);
- session.saveOrUpdate(course);
- trans.commit();
What are the steps to achieving persistence in JDBC?
- Connection con;
- int courseID = c.getCourseID();
- String sqlStmt = “INSERT INTO COURSE (course_id)” + VALUES(?)”;
- PreparedStatement ps = con.preparedStatement(sqlStmt);
- ps.setInt(1, courseID);
- ps.execute();
Compare and contrast Persistence with JDBC vs Hibernate
Hibernate uses DO instead of DTO.
Hibernate Business layer, JDBC service layer
Hibernate uses SessionFactory, JDBC PreparedStatement
Hibernate’s persistence is transparent and automatic, JDBC’s is SQL written and manual
Hibernate has a cache, JDBC doesnt
Hibernate maps to XML to map POJO, JDBC does this in DAO
Hibernate prevents leakage of concerns, JDBC code is mixed
Hibernate uses saveOrUpdate/ commit, JDBC uses execute