Because Hibernate is ORM, programmers don't need to write SQL statements. All operations are done through the manipulation of the object.
1, native session transaction management
Transaction tx = Session.begintransaction (); // Open Transaction tx.commit (); // Commit a transaction
Querying data
Querying data with a primary key value of 1 in the user table
Object user = Session.get (user. Class, 1);
Inserting data
Inserting a new piece of data into a data table
User user=new User (); User.setid (4); User.setname ("Jordan"); User.setpassword (" abc123 "= Session.begintransaction (); // Open Transaction session.save (user); Tx.commit (); // Submit
Update all data
Assigns all fields with ID 4 in the data table,
If a field in the corresponding user table does not have a given value in the inserted object, it will be assigned a value of NULL
User user=new User (); User.setid (4); User.setname ("James"= Session.begintransaction (); session.update (user); // This will change the data with ID 4 to ' James ' and the password field value to null. Tx.commit ();
Updating part of the data
If you update only some of the fields in a row in the datasheet, you can follow the idea
Ideas:
Get all the data out first,
Then change the data that needs to be modified
Make all updates again.
This allows you to implement a partial update
User user = (user) session.get (user). Class, 4); User.setpassword ("abcdef"= Session.begintransaction (); // Open Transaction session.update (user); Tx.commit (); // Commit a transaction
Delete data
Delete data with primary key 2
User user=new User (); User.setid (2); // use primary key as deletion condition Transaction tx = session.begintransaction (); session.delete (user); Tx.commit ();
2,HQL Querying all data
SQL statement: SELECT * from user
HQL Operation:
String hql= "from User"== Query.list (); // Query all data for (Object user:list) { System.out.println (user);}
Finding data for a specified condition
SQL statement: SELECT * from user where uid=2;
HQL Operation:
String hql= "from User where id=?" = session.createquery (HQL); Query.setinteger (0, 2); // Setting Parameters Object result = Query.uniqueresult (); // get the only results SYSTEM.OUT.PRINTLN (result);
Using pre-defined functions
SQL statement: SELECT COUNT (*) from user
HQL Operation:
String hql= "SELECT COUNT (*) from User"== Query.uniqueresult (); // The result must be plastic . SYSTEM.OUT.PRINTLN (result);
Page out
SQL statement: SELECT * from User limit
HQL Operation:
String hql= "from User"= session.createquery (HQL); Query.setfirstresult (1); // query Start position query.setmaxresults (2); // Specify the number of queries List List = query.list (); for (Object user:list) { System.out.println (user);}
Update
SQL statement: Update user set uname= ' Honny ' where uid=1
HQL Operation:
String hql= "Update User set name=?" where id=? " = session.createquery (HQL); Query.setstring (0, "Honny"); Query.setinteger (1, 1= Session.begintransaction (); // Open Transaction int rows = query.executeupdate (); // number of rows that are affected tx.commit (); // Commit a transaction System.out.println ("Number of affected rows:" +rows);
General operations collection for the "Hibernate" Data Session Object