Getting Started with hibernate

Source: Internet
Author: User

Hibernate is a data persistence tool and an ORM solution for open source code. Hibernate internally encapsulates the operation of accessing the database through JDBC, providing an object-oriented data access API to upper-level applications.
Use hibernate to complete the persistence operation. Code that needs to be written such as:
Session session=hibernateutil.currentsession ();
Query query=session.createquery ("from User");
List<user> users= (list<user>) query.list ();
Hibernate is a custom tool class that gets the session object of Hibernate, which is the core API for hibernate to perform persistent operations. Hibernate processing database query, the code is very concise, as a result of the query, you can directly obtain a user's instance of the list collection instance, to avoid the tedious duplication of data conversion process.
Benefits of Hibernate framework:
(1) Hibernate is powerful, the bridge between Java application and relational database, compared with JDBC mode operation database, the code amount is greatly reduced, the development speed of persistent code is improved, and maintenance cost is reduced.
(2) Hibernate supports many object-oriented features. such as combination, inheritance, polymorphism and so on. So that developers do not have to face the business domain object model and database-oriented relational data model to switch back and forth to facilitate the development of domain-driven object-oriented design and development.
(3) Good portability, the system will not be bound to a particular relational database, for the system to replace the database, only need to modify the Hibernate configuration file to run normally.
(4) Hibernate framework open source free. You can study the source code when you need it, rewrite the source code for functionality customization, and be extensible.
The disadvantage of Hibernate:
(1) Not suitable for data-centric applications that use a large number of stored procedures.
(2) Large-scale bulk inserts, modifications and deletions are not suitable for hibernate.
Hibernate environment collocation:
1. Download the jar file
2. Deploy the Jar file
3. Create a configuration file Hibernate.cfg.xml
4. Creating persistent classes and mapping files
Using hibernate to complete a persistence operation
Seven steps to use hibernate to manipulate a database:
(1) Read and parse the configuration file and mapping file
Configuration Cf=new configuration (). Configure ();
(2) Create a Sessionfactory object based on the information of the configuration file and the mapping file
Sessionfactory factory=cf.buildsessionfactory ();
(3) Open session
Session session=factory.opensession ();
Sessionfactory is responsible for creating session objects.
The session is the foundation of Hibernate persistence, and the session is the core of the persistence manager that runs through hibernate, providing a number of persistence methods, such as Save (), delete (), update (), get (), load (), etc. By using these methods, the object can be modified and deleted transparently.
(4) Start a transaction
Transaction tx=session.begintransaction ();
(5) Database operations
Session.save (dog); Save operation
(6) End of transaction
Tx.commit (); Commit a transaction
or Tx.rollback (); Rolling back a transaction
(7) If the session object is obtained through Sessionfactory's Opensession () method, the session must be closed
Session.close ();
The first case of-------------------------------------------hibernate----------------------------------------------------
First, create a class and write the attributes to encapsulate
public class Dog {
Private String dname;
private int dage;
private int did;
Create a small configuration mapping file
<?xml version= "1.0"?>
<! DOCTYPE hibernate-mapping Public
"-//hibernate/hibernate Mapping DTD 3.0//en"
"Http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" [
<! ENTITY types SYSTEM "Classpath://your/domain/types.xml" >]>

<class name= "Dog" table= "dog" >
<id name= "did" column= "did" >
<generator class= "native"/>
</id>
<property name= "dname" column= "Dname" ></property>
<property name= "Dage" column= "Dage" ></property>
</class>


Create a large configuration
<?xml version= ' 1.0 ' encoding= ' utf-8 '?>
<! DOCTYPE hibernate-configuration Public
"-//hibernate/hibernate Configuration DTD 3.0//en"
"Http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd" >

<session-factory>
<property name= "Connection.driver_class" >oracle.jdbc.driver.OracleDriver</property>
<property name= "Connection.url" >jdbc:oracle:thin: @localhost:1521:orcl</property>
<property name= "Connection.username" >happyy2165</property>
<property name= "Connection.password" >happyy2165</property>

<!--SQL dialect SQL dialect---
<property name= "dialect" >org.hibernate.dialect.Oracle10gDialect</property>

<!--&lt;! &ndash; JDBC Connection pool (use the built-in) &ndash;&gt;
<property name= "Connection.pool_size" >1</property>
-
<!--Print SQL console--
<property name= "Show_sql" >true</property>

<!--automatically build table structure create delete table structure again, update directly updates table data--
<property name= "Hbm2ddl.auto" >update</property>

<mapping resource= "Cn/happy/entity/dog.hbm.xml"/>
</session-factory>


Test class
public class Testhibernate01 {
@Test
public void test01 () {
1. Build the Configuration object, read the large configurations
Configuration Cf=new configuration (). Configure ();
2. Building the plant
Sessionfactory factory=cf.buildsessionfactory ();
3.session Object Opensession ()
Session session=factory.opensession ();

Dog dog=session.load (dog.class,1);

Transaction tx=session.begintransaction ();
Dog Dog=new Dog ();
Dog.setdname ("La La");
Dog.setdage (20);
Session.save (dog);
Tx.commit ();
System.out.println ("ok!");
Session.close ();
}
}
Pom.xml
<!--https://mvnrepository.com/artifact/org.hibernate/hibernate-core-->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>5.2.10.Final</version>
</dependency>

<!--https://mvnrepository.com/artifact/javax.transaction/jta-->
<dependency>
<groupId>javax.transaction</groupId>
<artifactId>jta</artifactId>
<version>1.1</version>
</dependency>

Getting Started with hibernate

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.