Hibernate series notes (1) --- add, delete, modify, and query for Hibernate

Source: Internet
Author: User

Hibernate series notes (1) --- add, delete, modify, and query for Hibernate

Hibernate addition, deletion, modification, and query

1. First, we need to know what Hibernate is.

Hibernate is a lightweight ORMapping object. It is mainly used to implement ing between Java and database tables. In addition, it also provides data query and data acquisition methods,

This greatly reduces the time required for manual use of SQL and JDBC to process data during development, freeing programmers from 95% of tasks.

2. What is ORM Object-Relational-Mapping Object relationship ing?

ORM: maps data tables to database tables through java objects. You can operate data tables through Java objects. (If you are using Dbutils, you still need to write SQL statements in the Java class, instead of using orm)

Hibernate is a complete ORM framework that only requires operations on objects to generate the underlying SQL.

Next, go to the topic:

Let's take a look at the basic process of using hibernate! Below is a simple flowchart

1. Create a project:

Use myeclipse to create a web project

2. Import hibernate-related racks to the Project

Step 3: configure the file hibernate

There are two forms of hibernate configuration!

One is to use the hibernate. properties file!

The other is to use the hibernate. cfg. xml file! Here we use hibernate. cfg. xml for configuration

A. Using the properties method, you must manually program the loading of hbm files or persistence classes.

B. You can add hbm files by using XML configuration.

Create an xml file named hibernate in the src directory. cfg. xml (you can also do not call this name, But modify it in the Code), copy the following content:

<? Xml version = "1.0" encoding = "UTF-8"?> <! DOCTYPE hibernate-configuration PUBLIC "-// Hibernate/Hibernate Configuration DTD 3.0 // EN" "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"> Configure hibernate. cfg. xml

Note: you do not need to manually create the customer table, but the database hibernateexec requires manual creation.

Step 4. Create an object and a ing File

Public class Customer {private int id; private String name; private int age; private String city; private String addr;}/** provides the set and get Methods */
Customer entity

The ing file and object are in the same package:

<? Xml version = "1.0" encoding = "UTF-8"?> <! DOCTYPE hibernate-mapping PUBLIC "-// Hibernate/Hibernate DTD ing DTD 3.0 // EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> Customer. hbm. xml

Step 5: Create a SessionFactory object

Step 6: Get the Session object for related operations

Step 5 and Step 6 are with me. Step 6 we find that no matter whether you add, delete, modify, and query the previous four steps are the same. We can actually extract a tool class and then call it to accelerate efficiency.

Import java. util. list; import org. hibernate. query; import org. hibernate. SQLQuery; import org. hibernate. session; import org. hibernate. sessionFactory; import org. hibernate. transaction; import org. hibernate. cfg. configuration; import org. junit. test; import com. study. model. customer; public class HibernateTest {/** save data */@ Test public void testInsert () {// instantiate configuration object loading ing file loading hibernate. cfg. xml Configuration configuration = new Configuration (). configure (); // create the session factory SessionFactory sessionFactory = configuration. buildSessionFactory (); // creates a Session session Session = sessionFactory. openSession (); // start Transaction transaction Transaction = session. beginTransaction (); // write your own logic code Customer customer = new Customer (); customer. setName ("Xiao Huang"); customer. setAge (40); customer. setCity ("Beijing"); // Save the session directly. save (customer); // submit the transaction. commit (); session. close (); sessionFactory. close () ;}// query all @ Test public void testFindAllByHQL () {// instantiate the configuration object, load the ing file, and load hibernate. cfg. xml Configuration configuration = new Configuration (). configure (); // create the session factory SessionFactory sessionFactory = configuration. buildSessionFactory (); // creates a Session session Session = sessionFactory. openSession (); // start Transaction transaction Transaction = session. beginTransaction (); // compile an HQL Statement (String hql = "from Customer" for queries of classes and attributes "; // here, the Customer is not the table name, but the class name Query Customer query Query = session. createQuery (hql); List <Customer> MERs = query. list (); System. out. println (MERS mers); // submit the transaction. commit (); session. close (); sessionFactory. close () ;}// Delete @ Test public void testDelete () {// instantiate configuration object loading ing file loading hibernate. cfg. xml Configuration configuration = new Configuration (). configure (); // create the session factory SessionFactory sessionFactory = configuration. buildSessionFactory (); // creates a Session session Session = sessionFactory. openSession (); // start Transaction transaction Transaction = session. beginTransaction (); Customer customer Customer = new customer (); Customer. setId (2); session. delete (customer); // submit the transaction. commit (); session. close (); sessionFactory. close () ;}// modify @ Test public void testUpdate () {// instantiate the configuration object to load the hibernate ing file to hibernate. cfg. xml Configuration configuration = new Configuration (). configure (); // create the session factory SessionFactory sessionFactory = configuration. buildSessionFactory (); // creates a Session session Session = sessionFactory. openSession (); // start Transaction transaction Transaction = session. beginTransaction (); Customer customer Customer = (Customer) session. get (Customer. class, 2); customer. setCity ("Hangzhou"); session. update (customer); // submit the transaction. commit (); session. close (); sessionFactory. close () ;}// query by id @ Test public void testFindById () {// instantiate configuration object loading ing file loading hibernate. cfg. xml Configuration configuration = new Configuration (). configure (); // create the session factory SessionFactory sessionFactory = configuration. buildSessionFactory (); // creates a Session session Session = sessionFactory. openSession (); // start Transaction transaction Transaction = session. beginTransaction (); Customer customer Customer = (Customer) session. get (Customer. class, 1); System. out. println (customer); // submit the transaction. commit (); session. close (); sessionFactory. close ();}}
Hibernate addition, deletion, modification, and query

Running Effect: When you run the first add user operation, the database automatically creates a customer table and adds data to the table.

In this way, you can use hibernate to perform basic addition, deletion, modification, and query.

This article is here. If you have any shortcomings, please kindly advise. Thank you!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.