Hibernate API and three ways to query

Source: Internet
Author: User

Hibernate Api

|--Configuration Management Class object

Config.configure (); The method of loading the master configuration file (Hibernate.cfg.xml) is loaded by default Src/hibernate.cfg.xml

Config.configure ("Cn/config/hibernate.cfg.xml"); Loads the master profile for the specified name under the specified path

Config.buildsessionfactory (); Create a factory object for the session

|--sessionfactory Session Factory (or representative of this hibernate.cfg.xml configuration file)

Sf.opensession (); Create a Sesison object

Sf.getcurrentsession (); Create a session or remove a Session object

The |--Session Session object maintains a connection (Connection) that represents the session to which the database is connected. Hibernate is the most important object: use Hibernate with database operations only, this object is used

Session.begintransaction (); Open a transaction; Hibernate requires all operations with the database must have a transactional environment, otherwise error!

Write a code test, as follows:

Write a test class that loads the configuration Management object class and the factory class of the session first

private static Sessionfactory sf;static{/* *//1, Create Configuration Management Object "Configuration: Configure Management class object" config config=new Config ();//load configuration file (default load Src/hibernate.cfg.xml), Config.configure (),///2, according to the loaded configuration management class object, Create a Sessionfactory object (representing the entire hibernate.cdg.xml profile) Sessionfactory sf=config.buildsessionfactory (); */sf=new Configuration (). Configure (). Buildsessionfactory ();}

(1) Save object (insert data)

@Testpublic void Testinsert () {//object user User=new User (); User.setname ("test"); User.setpassword ("666");//According to the session factory, Create Session object (maintains a connection connection, which represents a connection to the database sessions) session session=sf.opensession ();//Open things transaction transaction= Session.begintransaction ();/************* perform operation *********/session.save (user);//Submit things/close transaction.commit (); Session.close (); Sf.close ();}

(2), update objects

/* * Update object */@Testpublic void Testupdate () {//object user User=new User (); User.setid (4); User.setname ("Test5"); User.setpassword ("666");//Depending on the session factory, create session object (maintains a connection connection, represents the session of the database connection) session session=sf.opensession ( );//Open things Transaction transaction=session.begintransaction ();/************* perform operation *********///session.update (user); /primary Key query//user user2= (user) Session.get (user.class, 1),//user user2= (user) Session.load (user.class, 1);//Ibid.//No primary key set, Perform the save operation (insert data), set the primary key, perform the update operation (if the value of the primary key setting does not exist also will error) session.saveorupdate (user);//system.out.println (user2);//Submit the Thing/ Close Transaction.commit (); Session.close (); Sf.close ();}

Three ways to search

(1), HQL query:

HQL query differs from SQL query:

SQL: (structured query statement) queries are tables and fields; Case insensitive.

HQL: Hibernate Query Language-the object-oriented querying language provided by hibernate

The object and the properties of the object are queried. is case sensitive.

As follows:

HQL query Hibernate query language@testpublic void testhql () {Session session=sf.opensession ();//Open things Transaction Transaction=session.begintransaction ();//hql query, query All (Note: Query is object and object properties, not table, case sensitive) query Query=session.createquery ( "From User where id=1 or id=2"); List<user> users=query.list (); System.out.println (users); Transaction.commit (); Session.close (); Sf.close ();}

(2), criteria query:

Fully object-oriented queries.

As follows:

QBC querying query by Criteria@testpublic void Testqbc () {Session session=sf.opensession ();//Open things transaction transaction= Session.begintransaction (); Criteria Criteria=session.createcriteria (user.class);//Query Condition Criteria.add (RESTRICTIONS.EQ ("id", 1)); List<user> list=criteria.list (); SYSTEM.OUT.PRINTLN (list); Transaction.commit (); Session.close (); Sf.close ();}

(3), SQL query

Local SQL query:

Complex query, it is necessary to use the original SQL query, also can, is the support of local SQL query!

(Disadvantage: Can't cross database platform!) )

SQL query @testpublic void Testsql () {Session session=sf.opensession ();//Open things transaction transaction= Session.begintransaction ();//Specify each line of records as the favorite type of sqlquery sqlquery=session.createsqlquery ("SELECT * from users"). Addentity (User.class); List list=sqlquery.list (); SYSTEM.OUT.PRINTLN (list); Transaction.commit (); Session.close (); Sf.close ();}

JS: Gets the specified class compatibility issue,

<! DOCTYPE html>

Hibernate APIs and three ways to query

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.