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

What's the use of Hibernate saveOrUpdate?

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

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

So that's what update does, and it only needs to be written when a PO object is synchronized across the Session. A PO object does not need to write update when it does not need to manage state across Session.

Let's talk about the use of Hibernate saveOrUpdate:

The difference between Hibernate saveOrUpdate and update lies in what strategy Hibernate takes towards PO in PO state management across Session.

For example, when you write a DAOImpl, ask the cat object to add a mate, as defined below:

Public void addMate (Cat cat, Mate mate) {Session session =...; Transacton tx =...; session.update (cat); cat.addMate (mate); tx.commit (); session.close ();}

Here comes the problem: the above code runs correctly on the necessary premise that the method call parameter cat object must be a persisted PO, that is, it should be queried from the database before it can be used in this way. But the programmer of the business layer obviously does not know this internal mystery. If his business adds a cat now, and then adds its mate, he will obviously call it like this, new a cat object, and then addMate:

Cat cat = new Cat (); cat.setXXX (); daoimpl.addMate (cat,mate)

But please note that this cat object is just a VO, it has not been persisted, it is not PO, it is not qualified to call the addMate method, so calling the addMate method will not really send update's sql to the database, this cat object must be save to the database before it is qualified as an addMate.

You have to do this:

Cat cat = new Cat (); cat.setXXX (); daoimpl.addCat (cat); daoimpl.addMate (cat, mate)

Therefore, programmers in the business layer are required to know what state the cat object is in, whether it is the third or the third. If it is a * * species, it is necessary to first save, and then addMate; if it is the third kind, directly addMate.

But the deadliest thing is that if the whole software is layered and the programmer of the business layer gets the cat object, it may also be the cat passed from the upper Web application layer, and he does not know whether the cat is VO, has not been persisted, or has been persisted, then he has no way to write a program at all.

So this kind of DAOImpl is obviously problematic. It will cause a lot of programming traps for programmers in the business layer. The programmer in the business layer must deeply understand how each DAO he calls manages the state of the PO object, and must deeply understand the exact state of his PO object at any time in order to ensure the correctness of programming. Obviously, this cannot be done, but with Hibernate saveOrUpdate These problems can be easily solved.

At this point, the study of "what is the use of Hibernate saveOrUpdate" 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