Add, delete, modify, and query implementation code in hibernate

Source: Internet
Author: User

First, let's first look at the increment. in SQL, the increment is insert, that is, insert. In hibernate, we only need to manipulate an object for Sava and then commit the transaction, the insert function can be implemented. Let's take a look at it. Code The persistence class I will not write any more, which is exactly the same as the fields in the database. To have the set and get methods, I will directly write how to call the Save method.
// Import the required package
Import org. hibernate. hibernateexception;
Import org. hibernate. Session;
Import org. hibernate. sessionfactory;
Import org. hibernate. transaction;
Import org. hibernate. cfg. configuration;
Public class usertest {
Public static void main (string ARGs []) {
Configuration CFG = new configuration (). Configure (); // obtain the configuration information of Hibernate
Sessionfactory Sf = cfg. buildsessionfactory (); // create sessionfactory according to config
Session ses = SF. opensession (); // factory is used to create a session. Enabling a session is equivalent to enabling JDBC connection.
Transaction Ts = SES. begintransaction (); // create the transaction object TS
User user = new user (); // Persistent Object
User. setname ("Kobe ");
User. settel ("111111111 ");
Try {
SES. Save (User );
TS. Commit ();
} Catch (hibernateexception he ){
He. printstacktrace ();
TS. rollback ();
} Finally {
SES. Close ();
SF. Close ();
System. Out. println ("inserted successfully ");
}
}
}
Second, let's look at the deletion. The deletion in SQL is delete, that is, delete. In hibernate, we only need to call an object and call the delete method to delete the object.
Import org. hibernate. hibernateexception;
Import org. hibernate. Session;
Import org. hibernate. sessionfactory;
Import org. hibernate. transaction;
Import org. hibernate. cfg. configuration;
Public class deletetest {
Public static void main (string ARGs []) {
Configuration CFG = new configuration (). Configure ();
Sessionfactory Sf = cfg. buildsessionfactory ();
Session ses = SF. opensession ();
Transaction Ts = SES. begintransaction ();
User user = new user ();
User. setid ("8a8308891e9c3ef3011e9c3ef4aa0001 ");
Try {
SES. Delete (User );
TS. Commit ();
} Catch (hibernateexception he ){
He. printstacktrace ();
TS. rollback ();
} Finally {
SES. Close ();
SF. Close ();
System. Out. println ("deleted successfully ");
}
}
}
For details about the meaning in the middle, refer to the Sava method. Here we should note that when we call the delete operation, the deletion condition, that is, the condition after where must be the configuration ID in our XML, here, we use this method to perform search and deletion, which is particularly worth noting, that is, the user called here. setid (""); in this sentence, it is deleted through the content in.
Third, let's take a look at the changes and update them in SQL. In hibernate, we only need to operate on one object to change the information.
Import org. hibernate. hibernateexception;
Import org. hibernate. Session;
Import org. hibernate. sessionfactory;
Import org. hibernate. transaction;
Import org. hibernate. cfg. configuration;
Public class updatetest {
Public static void main (string ARGs []) {
Configuration CFG = new configuration (). Configure ();
Sessionfactory Sf = cfg. buildsessionfactory ();
Session ses = SF. opensession ();
Transaction Ts = SES. begintransaction ();
User user = new user ();
User. setid ("8a8308891e9c3ef3011e9c3ef4aa0001 ");
User. setname ("kobe24 ");
Try {
SES. Update (User );
TS. Commit ();
} Catch (hibernateexception he ){
He. printstacktrace ();
TS. rollback ();
} Finally {
SES. Close ();
SF. Close ();
System. Out. println ("changed successfully ");
}
}
}
But here we need to pay attention to it. If some friends use this update, they will find that when calling this method, it will not only update the data you want to update, the data you don't want to update will change as well. If you don't set a value for it, it will show null or there is nothing in the table, here we need to use another method to update the data you want to update, and the data you do not want to change will remain in the original state. here we need to call a method.
Session ses = SF. opensession ();
Transaction Ts = SES. begintransaction ();
User user = (User) SES. Get (user. class, "8a8308891e9c3ef3011e9c3ef4aa0001 ");
User. setname ("kobe24 ");
Try {
SES. Update (User );
TS. Commit ();
In this way, we will find that we only update the data we want to update. SES not only has this get method, but also has a load method for the same function. The two methods have the same functions, but what is the difference, the difference is that when the load method is used, it searches from the cache, while when we call the get method, it searches from the database, but the get method first searches from the cache, if it is not found in the database.
Third, let's look at the query. The query in SQL is select. In hibernate, there are multiple methods to query. Here I will write a method that is more advocated by hibernate, that is, hql. When using this method, we need to note that the from in it is not the table name, but the class name.
Package hibernate;
Import java. util. iterator;
Import java. util. List;
Import org. hibernate. query;
Import org. hibernate. Session;
Import org. hibernate. sessionfactory;
Import org. hibernate. transaction;
Import org. hibernate. cfg. configuration;
Public class seletetest {
Public static void main (string ARGs []) {
Configuration CFG = new configuration (). Configure ();
Sessionfactory Sf = cfg. buildsessionfactory ();
Session ses = SF. opensession ();
Transaction Tx = SES. begintransaction ();
User user = new user ();
Query query = SES. createquery ("from user ");
List users = query. List (); // serialization
Iterator it = users. iterator (); // Iteration
While (it. hasnext ()){
User = (User) it. Next ();
System. Out. println (user. getname () + "" + User. gettel () + "");
}
SES. Close ();
SF. Close ();
}
}

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.