Hibernate Principle and Framework Structure to Add, Delete and Amend Query Statements

Keywords: Session Hibernate Database xml

What is Hibernate?

hibernate is the framework of data access layer. It encapsulates JDBC and is an object-oriented solution for database access.

The role of Hibernate?

Hibernate can access objects directly. Hibernate automatically replaces this access with sql execution, which achieves the purpose of indirect access to the database and simplifies the code development of data access layer.

The advantages of Hibernate over JDBC are:

Automatic generation of sql statements; automatic assignment of parameters; automatic conversion of resultSet result sets into entity objects; using a consistent method to operate the database, good portability.

What is ORM?

ORM: Object relational mapping refers to the mapping between java objects and relational databases.

ORM thought: It is the idea of converting object and database data to each other. OrM is usually realized by configuration + reflection.

The design principle of Hibernate framework:

hibernate uses ORM to encapsulate Jdbc, which solves the mapping problem between object and database. hibernate provides a series of API s that allow us to access entity objects directly, then convert them into sql and execute them according to ORM mapping relationship, so as to achieve the purpose of accessing database.

Hibernate Framework Architecture
2.1 Master Profile
Hibernate's main configuration file is an xml file, usually named Hibernate.cfg.xml
In this file, you can configure database connection parameters, Hibernate framework parameters, and mapping relational files


2.2 Entity Classes
Entity classes are java types corresponding to database tables, which are object types used to encapsulate database records


2.3 Mapping Relational File
The mapping relationship file specifies the correspondence between the entity class and the data table, and between the attributes in the class and the fields in the table.
Hibernate uses XML files to describe mapping relationships. Files are usually named "entity class. hbm.xml" and placed in the same path as entity classes.

2.4 underlying API
Hibernate provides a series of underlying API s to access the database based on ORM.
These API s mainly parse mapping relationship files. According to the parsed content, sql statements are generated dynamically, and properties and fields are mapped automatically.


Hibernate usage steps
1 Import Hibernate package and database driver package
2. Introduce Hibernate master configuration file hibernate.cfg.xml
3 Create Entity Classes
4 Create mapping relationship files
5 Use Hibernate's common API to add, delete, and modify

Add:

	public void add(){
		Session session = HibernateUtil.getSession();
		Transaction tran = session.beginTransaction();
		Music music = new Music();
		music.setMusicName("Chengdu");
		music.setSinger("Zhao Lei");
		music.setSize("3.2M");
		try {
			session.save(music);
			tran.commit();
		} catch (Exception e) {
			e.printStackTrace();
			tran.rollback();
		}finally{
			session.close();
		}
	}

Delete:

public void delete(){
		Session session = HibernateUtil.getSession();
		Transaction tran = session.beginTransaction();
		Music music = new Music();
		music.setId(11);
		session.delete(music);
		tran.commit();
	}

Change:

public void update(){
		Session session = HibernateUtil.getSession();
		Transaction tran = session.beginTransaction();
		Music music = (Music)session.get(Music.class, 4);
		music.setMusicName("Counter war");
		music.setSinger("Zhang Jie");
		session.save(music);
		tran.commit();
	}

check

	public void findAll(){
		Session session = HibernateUtil.getSession();
		Query query = session.createQuery("from Music");
		List<Music> list = query.list();
		for(int i = 0;i < list.size();i++)
			System.out.println(list.get(i).getId()+ "--" + list.get(i).getMusicName() );
		
	}
	public void findById(){
		Session session = HibernateUtil.getSession();
		Music m = (Music) session.get(Music.class, 2);
		System.out.println(m.getSinger()+"---");
	}





Posted by narch31 on Mon, 17 Jun 2019 16:29:33 -0700