Hibernate: Participant Guide
Lesson 2: Acquiring Started with Hibernate
You can utilize Hibernate as an ORM model in a Java application by supplying function metadata through XML-based constellation files or by utilizing Java Persistence API ( JPA ) notes in the relentless categories. In add-on to configuring the function metadata [ PR1 ] , you need to configure Hibernate on how to utilize the database that your application will entree. You can configure Hibernate through a bequest Java belongingss file or an XML-based Hibernate constellation file. After you configure Hibernate, you can utilize the categories and interfaces of the Hibernate API to manage the continuity demands of your application.
In this lesson, we will larn about the minutess in Hibernate and how to log into Hibernate.Lesson AimsAt the terminal of this lesson, you will be able to:
- Describe object-relational function in Hibernate through constellation files and notes
- Explain Hibernate session mill and session
- Describe how to pull off minutess in Hibernate
- Integrate Spring with Hibernate
- Explain how to log SQL questions through the Hibernate constellation file
- Use log4j to execute logging
Subject 1: Function Concepts and Configuration
The Hibernate model allows you to specify how relentless categories should be mapped with database tabular arraies. You can specify the function information of a relentless category in an XML-based constellation file. The sentence structure for calling this function file is as follows:
Code [ PR3 ]Segment 2.3: The Employee.hbm.xml FileIn Code Segment 2.
3, the Employee category contains the Idaho, name, section, and age belongingss along with their corresponding getter and compositor methods.The function file in Code Segment 2.3 contains the function of the Employee category with the employeetbl tabular array. This file contains the & A ; lt ; hibernate-mapping & A ; gt ; root component that contains the undermentioned sub-elements:
- & A ; lt ; category & A ; gt ;: Maps the Employee category to the employeetbl tabular array. This component contains the & A ; lt ; Idaho & A ; gt ; element that maps the id belongings of the Employee category to the EMP_ID primary cardinal column of the tabular array. The & A ; lt ; generator & A ; gt ; component has a category property that specifies native as an alias name of a org.
hibernate.id.IdentifierGenerator execution. The native value for the category property instructs Hibernate to utilize an appropriate cardinal coevals algorithm, such as individuality, sequence, or hilo [ PR4 ] based on the capablenesss of the database.
- & A ; lt ; belongings & A ; gt ;: Is used to map the belongingss of the Employee category with the specific columns of the employeetbl tabular array.
For illustration, the name belongings of the Employee category, which is of type String, is mapped to the EMP_NAME column of the employeetbl tabular array.
Configuration SettingsIn add-on to the function information, you besides need to supply the constellation scenes related to database connectivity, mapping paperss, and other parametric quantities that affect the behavior of Hibernate at runtime. You can stipulate the constellation scenes in a Java belongingss file, namedHibernate.propertiesor an XML-based constellation file, namedHibernate.cfg.
xml.Hibernate provides default values for most of the constellation scenes. However, you need to explicitly stipulate scenes, such as the database URL, user name, watchword, and the function files to utilize, which are required by your application to put to death.Code Segment 2.4 shows the Hibernate constellation file that defines how to link with the trial MySQL database to prevail an Employee object whose function is defined in theEmployee.