Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to understand Hibernate Technology

2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly introduces "how to understand Hibernate technology". In daily operation, I believe many people have doubts about how to understand Hibernate technology. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful for you to answer the doubts about "how to understand Hibernate technology"! Next, please follow the editor to study!

Traditional MVC development model

❝M:Model includes pojo, service, and dao.

V:View includes jsp, html and module engine.

C:Controll includes our technology for dynamic network resources: Servlet. ❞

The role of SSH Framework in Development

❝S:Struts/SpringMVC: it actually solves the problem of our controller (it can simply be thought of as an Servlet package).

Spring: integrate the rest of the third-party framework to provide transactions for the Srvice layer.

Hibernate: it is actually the solution of the DAO layer. ❞

What is Hibernate?

1.Hibernate is a non-intrusive ORMapping framework

Non-intrusive framework: when we use this framework, we do not need to inherit or implement the classes or interfaces in this framework. This type of framework is called non-intrusive framework. Non-intrusive frameworks are better decoupled from the original framework when used.

Intrusive framework: when we use this framework, we need to inherit or implement some classes or interfaces in this framework, which is called intrusive framework.

2.ORMapping parsing

O:Object

R:Relation

M:Mapping mapping

A.Hibernate can map Java objects to a database through mapped relationships.

B.Hibernate can map database data to Java objects through mapping relationships.

Hibernate is a technology that can manipulate the database by manipulating Java objects.

What can Hibernate do?

To put it simply: to achieve all the operations of the database (CRUD) is not only a solution of the original DAO layer, but also a substitute.

Simple use of Hibernate

"easy to use:"

Guide the package and put the downloaded required+jpa-related packages into a file

Create a hibernate.cfg.xml configuration file under src

Com.mysql.jdbc.Driver jdbc:mysql:///qianyu root root org.hibernate.dialect.MySQL5Dialect update

Create an object of the class of Java

Public class User implements Serializable {private static final long serialVersionUID =-6949433888868762441L; private int uId; private String userName; private String userPwd; public User (int uId, String userName, String userPwd) {super (); this.uId = uId; this.userName = userName; this.userPwd = userPwd;} public User () {super ();} public int getuId () {return uId;} public void setuId (int uId) {this.uId = uId } public String getUserName () {return userName;} public void setUserName (String userName) {this.userName = userName;} public String getUserPwd () {return userPwd;} public void setUserPwd (String userPwd) {this.userPwd = userPwd;} @ Override public String toString () {return "User [uId=" + uId + ", userName=" + userName + ", userPwd=" + userPwd + "]";}}

Write test classes

@ Test public void testHelloWord () throws Exception {/ / introduce the configuration file Configuration cfg=new Configuration (). Configure ("hibernate.cfg.xml"); / / create SessionFactory object SessionFactory sf=cfg.buildSessionFactory (); / / create session Session session=sf.openSession (); / / Open transaction session.beginTransaction (); / / Operand object User user=new User (1, "Xiaoyu", "110"); / / start operation session.save (user) / / commit the transaction session.getTransaction () .commit (); session.close ();}

Detailed explanation of hibernate.cfg.xml configuration File in Hibernate

Com.mysql.jdbc.Driver jdbc:mysql:///qianyu root root org.hibernate.dialect.MySQL5Dialect update true true

Detailed explanation of xxx.hbm.xml configuration file of Hibernate

Implementation of CRUD in Hibernate

Add data session.save (user); session.persist (user); modify data session.beginTransaction (); / query data (active loading) User user=session.get (User.class,1); / / support lazy loading user.setUserName ("xxxxx"); user.setUserPwd ("yyyyy"); session.getTransaction (). Commit () Data query / / query data (active loading) User user=session.get (User.class,1); / / lazy loading User user2=session.load (User.class,1) is supported below; delete data / / query data (active loading) User user=session.get (User.class,1); session.delete (user)

The compilation of help classes in Hibernate

Public class HibernateUtils {private static Configuration cfg=null; private static SessionFactory sf=null; private static ThreadLocal threadLocal=null; static {/ / initialize the thread's local variable threadLocal=new ThreadLocal (); / load the configuration file cfg=new Configuration (). Configure ("config/hibernate.cfg.xml"); / / generate our factory sf=cfg.buildSessionFactory () } / * how to get session * @ Title: getSession * @ Description: TODO * @ param: @ return * @ return: Session * @ throws * / public static Session getSession () {Session session=threadLocal.get (); if (null==session) {session=sf.openSession (); session.beginTransaction (); threadLocal.set (session);} return session } / * close Session * @ Title: close * @ Description: TODO * @ param: * @ return: void * @ throws * / public static void close () {Session session=threadLocal.get (); if (null session session) {session.getTransaction () .commit (); session.close (); threadLocal.remove ();}

Matters needing attention

Hongmeng official Strategic Cooperation to build HarmonyOS Technology Community

The parenthesis in our hibernate.cfg.xml configuration file is hibernate. It can be omitted.

Why can you query the data without opening the transaction? Because there is a read-only transaction in the default Hibernate, and the read-only transaction can complete the read operation of the data. If you want to complete the addition, deletion and modification, then you need to read and write the transaction. At this time, you need to open the transaction.

The difference between Save and Persist

When Save saves data, if id is self-growing, you are right for both a given id and an ungiven id.

Persist when saving data, if the id is self-growing, you will report an error when you give id.

At this point, the study on "how to understand Hibernate technology" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report