1.hibernate.cfg.xml
| The code is as follows |
Copy Code |
|
<! DOCTYPE hibernate-configuration Public
"-//hibernate/hibernate Configuration DTD 3.0//en"
"Http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd" >
<session-factory>
<!--configuration Database dialect-->
<property name= "Hibernate.dialect" >org.hibernate.dialect.MySQLDialect</property>
Driver--> for <!--configuration database
<property name= "Hibernate.connection.driver_class" >com.mysql.jdbc.Driver</property>
<!--metabase User name-->
<property name= "Hibernate.connection.username" >root</property>
<!--configuration Database password-->
<property name= "Hibernate.connection.password" >root</property>
<!--configuration database URL-->
<property name= "Hibernate.connection.url" >jdbc:mysql://localhost:3306/hibernate</property>
<!--Configure the maximum capacity of the data pool-->
<property name= "Hibernate.c3p0.max_size" >20</property>
<!--Configure the minimum capacity of the data pool-->
<property name= "Hibernate.c3p0.min_size" >1</property>
<!--The timeout limit for configuring data links-->
<property name= "Hibernate.c3p0.timeout" >5000</property>
<!--the console shows whether the background print execution SQL-->
<property name= "Hibernate.show_sql" >true</property>
<!--whether to display the printed SQL--> in a friendly format
<property name= "Hibernate.format_sql" >true</property>
<!--Print some of the supporting annotations-->
<property name= "Hibernate.use_sql_comments" >true</property>
<property name= "Hibernate.c3p0.max_statements" >100</property>
<property name= "Hibernate.c3p0.idle_test_period" >3000</property>
<property name= "Hibernate.c3p0.acquire_increment" >2</property>
<property name= "Hibernate.c3p0.validate" >true</property>
<!--How to configure data manipulation-->
<property name= "Hbm2ddl.auto" >create</property>
<!--Add the mapping file for our pen above-->
<mapping resource= "Org/rudiment/hibernate/pen.hbm.xml"/>
</session-factory>
|
2. Pen.java of persistent type
| The code is as follows |
Copy Code |
|
Package Org. Rudiment.hibernate;
public class Pen {
private int id;
private int price;
Private String type;
public int getId () {
return ID;
}
public void setId (int id) {
This.id = ID;
}
public int GetPrice () {
return price;
}
public void Setprice (int price) {
This.price = Price;
}
Public String GetType () {
return type;
}
public void SetType (String type) {
This.type = type;
}
} |
3. Persistent class mapping file Pen.cfg.xml
| The code is as follows |
Copy Code |
|
<?xml version= "1.0"?>
<! DOCTYPE hibernate-mapping Public "-//hibernate/hibernate mapping DTD 3.0//en"
"Http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >
<!--generated 2013-9-8 23:34:47 by Hibernate Tools 3.4.0.CR1-->
<class name= "org. Rudiment.hibernate.pen "table=" pen ">
<id name= "id" type= "int" >
<column name= "ID"/>
<generator class= "native"/>
</id>
<property name= "Price" type= "int" >
<column name= "Price"/>
</property>
<property name= "type" type= "java.lang.String" >
<column name= "TYPE"/>
</property>
</class>
<!--This database-object should be placed under class, otherwise it will be an error-->
<database-object>
The SQL in the <!--create executes--> after drop
<create>create table Test (t_name varchar (255));</create>
<!--The following items are executed before the-->
<drop>drop Table test</drop>
<!--The following two sentences indicate that the database object is valid for those databases-->
<dialect-scope name= "Org.hibernate.dialect.MySQLDialect"/>
<dialect-scope name= "Org.hibernate.dialect.MySQLInnoDBDialect"/>
</database-object>
|
4. Operation of the persistence class of the processing class Penhandler.java
| The code is as follows |
Copy Code |
|
Package Org. Rudiment.hibernate;
Import org.hibernate.Session;
Import Org.hibernate.SessionFactory;
Import org.hibernate.cfg.Configuration;
Import Org.hibernate.service.ServiceRegistry;
Import Org.hibernate.service.ServiceRegistryBuilder;
public class Penhandler {
public static void Main (string[] args)
{
Configuration conf = new Configuration ();
Conf.configure ();
serviceregistry sr = new Serviceregistrybuilder (). Applysettings (Conf.getproperties ()). Buildserviceregistry ();
Sessionfactory SF = Conf.buildsessionfactory (SR);
}
} |
Note:
As you can see above, main in Penhandler only executes to the Conf.buildsessionfactory (SR); This part of our configuration file is in effect.
| code is as follows |
copy code |
|
< Database-object>
<!--The SQL in Create is executed after drop
<create >create table Test (t_name varchar (255)) </create>
<!--The following content is executed before the Create-->
<drop>drop Table Test</drop>
<!-- The following two sentences indicate that the database object is in effect for those databases;
<dialect-scope name= "Org.hibernate.dialect.MySQLDialect"/
<dialect-scope name= "Org.hibernate.dialect.MySQLInnoDBDialect"/>
</ Database-object> |
When you look at the database, you see that the table has been successfully built.
| The code is as follows |
Copy Code |
|
mysql> desc test;
+--------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------+--------------+------+-----+---------+-------+
| T_name | varchar (255) | YES | | NULL | |
+--------+--------------+------+-----+---------+-------+
1 row in Set (0.02 sec) |
The last thing to note is that the code is to be executed to Conf.buildsessionfactory (SR), and if the code is removed from main, then hibernate will not execute the database object, that is, if you want the database object to take effect, At a minimum, you need to execute to Conf.buildsessionfactory (SR), this code.