When Hibernate is a beginner, schemaexport is used to generate data tables and configure log.

Source: Internet
Author: User

Generally, during project development, tables are created first and then generated using hibernate reverse engineering *. HBM. XML files and pojo classes, I personally think that because the databases currently used are relational databases, and hibernate, as an Orm, objectify database operations, we should proceed from an object, generate related tables in the database, which is more in line with people's habits of cognition.
Because hibernate3 provides the built-in tool hbm2ddl, it is very easy to create a database based on your object.
.
1. Create a Java Project

2. Create a user library and add the following JAR. hibernate_home is a self-built user library that uses the hibernate shelf package as a shared library.
* Hibernate_home/hibernate3.jar,
* Hibernate_home/lib/*. Jar
* MySQL JDBC driver

3. Create the hibernate configuration file hibernate. cfg. xml. To facilitate debugging, it is best to add the log4j configuration file.

Hibernate. cfg. xml:

<! Doctype hibernate-configuration public
"-// Hibernate/hibernate configuration DTD 3.0 // en"
Http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd>

<Hibernate-configuration>
<Session-factory>
<Property name = "hibernate. Connection. url"> JDBC: mysql: // localhost/test </property>
<Property name = "hibernate. Connection. driver_class"> com. MySQL. JDBC. Driver </property>
<Property name = "hibernate. Connection. username"> root </property>
<Property name = "hibernate. Connection. Password"> 123456 </property>
<Property name = "hibernate. dialect"> org. hibernate. dialect. mysqldialect </property>
<Property name = "hibernate. show_ SQL"> true </property>

<Mapping Resource = "com/bjsxt/hibernate/user. HBM. xml"/>
</Session-factory>
</Hibernate-configuration>

Choose the log4j. properties configuration file option.

### Direct log messages to stdout ###
Log4j. appender. stdout = org. Apache. log4j. leleappender
Log4j. appender. stdout. Target = system. Out
Log4j. appender. stdout. layout = org. Apache. log4j. patternlayout
Log4j. appender. stdout. layout. conversionpattern = % d {absolute} % 5 P % c {1}: % L-% m % N

### Direct messages to file hibernate. Log ###
# Log4j. appender. File = org. Apache. log4j. fileappender
# Log4j. appender. file. File = hibernate. Log
# Log4j. appender. file. layout = org. Apache. log4j. patternlayout
# Log4j. appender. file. layout. conversionpattern = % d {absolute} % 5 P % c {1}: % L-% m % N

### Set log levels-for more verbose logging change 'info' to 'debug '###

Log4j. rootlogger = warn, stdout

# Log4j.logger.org. hibernate = info
# Log4j.logger.org. hibernate = debug

### Log hql query parser Activity
# Log4j.logger.org. hibernate. hql. Ast. Ast = debug

### Log just the SQL
# Log4j.logger.org. hibernate. SQL = debug

### Log JDBC bind parameters ###
# Log4j.logger.org. hibernate. type = info
# Log4j.logger.org. hibernate. type = debug

### Log schema export/update ###
# Log4j.logger.org. hibernate. tool. hbm2ddl = debug

### Log hql parse trees
# Log4j.logger.org. hibernate. hql = debug

### Log cache activity ###
# Log4j.logger.org. hibernate. cache = debug

### Log transaction activity
# Log4j.logger.org. hibernate. Transaction = debug

### Log JDBC Resource Acquisition
# Log4j.logger.org. hibernate. JDBC = debug

### Enable the following line if you want to track down connection ###
### Leakages when using drivermanagerconnectionprovider ###
# Log4j.logger.org. hibernate. Connection. drivermanagerconnectionprovider = trace

 

4. Define object class user. Java

Package com. bjsxt. hibernate;

Import java. util. date;

Public class user {
 
Private string ID;
 
Private string name;
 
Private string password;
 
Private date createtime;
 
Private date expiretime;

Public String GETID (){
Return ID;
}

Public void setid (string ID ){
This. ID = ID;
}

Public String getname (){
Return name;
}

Public void setname (string name ){
This. Name = Name;
}

Public String GetPassword (){
Return password;
}

Public void setpassword (string password ){
This. Password = password;
}

Public date getcreatetime (){
Return createtime;
}

Public void setcreatetime (date createtime ){
This. createtime = createtime;
}

Public date getexpiretime (){
Return expiretime;
}

Public void setexpiretime (date expiretime ){
This. expiretime = expiretime;
}
}

 

5. Define the user class ing file user. HBM. xml

<? XML version = "1.0"?>
<! Doctype hibernate-mapping public
"-// Hibernate/hibernate mapping DTD 3.0 // en"
Http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd>
<Hibernate-mapping>
<Class name = "com. bjsxt. hibernate. User">
<ID name = "ID">
<Generator class = "UUID"/>
</ID>
<Property name = "name"/>
<Property name = "password"/>
<Property name = "createtime"/>
<Property name = "expiretime"/>
</Class>
</Hibernate-mapping>

 

6. Add the user. hbml. xml file to the hibernate. cfg. xml file. For detailed configuration, see hibernate. cfg. xml.

 

7. Compile the hbm2ddl tool class to generate database tables for the entity class.

Package com. bjsxt. hibernate;

Import org. hibernate. cfg. configuration;
Import org. hibernate. tool. hbm2ddl. schemaexport;

Public class exportdb {

Public static void main (string [] ARGs ){

// Read the hibernate. cfg. xml file
Configuration CFG = new configuration (). Configure ();

Schemaexport export = new schemaexport (CFG );

Export. Create (True, true );
}
}

8. Develop the client

 

Package com. bjsxt. hibernate;

Import java. util. date;

Import org. hibernate. Session;
Import org. hibernate. sessionfactory;
Import org. hibernate. cfg. configuration;

Public class client {

Public static void main (string [] ARGs ){

// Read the hibernate. cfg. xml file
Configuration CFG = new configuration (). Configure ();

// Create sessionfactory
Sessionfactory factory = cfg. buildsessionfactory ();

Session session = NULL;
Try {
Session = factory. opensession ();

// Start the transaction
Session. begintransaction ();

User user = new user ();
User. setname ("Zhang San ");
User. setpassword ("123 ");
User. setcreatetime (new date ());
User. setexpiretime (new date ());

// Save data
Session. Save (User );

// Submit the transaction
Session. gettransaction (). Commit ();
} Catch (exception e ){
E. printstacktrace ();
// Roll back the transaction
Session. gettransaction (). rollback ();
} Finally {
If (session! = NULL ){
If (session. isopen ()){
// Close the session
Session. Close ();
}
}
}

}
}

 

To facilitate SQL tracking, add <property name = "hibernate. show_ SQL"> true </property> to the hibernate. cfg. xml file.

 

 

 

 

 

 

 

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.