Ce diaporama a bien été signalé.
Nous utilisons votre profil LinkedIn et vos données d’activité pour vous proposer des publicités personnalisées et pertinentes. Vous pouvez changer vos préférences de publicités à tout moment.

Hibernate using jpa

415 vues

Publié le

Hibernate using jpa

Publié dans : Formation
  • Identifiez-vous pour voir les commentaires

Hibernate using jpa

  1. 1. Hibernate Framework
  2. 2. What is ORM ? ● ORM handles object relational impedance mis-match . ● Relational Database is table driven (with rows and column) for fast query operation whereas java code is made up with object and classes of this is mis-match in between java code and relational database . ● Like there is different tend to define data e.g for numeric value NUMBER in oracle ● ORM handles this mis-match for you. ● If you are not using any ORM tool so you have to do all these mapping and for large application it could be complex .
  3. 3. What is Hibernate ? ● Hibernate is most popular ORM framework and it and it let you work without being constrained by table driven relational database model . ● Is handles relational mis-match. ● And in addition to its own native API hibernate also implements JPA API specification . As such it can be use in any environment . ● You do not need to work with JDBC . ● So the core of hibernate is all about making persisting data easier .
  4. 4. Architecture ● The following diagram describe high level architecture of hibernate ● In this diagram the hibernate is using Persistence.xml file for the configuration. ● And at the bottom there is Database which is managed by hibernate connection manager. ● Database is most expensive part because it require lots of resource to open and close connection.
  5. 5. Persistence.xml file ● Its standard configuration file in JPA it has to be include in the META-INF folder in class directory . ● This file specify that which underline database is suppose to save, update and remove the entity objects . ● This file configure for cache. ● This file configure for object relational mapping .
  6. 6. Example 1. <persistence xmlns="http://xmlns.jcp.org/xml/ns/persistence" version="2.1"> 2. <persistence-unit name="infinite"> 3. <provider>org.hibernate.ejb.HibernatePersistence</provider> 4. <properties> 5. <property name="hibernate.connection.driver_class" value="oracle.jdbc.OracleDriver"/> 6. <property name="hibernate.dialect" value="org.hibernate.dialect.Oracle10gDialect"/> 7. <property name="hibernate.connection.username" value="SRT3"/> 8. <property name="hibernate.connection.password" value="adept"/> 9. <property name="hibernate.connection.url" value="jdbc:oracle:thin:@//v-in-windmill-db-m:1521/optymyze"/> 10. <property name="hibernate.format_sql" value="true"/> 11. <property name="hibernate.hbm2ddl.auto" value="update"/> 12. </properties> 13. </persistence-unit> 14. </persistence>
  7. 7. Domain class ● Domain classes are class in an application that implement the entity of business domain. ● An entity is a plain old java object (POJO) and formally it also called as entity bean . ● This class represents a table in the database and instance represents row in that table. ● Requirements ○ Annotation with the javax.persistence.Entity annotation ○ Public or protected non argument constructor ○ The class must not be declare final ○ No instance must not be declare final .
  8. 8. Persistence Context ● Persistence context is the set of managed objects of entity manager that exist in particular data store. ● At runtime whenever a session is opened and closed, between those open and close boundaries Hibernate maintains the object in a Persistence Context. Think of it like a first level runtime cache which hibernate controls. ● If an object that has to be retrieved and already exists in the persistence context, the existing managed object is returned without actually accessing the database
  9. 9. Instance States ● An instance of the domain class that means domain object can be in one of three states like . 1. Transient state 2. Persistence state 3. Removed state 4. Detached state
  10. 10. Operations ● Saving Objects ○ It’s most common operation that we perform in hibernate ○ By using persist method we can save objects. ● Retrieving objects ○ When we working with any sort of ORM or persistence framework we always need to return entity from the database it can be list or a single entity . ○ So we gonna look how we do it with JPA. ● Updating Object ○ Here we see that how to modify persisted instance.
  11. 11. Count.. ● Removing Objects ○ Here we remove entity from Database so for that in EntityManager we have remove method by calling .
  12. 12. Entity Association When we build an application we are not only writing data to a single table within a data model , entities are related to each other in different ways. Following are the four ways in which the cardinality of the relationship between the objects can be expressed. An association mapping can be unidirectional as well as bidirectional. 1. One-To-One 2. One-To-Many 3. Many-to-One 4. Many-To-Many
  13. 13. One-To-One → A one-to-one relationships occurs when one entity is related to exactly one occurrence in another entity. → In this relationship, one object of the one pojo class contains association with one object of the another pojo class. This relationship is supposing a user has only one credential.The navigation is one-way from the credential to the user : it’s possible to know Credential of a User, but not vice-versa.
  14. 14. One-To-Many → A one-to-many association is the most common kind of association where an Object can be associated with multiple objects. For example a same Department object can be associated with multiple employee objects. → If the persistent class has list object that contains the entity reference, we need to use one-to-many association to map the list element. Employee and Department table hold One-to-many relationship. Each Department can be associated with multiple Employees and each Employee can have only one Department.
  15. 15. Many-To-Many → A logical data relationship in which the value of one data element can exist in combination with many values of another data element, and vice versa. A many-to-many relationship refers to a relationship between tables in a database when a parent row in one table contains several child rows in the second table, and vice versa. → We are using Employee-Meeting relationship as a many to many relationship example. Each Employee can attain more than one meetings and each meetings can have more than one employee
  16. 16. Entity Inheritance → Java is an object oriented language and It is possible to implement Inheritance in java and which one of the most visible Object-relational mismatch in Relational model. Object oriented systems can model both “is a” and “has a” relationship but Relational model supports only “has a” relationship between two entities. Hibernate can help you to map such Objects with relational tables. But we need to choose certain mapping strategy based on your needs. There are three types of inheritance mapping in hibernate 1. Table per concrete class 2. Table per class hierarchy(Single Table Strategy) 3. Table per subclass
  17. 17. Table Per Class Hierarchy In One Table per Class Hierarchy scheme, we store all the class hierarchy in a single table. A discriminator is a key to uniquely identify the base type of the class hierarchy. Following are the advantages and disadvantages of One Table per Class Hierarchy scheme. Advantage ● This hierarchy offers the best performance since single select may suffice. ● Only one table to deal with. ● Performance wise better than all strategies because no joins need to be performed. .
  18. 18. Advantage/Disadvantage Disadvantage ● Changes to members of the hierarchy required column to be altered, added or removed from the table. ● Most of the column of table are nullable so the NOT NULL constraint cannot be applied. ● Tables are not normalized.
  19. 19. One Table per Concrete Class In case of Table Per Concrete class, tables are created per class and there are no nullable values in the table. Disadvantage of this approach is that duplicate columns are created in the subclass tables. In this case let's say our Person class is abstract and Employee and Owner are concrete classes. So the table structure that comes out is basically one table for Owner and one table for Employee. The data for Person is duplicated in both the tables.
  20. 20. Following are the advantages and disadvantages of One Table per Subclass scheme Advantages This is the easiest method of Inheritance mapping to implement. ● Possible to define NOT NULL constraints on the table. Disadvantages ● Disadvantage of this approach is that duplicate columns are created in the sub tables. ● Changes to a parent class is reflected to large number of tables ● Tables are not normalized.
  21. 21. One Table Per Subclass Suppose we have a class Person with subclass Employee and Owner. Following the class diagram and relationship of these classes. In One Table per Subclass scheme, each class persist the data in its own separate table. Thus in this one we have 3 tables; PERSON, EMPLOYEE and OWNER to persist the class data. But a foreign key relationship exists between the sub class tables and superclass table. So the common data is stored in PERSON table and subclass specific fields are stored in EMPLOYEE and OWNER tables.
  22. 22. Advantage/Disadvantage Advantage ● Tables are normalized. ● Able to define NOT NULL constraint. ● It works well with shallow hierarchy. Disadvantage ● As the hierarchy grows, it may result in poor performance.
  23. 23. Caching Caching is all about application performance optimization and it exists between your application and the database to avoid the number of database hits as many as possible to give a better performance for performance critical applications. Hibernate second level cache uses a common cache for all the entityManager object of an Entity Manager Factory. It is useful if you have multiple session objects from a session factory. EntityManagerFactory holds the second level cache data. It is global for all the session objects and not enabled by default.

×