SlideShare a Scribd company logo
Hibernate Introduction Session - I
Object Relational Mapping ORM refers to the technique of peristing objects in relational database using metadata that describes mapping between objects and database tables. Performs automatic dirty checking, lazy associations etc.., Has a language or API for specifying queries that refer to classes and properties.
Hibernate Hibernate is a complete ORM tool. 1. Integrates elegantly with all popular Java EE application servers , web containers and also works in standalone applications . 2. Natural programming model – Support for Inheritance, polymorphism, composition and the Java collections framework 3. Scalability - has a dual-layer cache architecture 4. Unique query language which supports writing query on objects -  HQL
Contd.., 5. Productivity – Eliminates much of Persistence-related code. 6. Vendor Independence  7. Solves portability issues
Hibernate Object Hibernate object is a simple POJO with some recommended good practices as below: There must be a default constructor for the class. There must be accessors and optional mutators for all the instance variables of the class. (Immutable objcets are exceptional cases). Recommended - POJO implements Serializable.
Hibernate Object (contd..,) Recommended - POJO should ideally have an ID instance variable, usually of type Long.  Recommended - The mutator for the id property should be private, not public.
Hibernate Object (contd..,) You should decide on a business key for the object and implement the equals and hashCode methods for it. You should add any extra type specific constructors (which should leave the id field null) and business rule methods you like. You should not make the class final if you want to be able to use lazy loading for objects of the class.
Hibernate Architecture terminology SessionFactory   A threadsafe (immutable) cache of compiled mappings for a single database. A factory for Session and a client of ConnectionProvider.  Might hold an optional (second-level) cache of data that is reusable between transactions, at a process- or cluster-level.  Session A single-threaded, short-lived object representing a conversation between the application and the persistent store.  Wraps a JDBC connection. Factory for Transaction.  Holds a mandatory (first-level) cache of persistent objects, used when navigating the object graph or looking up objects by identifier.
Database Interaction Pattern 1.   Create a Configuration object, load the configuration parameters from the hibernate.properties and adjust them as required. 2.  Create a SessionFactory object from the Configuration object. The SessionFactory object is a heavyweight, thread safe object. You would normally share one such object between all your threads in a web application. 3.   For each unit of work (normally one use case) use the SessionFactory object to obtain a Session object. This is an extremely lightweight, non-thread safe object. It will be associated with a database connection but it only obtains that connection lazily, i.e., only when (and if) it is required. Session objects must not be shared between different threads.  4.  Inside a try block, get a Transaction object by calling beginTransaction() on the Session object.
Hibernate - Database Interaction Pattern (contd..,) 5. Interact with the database: explicitly by calling methods of Session to associate objects to the database (i.e., map them to the database), execute queries, load, save, delete mapped objects etc. implicitly by calling property mutators on mapped objects that will lead to the database being updated. implicitly by referencing non-mapped objects from mapped objects which (in certain circumstances) can cause the non-mapped objects to be added to the database. implicitly by unreferencing mapped objects from other mapped objects which (in certain circumstances) can cause the unreferenced objects to be deleted from the database.  6.  Call commit() on the Transaction object and close the try block, handling exceptions and closing the Session object in the usual way.
Hibernate Object - Life Cycle
Hibernate Object - Life Cycle Transient objects  DO NOT  (yet) have any association with the database.They act like any normal Java object and are not saved to the database. Persistent objects  DO  have an association with the database.  Detached objects are objects that were persistent but no longer have a connection to a Session object (usually because you have closed the session). A detached object may be re-attached later to another Session object to become persistent again.
Built-in Mapping Types – Primitive Types
Built-in Mapping Types –  Date and Time Types
Session I - Demo
Ad

More Related Content

What's hot (20)

JPA For Beginner's
JPA For Beginner'sJPA For Beginner's
JPA For Beginner's
NarayanaMurthy Ganashree
 
Java persistence api
Java persistence api Java persistence api
Java persistence api
Luis Goldster
 
jpa-hibernate-presentation
jpa-hibernate-presentationjpa-hibernate-presentation
jpa-hibernate-presentation
John Slick
 
hibernate with JPA
hibernate with JPAhibernate with JPA
hibernate with JPA
Mohammad Faizan
 
Евгений Капинос "Advanced JPA (Java Persistent API)"
Евгений Капинос "Advanced JPA (Java Persistent API)"Евгений Капинос "Advanced JPA (Java Persistent API)"
Евгений Капинос "Advanced JPA (Java Persistent API)"
Anna Shymchenko
 
Introduction to JPA and Hibernate including examples
Introduction to JPA and Hibernate including examplesIntroduction to JPA and Hibernate including examples
Introduction to JPA and Hibernate including examples
ecosio GmbH
 
JPA Best Practices
JPA Best PracticesJPA Best Practices
JPA Best Practices
Carol McDonald
 
Incremental Java Deserialization/Reserialization
Incremental Java Deserialization/ReserializationIncremental Java Deserialization/Reserialization
Incremental Java Deserialization/Reserialization
Bill La Forge
 
Serialization/deserialization
Serialization/deserializationSerialization/deserialization
Serialization/deserialization
Young Alista
 
Entity Persistence with JPA
Entity Persistence with JPAEntity Persistence with JPA
Entity Persistence with JPA
Subin Sugunan
 
Introduction to JPA (JPA version 2.0)
Introduction to JPA (JPA version 2.0)Introduction to JPA (JPA version 2.0)
Introduction to JPA (JPA version 2.0)
ejlp12
 
Intro To Hibernate
Intro To HibernateIntro To Hibernate
Intro To Hibernate
Amit Himani
 
Spring Data in 10 minutes
Spring Data in 10 minutesSpring Data in 10 minutes
Spring Data in 10 minutes
Corneil du Plessis
 
Spring data jpa
Spring data jpaSpring data jpa
Spring data jpa
Jeevesh Pandey
 
Jpa 2.1 Application Development
Jpa 2.1 Application DevelopmentJpa 2.1 Application Development
Jpa 2.1 Application Development
ThirupathiReddy Vajjala
 
JPA and Hibernate
JPA and HibernateJPA and Hibernate
JPA and Hibernate
elliando dias
 
ZODB, the Zope Object Database (May 2003)
ZODB, the Zope Object Database (May 2003)ZODB, the Zope Object Database (May 2003)
ZODB, the Zope Object Database (May 2003)
Kiran Jonnalagadda
 
Serialization in .NET
Serialization in .NETSerialization in .NET
Serialization in .NET
Abhi Arya
 
Spring Data JPA
Spring Data JPASpring Data JPA
Spring Data JPA
Cheng Ta Yeh
 
Powerful persistence layer with Google Guice & MyBatis
Powerful persistence layer with Google Guice & MyBatisPowerful persistence layer with Google Guice & MyBatis
Powerful persistence layer with Google Guice & MyBatis
simonetripodi
 
Java persistence api
Java persistence api Java persistence api
Java persistence api
Luis Goldster
 
jpa-hibernate-presentation
jpa-hibernate-presentationjpa-hibernate-presentation
jpa-hibernate-presentation
John Slick
 
Евгений Капинос "Advanced JPA (Java Persistent API)"
Евгений Капинос "Advanced JPA (Java Persistent API)"Евгений Капинос "Advanced JPA (Java Persistent API)"
Евгений Капинос "Advanced JPA (Java Persistent API)"
Anna Shymchenko
 
Introduction to JPA and Hibernate including examples
Introduction to JPA and Hibernate including examplesIntroduction to JPA and Hibernate including examples
Introduction to JPA and Hibernate including examples
ecosio GmbH
 
Incremental Java Deserialization/Reserialization
Incremental Java Deserialization/ReserializationIncremental Java Deserialization/Reserialization
Incremental Java Deserialization/Reserialization
Bill La Forge
 
Serialization/deserialization
Serialization/deserializationSerialization/deserialization
Serialization/deserialization
Young Alista
 
Entity Persistence with JPA
Entity Persistence with JPAEntity Persistence with JPA
Entity Persistence with JPA
Subin Sugunan
 
Introduction to JPA (JPA version 2.0)
Introduction to JPA (JPA version 2.0)Introduction to JPA (JPA version 2.0)
Introduction to JPA (JPA version 2.0)
ejlp12
 
Intro To Hibernate
Intro To HibernateIntro To Hibernate
Intro To Hibernate
Amit Himani
 
ZODB, the Zope Object Database (May 2003)
ZODB, the Zope Object Database (May 2003)ZODB, the Zope Object Database (May 2003)
ZODB, the Zope Object Database (May 2003)
Kiran Jonnalagadda
 
Serialization in .NET
Serialization in .NETSerialization in .NET
Serialization in .NET
Abhi Arya
 
Powerful persistence layer with Google Guice & MyBatis
Powerful persistence layer with Google Guice & MyBatisPowerful persistence layer with Google Guice & MyBatis
Powerful persistence layer with Google Guice & MyBatis
simonetripodi
 

Viewers also liked (7)

08 Queries
08 Queries08 Queries
08 Queries
Ranjan Kumar
 
Hibernate Session 2
Hibernate Session 2Hibernate Session 2
Hibernate Session 2
b_kathir
 
Hibernate查询
Hibernate查询Hibernate查询
Hibernate查询
llying
 
Hibernate Session 3
Hibernate Session 3Hibernate Session 3
Hibernate Session 3
b_kathir
 
HQL over Tiered Data Warehouse
HQL over Tiered Data WarehouseHQL over Tiered Data Warehouse
HQL over Tiered Data Warehouse
DataWorks Summit
 
Hibernate Session 4
Hibernate Session 4Hibernate Session 4
Hibernate Session 4
b_kathir
 
14 hql
14 hql14 hql
14 hql
thirumuru2012
 
Hibernate Session 2
Hibernate Session 2Hibernate Session 2
Hibernate Session 2
b_kathir
 
Hibernate查询
Hibernate查询Hibernate查询
Hibernate查询
llying
 
Hibernate Session 3
Hibernate Session 3Hibernate Session 3
Hibernate Session 3
b_kathir
 
HQL over Tiered Data Warehouse
HQL over Tiered Data WarehouseHQL over Tiered Data Warehouse
HQL over Tiered Data Warehouse
DataWorks Summit
 
Hibernate Session 4
Hibernate Session 4Hibernate Session 4
Hibernate Session 4
b_kathir
 
Ad

Similar to Hibernate Session 1 (20)

Hibernate 3
Hibernate 3Hibernate 3
Hibernate 3
Rajiv Gupta
 
Hibernate
HibernateHibernate
Hibernate
Murali Pachiyappan
 
Hibernate tutorial
Hibernate tutorialHibernate tutorial
Hibernate tutorial
Mumbai Academisc
 
Hibernate tutorial for beginners
Hibernate tutorial for beginnersHibernate tutorial for beginners
Hibernate tutorial for beginners
Rahul Jain
 
02 java spring-hibernate-experience-questions
02 java spring-hibernate-experience-questions02 java spring-hibernate-experience-questions
02 java spring-hibernate-experience-questions
Dhiraj Champawat
 
Hibernate3 q&a
Hibernate3 q&aHibernate3 q&a
Hibernate3 q&a
Faruk Molla
 
Hibernate
HibernateHibernate
Hibernate
Preetha Ganapathi
 
Hibernate training-topics
Hibernate training-topicsHibernate training-topics
Hibernate training-topics
Vibrant Technologies & Computers
 
Hibernate Interview Questions and Answers
Hibernate Interview Questions and AnswersHibernate Interview Questions and Answers
Hibernate Interview Questions and Answers
AnuragMourya8
 
TY.BSc.IT Java QB U6
TY.BSc.IT Java QB U6TY.BSc.IT Java QB U6
TY.BSc.IT Java QB U6
Lokesh Singrol
 
Hibernate
HibernateHibernate
Hibernate
Sujit Kumar
 
Java hibernate orm implementation tool
Java hibernate   orm implementation toolJava hibernate   orm implementation tool
Java hibernate orm implementation tool
javaease
 
Hibernate
HibernateHibernate
Hibernate
VISHAL DONGA
 
Hibernate
HibernateHibernate
Hibernate
vaishnavmem
 
Module-3 for career and JFSD ppt for study.pptx
Module-3 for career and JFSD ppt for study.pptxModule-3 for career and JFSD ppt for study.pptx
Module-3 for career and JFSD ppt for study.pptx
ViratKohli78
 
Hibernate jj
Hibernate jjHibernate jj
Hibernate jj
Joe Jacob
 
Hibernate ppt
Hibernate pptHibernate ppt
Hibernate ppt
Aneega
 
Hibernate.pdf
Hibernate.pdfHibernate.pdf
Hibernate.pdf
SaadAnsari73
 
Introduction to Hibernate
Introduction to HibernateIntroduction to Hibernate
Introduction to Hibernate
Krishnakanth Goud
 
Hibernate
HibernateHibernate
Hibernate
Ajay K
 
Hibernate tutorial for beginners
Hibernate tutorial for beginnersHibernate tutorial for beginners
Hibernate tutorial for beginners
Rahul Jain
 
02 java spring-hibernate-experience-questions
02 java spring-hibernate-experience-questions02 java spring-hibernate-experience-questions
02 java spring-hibernate-experience-questions
Dhiraj Champawat
 
Hibernate Interview Questions and Answers
Hibernate Interview Questions and AnswersHibernate Interview Questions and Answers
Hibernate Interview Questions and Answers
AnuragMourya8
 
Java hibernate orm implementation tool
Java hibernate   orm implementation toolJava hibernate   orm implementation tool
Java hibernate orm implementation tool
javaease
 
Module-3 for career and JFSD ppt for study.pptx
Module-3 for career and JFSD ppt for study.pptxModule-3 for career and JFSD ppt for study.pptx
Module-3 for career and JFSD ppt for study.pptx
ViratKohli78
 
Hibernate jj
Hibernate jjHibernate jj
Hibernate jj
Joe Jacob
 
Hibernate ppt
Hibernate pptHibernate ppt
Hibernate ppt
Aneega
 
Hibernate
HibernateHibernate
Hibernate
Ajay K
 
Ad

Recently uploaded (20)

RTP Over QUIC: An Interesting Opportunity Or Wasted Time?
RTP Over QUIC: An Interesting Opportunity Or Wasted Time?RTP Over QUIC: An Interesting Opportunity Or Wasted Time?
RTP Over QUIC: An Interesting Opportunity Or Wasted Time?
Lorenzo Miniero
 
GyrusAI - Broadcasting & Streaming Applications Driven by AI and ML
GyrusAI - Broadcasting & Streaming Applications Driven by AI and MLGyrusAI - Broadcasting & Streaming Applications Driven by AI and ML
GyrusAI - Broadcasting & Streaming Applications Driven by AI and ML
Gyrus AI
 
Cybersecurity Threat Vectors and Mitigation
Cybersecurity Threat Vectors and MitigationCybersecurity Threat Vectors and Mitigation
Cybersecurity Threat Vectors and Mitigation
VICTOR MAESTRE RAMIREZ
 
Viam product demo_ Deploying and scaling AI with hardware.pdf
Viam product demo_ Deploying and scaling AI with hardware.pdfViam product demo_ Deploying and scaling AI with hardware.pdf
Viam product demo_ Deploying and scaling AI with hardware.pdf
camilalamoratta
 
Agentic Automation - Delhi UiPath Community Meetup
Agentic Automation - Delhi UiPath Community MeetupAgentic Automation - Delhi UiPath Community Meetup
Agentic Automation - Delhi UiPath Community Meetup
Manoj Batra (1600 + Connections)
 
Design pattern talk by Kaya Weers - 2025 (v2)
Design pattern talk by Kaya Weers - 2025 (v2)Design pattern talk by Kaya Weers - 2025 (v2)
Design pattern talk by Kaya Weers - 2025 (v2)
Kaya Weers
 
UiPath Agentic Automation: Community Developer Opportunities
UiPath Agentic Automation: Community Developer OpportunitiesUiPath Agentic Automation: Community Developer Opportunities
UiPath Agentic Automation: Community Developer Opportunities
DianaGray10
 
Com fer un pla de gestió de dades amb l'eiNa DMP (en anglès)
Com fer un pla de gestió de dades amb l'eiNa DMP (en anglès)Com fer un pla de gestió de dades amb l'eiNa DMP (en anglès)
Com fer un pla de gestió de dades amb l'eiNa DMP (en anglès)
CSUC - Consorci de Serveis Universitaris de Catalunya
 
Integrating FME with Python: Tips, Demos, and Best Practices for Powerful Aut...
Integrating FME with Python: Tips, Demos, and Best Practices for Powerful Aut...Integrating FME with Python: Tips, Demos, and Best Practices for Powerful Aut...
Integrating FME with Python: Tips, Demos, and Best Practices for Powerful Aut...
Safe Software
 
The No-Code Way to Build a Marketing Team with One AI Agent (Download the n8n...
The No-Code Way to Build a Marketing Team with One AI Agent (Download the n8n...The No-Code Way to Build a Marketing Team with One AI Agent (Download the n8n...
The No-Code Way to Build a Marketing Team with One AI Agent (Download the n8n...
SOFTTECHHUB
 
Does Pornify Allow NSFW? Everything You Should Know
Does Pornify Allow NSFW? Everything You Should KnowDoes Pornify Allow NSFW? Everything You Should Know
Does Pornify Allow NSFW? Everything You Should Know
Pornify CC
 
Build With AI - In Person Session Slides.pdf
Build With AI - In Person Session Slides.pdfBuild With AI - In Person Session Slides.pdf
Build With AI - In Person Session Slides.pdf
Google Developer Group - Harare
 
Unlocking Generative AI in your Web Apps
Unlocking Generative AI in your Web AppsUnlocking Generative AI in your Web Apps
Unlocking Generative AI in your Web Apps
Maximiliano Firtman
 
AI You Can Trust: The Critical Role of Governance and Quality.pdf
AI You Can Trust: The Critical Role of Governance and Quality.pdfAI You Can Trust: The Critical Role of Governance and Quality.pdf
AI You Can Trust: The Critical Role of Governance and Quality.pdf
Precisely
 
Everything You Need to Know About Agentforce? (Put AI Agents to Work)
Everything You Need to Know About Agentforce? (Put AI Agents to Work)Everything You Need to Know About Agentforce? (Put AI Agents to Work)
Everything You Need to Know About Agentforce? (Put AI Agents to Work)
Cyntexa
 
The Changing Compliance Landscape in 2025.pdf
The Changing Compliance Landscape in 2025.pdfThe Changing Compliance Landscape in 2025.pdf
The Changing Compliance Landscape in 2025.pdf
Precisely
 
UiPath Agentic Automation: Community Developer Opportunities
UiPath Agentic Automation: Community Developer OpportunitiesUiPath Agentic Automation: Community Developer Opportunities
UiPath Agentic Automation: Community Developer Opportunities
DianaGray10
 
Webinar - Top 5 Backup Mistakes MSPs and Businesses Make .pptx
Webinar - Top 5 Backup Mistakes MSPs and Businesses Make   .pptxWebinar - Top 5 Backup Mistakes MSPs and Businesses Make   .pptx
Webinar - Top 5 Backup Mistakes MSPs and Businesses Make .pptx
MSP360
 
Financial Services Technology Summit 2025
Financial Services Technology Summit 2025Financial Services Technology Summit 2025
Financial Services Technology Summit 2025
Ray Bugg
 
Mastering Testing in the Modern F&B Landscape
Mastering Testing in the Modern F&B LandscapeMastering Testing in the Modern F&B Landscape
Mastering Testing in the Modern F&B Landscape
marketing943205
 
RTP Over QUIC: An Interesting Opportunity Or Wasted Time?
RTP Over QUIC: An Interesting Opportunity Or Wasted Time?RTP Over QUIC: An Interesting Opportunity Or Wasted Time?
RTP Over QUIC: An Interesting Opportunity Or Wasted Time?
Lorenzo Miniero
 
GyrusAI - Broadcasting & Streaming Applications Driven by AI and ML
GyrusAI - Broadcasting & Streaming Applications Driven by AI and MLGyrusAI - Broadcasting & Streaming Applications Driven by AI and ML
GyrusAI - Broadcasting & Streaming Applications Driven by AI and ML
Gyrus AI
 
Cybersecurity Threat Vectors and Mitigation
Cybersecurity Threat Vectors and MitigationCybersecurity Threat Vectors and Mitigation
Cybersecurity Threat Vectors and Mitigation
VICTOR MAESTRE RAMIREZ
 
Viam product demo_ Deploying and scaling AI with hardware.pdf
Viam product demo_ Deploying and scaling AI with hardware.pdfViam product demo_ Deploying and scaling AI with hardware.pdf
Viam product demo_ Deploying and scaling AI with hardware.pdf
camilalamoratta
 
Design pattern talk by Kaya Weers - 2025 (v2)
Design pattern talk by Kaya Weers - 2025 (v2)Design pattern talk by Kaya Weers - 2025 (v2)
Design pattern talk by Kaya Weers - 2025 (v2)
Kaya Weers
 
UiPath Agentic Automation: Community Developer Opportunities
UiPath Agentic Automation: Community Developer OpportunitiesUiPath Agentic Automation: Community Developer Opportunities
UiPath Agentic Automation: Community Developer Opportunities
DianaGray10
 
Integrating FME with Python: Tips, Demos, and Best Practices for Powerful Aut...
Integrating FME with Python: Tips, Demos, and Best Practices for Powerful Aut...Integrating FME with Python: Tips, Demos, and Best Practices for Powerful Aut...
Integrating FME with Python: Tips, Demos, and Best Practices for Powerful Aut...
Safe Software
 
The No-Code Way to Build a Marketing Team with One AI Agent (Download the n8n...
The No-Code Way to Build a Marketing Team with One AI Agent (Download the n8n...The No-Code Way to Build a Marketing Team with One AI Agent (Download the n8n...
The No-Code Way to Build a Marketing Team with One AI Agent (Download the n8n...
SOFTTECHHUB
 
Does Pornify Allow NSFW? Everything You Should Know
Does Pornify Allow NSFW? Everything You Should KnowDoes Pornify Allow NSFW? Everything You Should Know
Does Pornify Allow NSFW? Everything You Should Know
Pornify CC
 
Unlocking Generative AI in your Web Apps
Unlocking Generative AI in your Web AppsUnlocking Generative AI in your Web Apps
Unlocking Generative AI in your Web Apps
Maximiliano Firtman
 
AI You Can Trust: The Critical Role of Governance and Quality.pdf
AI You Can Trust: The Critical Role of Governance and Quality.pdfAI You Can Trust: The Critical Role of Governance and Quality.pdf
AI You Can Trust: The Critical Role of Governance and Quality.pdf
Precisely
 
Everything You Need to Know About Agentforce? (Put AI Agents to Work)
Everything You Need to Know About Agentforce? (Put AI Agents to Work)Everything You Need to Know About Agentforce? (Put AI Agents to Work)
Everything You Need to Know About Agentforce? (Put AI Agents to Work)
Cyntexa
 
The Changing Compliance Landscape in 2025.pdf
The Changing Compliance Landscape in 2025.pdfThe Changing Compliance Landscape in 2025.pdf
The Changing Compliance Landscape in 2025.pdf
Precisely
 
UiPath Agentic Automation: Community Developer Opportunities
UiPath Agentic Automation: Community Developer OpportunitiesUiPath Agentic Automation: Community Developer Opportunities
UiPath Agentic Automation: Community Developer Opportunities
DianaGray10
 
Webinar - Top 5 Backup Mistakes MSPs and Businesses Make .pptx
Webinar - Top 5 Backup Mistakes MSPs and Businesses Make   .pptxWebinar - Top 5 Backup Mistakes MSPs and Businesses Make   .pptx
Webinar - Top 5 Backup Mistakes MSPs and Businesses Make .pptx
MSP360
 
Financial Services Technology Summit 2025
Financial Services Technology Summit 2025Financial Services Technology Summit 2025
Financial Services Technology Summit 2025
Ray Bugg
 
Mastering Testing in the Modern F&B Landscape
Mastering Testing in the Modern F&B LandscapeMastering Testing in the Modern F&B Landscape
Mastering Testing in the Modern F&B Landscape
marketing943205
 

Hibernate Session 1

  • 2. Object Relational Mapping ORM refers to the technique of peristing objects in relational database using metadata that describes mapping between objects and database tables. Performs automatic dirty checking, lazy associations etc.., Has a language or API for specifying queries that refer to classes and properties.
  • 3. Hibernate Hibernate is a complete ORM tool. 1. Integrates elegantly with all popular Java EE application servers , web containers and also works in standalone applications . 2. Natural programming model – Support for Inheritance, polymorphism, composition and the Java collections framework 3. Scalability - has a dual-layer cache architecture 4. Unique query language which supports writing query on objects - HQL
  • 4. Contd.., 5. Productivity – Eliminates much of Persistence-related code. 6. Vendor Independence 7. Solves portability issues
  • 5. Hibernate Object Hibernate object is a simple POJO with some recommended good practices as below: There must be a default constructor for the class. There must be accessors and optional mutators for all the instance variables of the class. (Immutable objcets are exceptional cases). Recommended - POJO implements Serializable.
  • 6. Hibernate Object (contd..,) Recommended - POJO should ideally have an ID instance variable, usually of type Long. Recommended - The mutator for the id property should be private, not public.
  • 7. Hibernate Object (contd..,) You should decide on a business key for the object and implement the equals and hashCode methods for it. You should add any extra type specific constructors (which should leave the id field null) and business rule methods you like. You should not make the class final if you want to be able to use lazy loading for objects of the class.
  • 8. Hibernate Architecture terminology SessionFactory A threadsafe (immutable) cache of compiled mappings for a single database. A factory for Session and a client of ConnectionProvider. Might hold an optional (second-level) cache of data that is reusable between transactions, at a process- or cluster-level. Session A single-threaded, short-lived object representing a conversation between the application and the persistent store. Wraps a JDBC connection. Factory for Transaction. Holds a mandatory (first-level) cache of persistent objects, used when navigating the object graph or looking up objects by identifier.
  • 9. Database Interaction Pattern 1. Create a Configuration object, load the configuration parameters from the hibernate.properties and adjust them as required. 2. Create a SessionFactory object from the Configuration object. The SessionFactory object is a heavyweight, thread safe object. You would normally share one such object between all your threads in a web application. 3. For each unit of work (normally one use case) use the SessionFactory object to obtain a Session object. This is an extremely lightweight, non-thread safe object. It will be associated with a database connection but it only obtains that connection lazily, i.e., only when (and if) it is required. Session objects must not be shared between different threads. 4. Inside a try block, get a Transaction object by calling beginTransaction() on the Session object.
  • 10. Hibernate - Database Interaction Pattern (contd..,) 5. Interact with the database: explicitly by calling methods of Session to associate objects to the database (i.e., map them to the database), execute queries, load, save, delete mapped objects etc. implicitly by calling property mutators on mapped objects that will lead to the database being updated. implicitly by referencing non-mapped objects from mapped objects which (in certain circumstances) can cause the non-mapped objects to be added to the database. implicitly by unreferencing mapped objects from other mapped objects which (in certain circumstances) can cause the unreferenced objects to be deleted from the database. 6. Call commit() on the Transaction object and close the try block, handling exceptions and closing the Session object in the usual way.
  • 11. Hibernate Object - Life Cycle
  • 12. Hibernate Object - Life Cycle Transient objects DO NOT (yet) have any association with the database.They act like any normal Java object and are not saved to the database. Persistent objects DO have an association with the database. Detached objects are objects that were persistent but no longer have a connection to a Session object (usually because you have closed the session). A detached object may be re-attached later to another Session object to become persistent again.
  • 13. Built-in Mapping Types – Primitive Types
  • 14. Built-in Mapping Types – Date and Time Types
  • 15. Session I - Demo
  翻译: