Getting started 02 - The first Hibernate program
Here with a simple stand-alone program to demonstrate the configuration and functionality of Hibernate, some of this example, actually use some automation tools to complete, not to manually operate, this is completely manual, Let you know that Hibernate will actually make those actions. Before conducting examples, first determine that the relevant JAR files in the previous topic have been set in the classpath. Let's preparation for the database, add a HibernateTest database in MySQL and establish a USER form:
Create Table User
User_id char (32) Not Null Primary Key,
Name varchar (16) Not null,
Sex char (1),
Age Int
);
Let's write a pure Java object, which is purely representing a data collection, and we will send the image to the table of the database, the program is as follows:
User.java
Package online.
PUBLIC CLASS User {
Private string id;
PRIVATE STRING NAME;
Private char sex;
PRIVATE INT AGE;
Public int getage () {
Return Age;
}
Public string getId () {
Return ID;
}
Public string getname () {
Return Name;
}
Public char getsex () {
Return SEX;
}
Public void setage (INT i) {
AGE = I;
}
Public void setid (String String) {
ID = String;
}
Public void setname (String string) {
Name = string;
}
Public void setsex (char c) {
SEX = C;
}
}
Where ID is a special property, Hibernate uses it as a primary key identification, we can define the mode of the primary key, which is completed in the XML image file, to tell the Hibernate object how to image the database table, we write an XML mapping file file The name is User.hbm.xml, as shown below:
User.hbm.xml
XML Version = "1.0"?>
Public "- // hibernate / hibernate mapping dtd // en"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
id>
Property>
clas>
hibernate-maping>
This XML file defines the relationship between the object properties image to the database table. You can understand the description of the list, like the User object to the USER table, where we use uuid.hex to define the generating algorithm for primary key, UUID algorithm use The IP address, the startup time, system time, and one count value of the JVM are generated. In addition to using uuid.hex, we can use other ways to generate primary keys, such as increment, etc., which can be found in the Hibernate reference manual.
Hibernate.cfg.xml
XML Version = '1.0' encoding = 'big5'?>>
PUBLIC "- // Hibernate / Hibernate Configuration DTD // EN"
"http://hibernate.sourceforge.net/hibernate-configuration-2.0.dtd">
session-factory>
Hibernate-Configuration>
Next we write a test program, this program will operate the object directly with the syntax method familiar to the Java program, and actually complete the operation of the database, the program will deposit a data into the form:
Hibernatetest.java
Import Onlyfun.caterpillar. *;
Import net.sf.hibernate. *;
Import net.sf.hibs. *;
Public class hibernatetest {
Public static void main (string [] args) throws hibernateException {
SESSIONFACTORY sessionFactory = new configuration (). CONFIGURE (). BuildSessionFactory ();
User User = new user ();
User.setname ("Caterpillar");
User.setSex ('m');
User.Setage (28);
Session session = sessionFactory.openSession ();
Transaction tx = session.begintransaction ();
Session.save (user);
TX.comMit ();
session.close ();
SessionFactory.Close ();
System.out.println ("Added information OK! Please use mysql watch results first!");
}
}
Configuration represents the image setting of the Java object to the database. This setting is from the XML above, then we get the sessionFactory object from Configuration, and open a session, which represents a session of the object and table And Transaction represents a set of session operations, we only need to manipulate the User object directly, and perform the relevant operations of the session and Transaction, Hibernate automatically completes the operation of the database. This is just a simple introduction to the program first, then in detail. Compile all .java files and place two XML files in the same directory as HibernateTest, that is, the file location is as follows:
/
| - HibernateTest.class
| --User.hbm.xml
| --Hibernate.cfg.xml
/ Onlyfun
/ Caterpillar
| --User.class
OK! Now you can perform HibernateTest, the following message will appear:
Log4j: warn no appenders could be bound for logger (net.sf.hibernate.cfg.environment).
Log4J: Warn Please Initialize The log4j system prot in.
Hibernate: INSERT INTO USER (Name, SEX, AGE, User_ID) VALUES (?,?,?,?)
Added information OK! Please use MySQL to watch the results first!
Only the data is deposited here first, if you want to watch the results of data, please enter mysql viewing, the following is the result of the data inventory:
MySQL> Select * from user;
----------------------------------- ------ ------
| User_id | Name | SEX | AGE |
----------------------------------- ------ ------
| 297E3DBDFEA6023D00FEA60241000001 | Caterpillar | M | 28 |
----------------------------------- ------ ------
1 ROWS IN SET (0.00 sec)