Using object-oriented methods to access the database is provided by Hibernate, a popular ORM framework. In Hibernate, we can use a variety of query methods to retrieve data, including OID query, object navigation retrieval, HQL retrieval, QBC retrieval and SQL retrieval.
OID (Object Identifier) is the unique identifier of each persistent object in Hibernate. You can use OID queries to retrieve a specific persistent object. When using OID query, we need to use the load()
or get()
method. The difference between these two methods is that the load()
method will load the object when needed, while the get()
method will load the object immediately. The following is an example of using the get()
method:
Session session = HibernateUtil.getSessionFactory().getCurrentSession(); session.beginTransaction(); Student student = (Student) session.get(Student.class, 1); session.getTransaction().commit();
In the above example, we use the get()
method to retrieve a Student with ID 1 object.
Object navigation retrieval allows us to retrieve data through the relationships between objects. For example, if we have a Student class and an Address class, and there is a one-to-one relationship between them, we can use object navigation retrieval to retrieve the address of a specific Student object. The following is an example of retrieval using object navigation:
Session session = HibernateUtil.getSessionFactory().getCurrentSession(); session.beginTransaction(); Student student = (Student) session.get(Student.class, 1); Address address = student.getAddress(); session.getTransaction().commit();
In the above example, we retrieve a Student object and use the getAddress()
method to get the student's address.
HQL (Hibernate Query Language) is an object-based query language, which is similar to SQL, but more object-oriented. HQL uses classes and properties from Hibernate mapping files to build queries. The following is an example of using HQL to query all Student objects:
Session session = HibernateUtil.getSessionFactory().getCurrentSession(); session.beginTransaction(); Query query = session.createQuery("from Student"); List<Student> students = query.list(); session.getTransaction().commit();
In the above example, we use the createQuery()
method to create a HQL query, and then use list()
Method to get the result list.
QBC (Query By Criteria) is an object-based query method that uses the Criteria API to build queries. Using the Criteria API can avoid some common query errors because it is a type-safe way of querying. The following is an example of using QBC to query all Student objects:
Session session = HibernateUtil.getSessionFactory().getCurrentSession(); session.beginTransaction(); Criteria criteria = session.createCriteria(Student.class); List<Student> students = criteria.list(); session.getTransaction().commit();
In the above example, we use the createCriteria()
method to create a Criteria object and use list()
Method to get the result list.
Although Hibernate supports a variety of object-based query methods, in some cases we may need to perform some complex SQL queries. In this case we can use SQL query to retrieve the data. The following is an example of querying all Student objects using SQL:
Session session = HibernateUtil.getSessionFactory().getCurrentSession(); session.beginTransaction(); SQLQuery query = session.createSQLQuery("select * from Student"); query.addEntity(Student.class); List<Student> students = query.list(); session.getTransaction().commit();
In the above example, we create a SQL query using the createSQLQuery()
method and use addEntity()
Method maps the result to the Student class.
The fetch strategy is the mechanism used by Hibernate to handle object relationships. Hibernate provides three data extraction methods: immediate extraction, delayed extraction and batch extraction.
When retrieving an object, grabbing immediately means that Hibernate will immediately retrieve all associated objects of the object. This crawling strategy can cause performance issues because it can cause large amounts of data to be transferred. The following is an example of using immediate fetching:
@ManyToOne(fetch = FetchType.EAGER) private Address address;
In the above example, we set the fetch
attribute to EAGER
, indicating the use of immediate fetching.
Hibernate's delayed fetching refers to only retrieving the entity itself and not retrieving associated entities. When we need to access related objects, Hibernate will query these objects again. This crawling strategy improves performance because it avoids unnecessary data transfers. The following is an example of using delayed fetching:
@ManyToOne(fetch = FetchType.LAZY) private Address address;
In the above example, we set the fetch
attribute to LAZY
, indicating the use of delayed fetching.
Batch crawling is a crawling strategy that allows us to retrieve the associated objects of multiple objects at once. This crawling strategy improves performance because it reduces the number of multiple retrievals. The following is an example of using batch fetching:
@OneToMany(mappedBy = "student", fetch = FetchType.LAZY) @BatchSize(size = 10) private List<Grade> grades;
In the above example, we add the @BatchSize
annotation to the @OneToMany
annotation to indicate the use of batch Grab.
Lazy loading in Hibernate means that other objects associated with an object are loaded only when needed. This mechanism can reduce unnecessary data transmission and improve performance. The following is an example of using lazy loading:
Session session = HibernateUtil.getSessionFactory().getCurrentSession(); session.beginTransaction(); Student student = (Student) session.load(Student.class, 1); Address address = student.getAddress(); session.getTransaction().commit();
In the above example, we use the load()
method to retrieve a Student object with ID 1, and use getAddress( )
method to get the student's address. Since we are using lazy loading, Hibernate will only load the address object when needed.
The above is the detailed content of How to use query strategy and crawling strategy in Java Hibernate. For more information, please follow other related articles on the PHP Chinese website!