Home > Java > javaTutorial > body text

How to use query strategy and crawling strategy in Java Hibernate

王林
Release: 2023-05-25 11:21:08
forward
502 people have browsed it

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 Query

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();
Copy after login

In the above example, we use the get() method to retrieve a Student with ID 1 object.

Object navigation retrieval

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();
Copy after login

In the above example, we retrieve a Student object and use the getAddress() method to get the student's address.

HQL Retrieval

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();
Copy after login

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 Retrieval

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();
Copy after login

In the above example, we use the createCriteria() method to create a Criteria object and use list() Method to get the result list.

SQL retrieval

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();
Copy after login

In the above example, we create a SQL query using the createSQLQuery() method and use addEntity() Method maps the result to the Student class.

Fetch strategy

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.

Catch immediately

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;
Copy after login

In the above example, we set the fetch attribute to EAGER, indicating the use of immediate fetching.

Delayed 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;
Copy after login

In the above example, we set the fetch attribute to LAZY, indicating the use of delayed fetching.

Batch crawling

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;
Copy after login

In the above example, we add the @BatchSize annotation to the @OneToMany annotation to indicate the use of batch Grab.

Lazy loading

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();
Copy after login

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!

Related labels:
source:yisu.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!