In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly explains "Hibernate class increase, delete, modify and query operation", the explanation content in the article is simple and clear, easy to learn and understand, please follow the idea of Xiaobian slowly in-depth, together to study and learn "Hibernate class increase, delete, modify and query operation"!
Hibernate operations are roughly add, delete, modify and check four, referred to as add, delete and change. Here is the implementation code:
/* * Specific operation hibernate class * Add, delete, modify, query by ID, fuzzy query, query all **/ public class PersonOperate { //All operations in hibernate are completed by Session public Session = null; //instantiate Session object in constructor public PersonOperate(){ //find hibernate configuration Configuration config = new Configuration().configure(); //remove SessionFactory from configuration SessionFactory sf = config.buildSessionFactory(); //Remove Session from SessionFactory this.session = sf.openSession(); } //all operations of Hibernate class are done through Session //add data in database public void insert(Person person){ //Start transaction Transaction tran = session.beginTransaction(); //execute statements session.save(person); //Submit a transaction tran.commit(); session.close(); } //operation Hibernate class changes data in database public void update(Person person){ //Start transaction Transaction tran = session.beginTransaction(); //execute statements session.update(person); //Submit a transaction tran.commit(); } /* Operation Hibernate class: query by ID * We insert and modify objects * Then we should also return an object when querying **/ public Person queryById(String id){ System.out.println(id); Person person = null; //Hibernate query statement String hql = "FROM Person as p WHERE p.id = ? "; Query q = session.createQuery(hql); q.setString(0, id); List list = q.list(); Iterator iteator = list.iterator(); if(iteator.hasNext()){ person = (Person)iteator.next(); } return person; } /* Operation Hibernate class: delete data in database *Hiberante2,Hibernate3 common deletion method * Disadvantages: Query the data once before deleting the data to find out the deleted data object :: Low performance **/ public void delete(Person person){ //Start transaction Transaction tran = session.beginTransaction(); //execute statements session.delete(person); //Submit a transaction tran.commit(); } public void delete(String id){ //Start transaction Transaction tran = session.beginTransaction(); String hql = "DELETE Person WHERE id = ? "; Query q = session.createQuery(hql); q.setString(0, id); //execute update statement q.executeUpdate(); //Submit a transaction tran.commit(); } //Operation Hibernate class: query all data public List queryAll(){ List list = null; String hql = "FROM Person as p "; Query q = session.createQuery(hql); list = q.list(); return list; } //operation Hibernate class: fuzzy query public List queryByLike(String colnum,String value){ List list = null; String hql = "FROM Person as p WHERE p. "+ colnum +" like ? "; Query q = session.createQuery(hql); q.setString(0, "%"+ value +"%"); list = q.list(); Thank you for reading, the above is the "Hibernate class increase, delete, modify and query operation" content, after the study of this article, I believe that everyone on Hibernate class increase, delete, modify and query operation This problem has a deeper understanding, the specific use of the situation also needs to be verified by practice. Here is, Xiaobian will push more articles related to knowledge points for everyone, welcome to pay attention!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.