Some problems that should be paid attention to in Hibernate learning
One. Introduction
Note that the introduction of the hibernate package (the introduction of the package is a problem that must be paid attention to each development framework), when it is not possible to use which package (especially for novice), it is recommended to put the package and hibernate2.jar in hibernate / lib. Introduction. There is also a package for connecting to the database (this follow-up. I use MySQL MySQL-Connector-Java-3.0.14-Production-bin.jar).
two. Book writing of Hibernate.cfg.xml
XML Version = '1.0' encoding = 'UTF-8'?>
"- // Hibernate / Hibernate Configuration DTD 2.0 // en"
"http://hibernate.sourceforge.net/hibernate-configuration-2.0.dtd">
JDBC: mysql: //192.168.151.72: 3306 / QuickStart
Property>
Net.sf.hibernate.Dialect.MysqldiaLect
Property>
com.mysql.jdbc.driver
Property>
session-factory>
Hibernate-Configuration>
The first half of the configuration file is used to configure the database connection pool (suggestion novice is connected to the connection pool configuration with Hibernate - the master can be using another connection pool - such as Tomcat, Struts, or JBoss)
The last sentence "
"- // Hibernate / Hibernate mapping DTD 2.0 // en"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
id>
clas>
hibernate-maping>
In this profile, the Class tag is a mapping for establishing a table to class. If the above example, the Customer class under the COM.LN.hb package is a mapping relationship with the Customer table in the database. Class label of sublays:
1. The ID is used to indicate the main key,
Options Description:
1) Assigned
The primary key is generated by the external program without having hibernate participation.
2)
Hilo
The primary key generation mechanism implemented by the Hi / LO algorithm requires additional database tables to save the main
The keys generate a historical state.
3) SEQHILO
Similar to HILO, primary key generation mechanism implemented by Hi / LO algorithm, just master key history
Status Save in Sequence, which is suitable for databases that support Sequence, such as Oracle.
4) Increment
The primary key is incremented in the order of value. The implementation mechanism of this method is maintained in the current application instance
A variable to save the current maximum, then time you need to generate the primary key.
Plus this value 1 as the primary key.
The problem that may be generated in this way is: if there are multiple instances to access the same data
The library, then different instances may generate the same due to their respective cases of the primary key due to each instance.
The primary key, thereby causing the primary key to repeat an exception. Therefore, if there are multiple realities in the same database
Example Access, this way must be avoided.
5) Identity
The primary key generation mechanism provided by the database. Such as DB2, SQL Server, MySQL
Primary key generation mechanism.
6) SEQUENCE
The SEQUENCE mechanism supplied with the database generates a primary key. Oralce
SEQUENCE.
7) native
From Hibernate, it is determined by the underlying database, it uses Identity, Hilo, SEQUENCE
One of them is a primary key generation.
8) UUID.HEX
Generates 16-based value based on 128-bit unique value generated by Hibernate (encoded
A string representation of length 32 is represented as a primary key.
9) uuid.string
Similar to uuid.hex, only the generated primary key is not encoded (length 16). In certain
There may be problems in the database (such as PostgreSQL).
10) Foreign
Use the fields of the external table as the primary key.
In general, use UUID.HEX mode to generate primary keys will provide the best performance and database platform.
State.
In addition, due to common databases such as Oracle, DB2, SQLServer, MySQL, etc.
The easy-to-use primary key generation mechanism (Auto-Increase field or sequence). We can
According to the primary key generation mechanism provided by the library, the primary key generation method of generator-class = native is used.
However, it is worth noting that the primary key generation mechanism provided by some databases is not optimal.
The interlock between the tables may cause a lot of incorporated INSERT data.
The primary key generating mechanism provided by the database is often by saving the current primary key in an internal table.
(For example, for self-amplified primary keys, the current maximum and increment is maintained in this internal table).
The maximum value will be read each time inserting the data, and then add increment as the primary key of the new record.
After updating this new maximum, this new maximum is updated back in the internal table, so that one INSERT operation may cause data.
The library has multiple table reads and writes, and the data is accompanied by lock unlocking operations, which produces performance.
Great influence.
Therefore, for concurrent INSERT requirements, it is recommended to use uuid.hex as a primary key generation.
mechanism.
2.
three. Persistence class
Like a general forma class, define properties, and its getter and setter methods. (Note that attribute names are consistent with the name of the properties of the profile), as follows:
Package com.ln.hb;
Import java.io.serializable;
Public Class Customer Implements Serializable {
Private int ID;
PRIVATE STRING UserName;
PRIVATE STRING Password;
Public int getId () {
Return ID;
}
Public string getpassword () {
Return Password;
}
Public string getUsername () {
Return UserName;
}
Public void setid (int id) {
THIS.ID = ID;
}
Public void setpassword (string password) {
this.password = password;
}
Public void setusername (String username) {
THIS.USERNAME = UserName;
}
}
4. The writing of the test class (Hibernate call)
Package com.ln.test;
Import java.util.iterator;
Import com.ln.hb.customer;
Import net.sf.hibernate.hibernateException;
Import net.sf.hibernate.query;
Import net.sf.hibernate.Session;
Import net.sf.hibrnate.SessionFactory; import net.sf.hibernate.transaction;
Import Net.sf.hibiBernate.cfg.configuration;
Public class test {
Public static void main (String [] args) {
Try {
SessionFactory sf = new configuration (). Configure ()
.BUILDSessionFactory ();
/ / The above is to read the default hibernate.cfg.xml
Session session = sf.opensession ();
Transaction tx = session.begintransaction ();
Customer Customer = New Customer ();
System.out.println ("Let's Go!");
// Customer.SetUserName ("liang");
// Customer.SetPassword ("Hello");
// session.save (Customer);
// session.flush ();
Query Query = session.createQuery ("Select Customer from Customer As Customer);
Iteerator it = query.Itemate (); it.hasnext ();) {
Customer Cust = (Customer) IT.Next ();
System.out.print ("********" "UserName:" Cust.getuserName () "*********");
System.out.println ("Password:" Cust.getPassword () "********");
}
TX.comMit ();
session.close ();
} catch (hibernateException e) {
E.PrintStackTrace ();
}
}
}
Fives. Tasks that you should pay attention to in the design of the database table.
1. Due to the Hibernate primary phase to avoid manual setting, modifying the primary key value. So it is recommended to set the primary key to auto_increment (that is, the form of automatically adds a form) and cannot be empty.
2. An example of using the Tomcat connection pool is attached below:
The above configuration is only modified with hibernate.cfg.xml. The modified hibernate.cfg.xml is as follows:
XML Version = '1.0' encoding = 'UTF-8'?>
"- // Hibernate / Hibernate Configuration DTD 2.0 // en"
"http://hibernate.sourceforge.net/hibernate-configuration-2.0.dtd">
root property>
JDBC: MySQL: // localhost: 3306 / QuickStart
Property> ->
Here is where to connect Tomcat (or other) connecting pools, Property's content is the JNDI name of the connection pool you configure.
Net.sf.hibernate.Dialect.MysqldiaLect
Property>
print> ->
com.mysql.jdbc.driver
Property>
session-factory>
Hibernate-Configuration>
The Tomcat's connection pool configuration is as follows (configured inside youProject.xml pointing to your project):
XML Version = '1.0' encoding = 'UTF-8'?>
PATH = "/ hb2" docBase = "Eclipse / Workspace / Hbstruts" Reloadable = "True"> parameter> parameter> parameter> parameter> parameter> parameter> Resourceparams> Context>