Java Interview points---hibernate interview system knowledge points Review, hibernate principle, buffer---update at any time

Source: Internet
Author: User
Tags generator mysql in
1.Hibernate Foundation
What 1.1 hibernate used to do.
Hibernate is an open-source, encapsulated data access layer component, which we call remainders the data Access Layer framework (the Ring Persistence layer framework).
Before remainders we access/manipulate data from the database through the Jdbc/sql statement, and hibernate encapsulates these operations and
The door uses the component technology of the data access layer.
Before the Hibernate framework remainders in the enterprise project development process, the following points caused the programmer's pain:
The SQL statement is too complicated, and the database coupling degree is high
Some SQL involves multiple table operations, some of which are very large, and the SQL written in DAO is not
is often complex and causes a high degree of coupling between DAO and database.
 the remainders of SQL between databases, resulting in migration difficulties
Not the same as the database although the SQL statements are roughly the same, but there are some differences in the gentry, such as the way of paging in Oracle
and MySQL in the page is not the same (see the database part of the knowledge), so there will be code porting difficult factors.
 the mismatch between two dimensional relational table and object remainders data structure
The data we take out of the database is the result set (a table), and we need to encapsulate the query's result set as
object, however, the data structure of the two-dimensional table in the database and the structure of the Java object in memory are not matched (the number in the table
It needs to be processed to become a Java object.

As a few points, the pain has been to promote the technological innovation and the step, hibernate the application of life.
------------------------------------------------------------------------------------------------
2.Hibernate Working Principle-orm * * *
Object-Relational mapping (Object-relationship Mapping) in our application (app), the data is represented by objects, while in the database the data is saved in the form of a table.
Hibernate mapping between data relationships (relationship) remainders in objects in an application (object) not in the table
(Mapping), that is, to save an object in a relational table, the withdrawal maps the data in the relational table to the object.
Hibernate a high degree of self-adaptive components, so it is more difficult to control, in the case of hibernate understanding is not sufficiently thorough
Use, and a little inattention may affect performance.
The industry also has a slightly less self-adaptive data access layer components, such as Ibatis, rather than the half of the machine, controllable
Hibernate is stronger, it is also more popular at present.
Hibernate HelloWorld * *
Core steps
1) Import Jar Pack
2 Hibernate configuration file (only 1)
Hibernate.cfg.xml
Some configuration information of database connection information and hibernate
3 Hibernate mapping file (can have N)
Used to indicate the correspondence between class and table remainders, Hibernate generates SQL statements from that file
For example, the Pojo class is named Emp.java, and the corresponding mapping file is named Emp.hbm.xml
---------------------------------------------------------------------------
New configuration file Hibernate.cfg.xml
Note: should be placed in the SRC directory of the source file, the default is Hibernate.cfg.xml
File content is the basic information that must be used when hibernate work
<?xml version= "1.0" encoding= "UTF-8"?>
<! DOCTYPE hibernate-configuration Public
"-//hibernate/hibernate Configuration DTD 3.0//en"
"Http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd" >
<session-factory>
<!--1. Database connection Information-->
<property name= "Connection.url" >
Jdbc:mysql://localhost:3306/test
</property>
<property name= "Connection.username" >root</property>
<property name= "Connection.password" >root</property>
<property name= "Connection.driver_class" >
Com.mysql.jdbc.Driver
</property>

<!--2. Hibernate configuration Information-->
<!--dialect is a dialect, used to configure the SQL statement for which database is generated-->
<property name= "dialect" >
<!--the dialect provided by hibernate, used to encapsulate a particular database of dialects-->
Org.hibernate.dialect.MySQLDialect
</property>
<!--print execution SQL to the console, typically with SQL tuning-->
<property name= "Hibernate.show_sql" >true</property>
</session-factory>
-----------------------------------------------------------
5. New Mapping File
The mapping file indicates the mapping relationship between the Pojo class and the table remainders (XX property corresponds to the xx field)
A class corresponds to a mapping file
New Pojo class User.java
The Pojo class represents an ordinary and ordinary class
Package Com.tarena.tts.po;

public class User {
Private Integer ID;
Private String LoginName;
private String password;
private String name;

Public Integer GetId () {return ID;}
public void SetId (Integer id) {this.id = ID;}
Public String Getloginname () {return loginName;}
public void Setloginname (String loginName) {
This.loginname = LoginName;
}
Public String GetPassword () {return password;}
public void SetPassword (String password) {
This.password = password;}
Public String GetName () {return name;}
public void SetName (String name) {this.name = name;}
------------------------------------------------------------------
C. New mapping File User.hbm.xml
Note: The mapping file does not Pojo classes together by default; the naming rule is the class name. Hbm.xml
<?xml version= "1.0" encoding= "Utf-8"?>
<! DOCTYPE hibernate-mapping Public
"-//hibernate/hibernate Mapping DTD 3.0//en"
"Http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >
<class name= "Com.tarena.tts.po.User" table= "T_user" >
<id name= "id" type= "java.lang.Integer" column= "t_id" >
<!--generator is used to indicate how primary keys are generated-->
<generator class= "Identity" ></generator>
</id>
<property name= "LoginName" type= "java.lang.String"
Column= "T_login_name" ></property>
<property name= "Password" type= "string"
Column= "T_password" ></property>
<property name= "name" type= "Java.lang.String"
Column= "T_name" ></property>
</class>
Primary keys are generally generated by themselves.
We generally do not use business data as the primary key because changes in the business logic may change the primary key.
As follows: There are many different ways to generate primary keys.
-----------------------------------------------------------------------------
6. Among them, sequence is the use of sequences to generate primary keys (Oracle database is often used)
MySQL databases typically use identity to generate primary keys (note that when you need to create a table, you designate a primary key to become
auto_increment).
----------------------------------------------
D. Associating a mapping file in a configuration file
<?xml version= "1.0" encoding= "UTF-8"?>
<! DOCTYPE hibernate-configuration Public
"-//hibernate/hibernate Configuration DTD 3.0//en"
"Http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd" >
<session-factory>
<!--database connection information-->
<property name= "Connection.url" >
Jdbc:mysql://localhost:3306/test
</property>
<property name= "Connection.username" >root</property>
<property name= "Connection.password" >root</property>
<property name= "Connection.driver_class" >
Com.mysql.jdbc.Driver
</property>
<!--hibernate configuration information-->
<!--dialect dialect, used to configure the SQL statement for which database is generated-->
<property name= "dialect" >
<!--dialect, hibernate, used to encapsulate a particular database-->
Org.hibernate.dialect.MySQLDialect
</property>
<property name= "Current_session_context_class" >thread</property>
<property name= "Hibernate.show_sql" >true</property>

<!--associating mapping files in a configuration file-->
<mapping resource= "Com/tarena/tts/po/user.hbm.xml"/>
</session-factory>
A. New testhibernate
Insert a piece of data into the database
Package com.tarena.tts.test;

Import org.hibernate.Session;
Import Org.hibernate.SessionFactory;
Import org.hibernate.Transaction;
Import org.hibernate.cfg.Configuration;
Import Org.junit.Test;
Import Com.tarena.tts.po.User;
public class Testhibernate {
/**
* Deposit a user object into the database
*/
@Test
public void Tst1 () {
1. Create a User Object
User user = new user ();
User.setloginname ("Whatisjava");
User.setname ("Goodman");
User.setpassword ("12345");

2. Invoke the Hibernate API to load the hibernate configuration file
Configuration conf = new Configuration ();

/*3. To load the default profile in the Classpath hibernate.cfg.xml
* If the profile is associated with the mapping file, the mapping information is also loaded.
Conf.configure ();
3.1 If the specified configuration file is loaded
Conf.configure (New File ("Abc.xml"));

4. Create Sessionfactory
Sessionfactory factory = Conf.buildsessionfactory ();

5. Create session
Hibernate provides the Access interface
Session session = Factory.opensession ();

6. Get a transaction
Transaction tx = Session.gettransaction ();
6.1 Open the transaction
Tx.begin ();

6.2 Inserting data
Session.save (user);

6.3 Committing a transaction
Tx.commit ();

7. Close session
Session.close ();
}
}
----------------------------------------------------------------------------------
Related Article

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.