Hibernate.properties properties file in Hibernate framework, hibernate.cfg.xml configuration file, and Users.hbm.xml mapping file configuration

Source: Internet
Author: User
Tags generator uuid

In the actual development is generally hibernate.properties and hibernate.cfg.xml combined use. Generally in the property file Hibernate.properties to store database connection related operational data, in the Hibernate.cfg.xml file to store mapping configuration, in Users.hbm.xml (. Hbm.xml before the users, custom, you can have an entity class corresponding to one. h Bm.xml file, you can also have multiple entity classes that correspond to a. hbm.xml file.

The attribute and mapping configuration were previously stored in Hibernate.cfg.xml, as follows:

<! DOCTYPE hibernate-configuration Public "-//hibernate/hibernate configuration DTD 3.0//en" "http://www.hibernate.org/ Dtd/hibernate-configuration-3.0.dtd "> It is possible to define attributes and mapping configurations in this way, but now development is to separate the attribute definition from the mapping configuration, which will make the results clearer. Need to modify the property to Hibernate.properties file modification, need to modify the mapping configuration in the Hibernate.cfg.xml to modify.

Like what:

1. Configure the properties required to connect to the database in Hibernate.properties:

hibernate.dialect=org.hibernate.dialect.mysqldialect//defines dialects (SQL statements that define optimizations)
hibernate.connection.driver_class= Com.mysql.jdbc.Driver
Hibernate.connection.url=jdbc:mysql://localhost:3306/firstdb?characterencoding=utf8
hibernate.connection.username=root
hibernate.connection.password=root
hibernate.show_sql=true// Statement that indicates whether to output the operation database
hibernate.format_sql=true//is a formatted output SQL statement
hibernate.hbm2ddl.auto=update// Indicates whether database tables are automatically created based on the mapping file
Note: The value of Hibernate.hbm2ddl.auto is create means that each run will create a new table, the value of Create-drop is to delete the legend table when the sessionfactory is closed; the value of update (common value) means that if the database does not have a corresponding table at the first run-time, a new table is created , but the corresponding data table is no longer created if the corresponding datasheet exists.


2. Mapping configuration in Hibernate.cfg.xml (registering the mapping file):

<! DOCTYPE hibernate-configuration public
	"-//hibernate/hibernate configuration DTD 3.0//en"
	"http:// Www.hibernate.org/dtd/hibernate-configuration-3.0.dtd ">


3. Configure the mappings for the persistence (entity, Pojo) class and database table in Users.hbm.xml:

<?xml version= "1.0"?>
<! DOCTYPE hibernate-mapping public 
	"-//hibernate/hibernate mapping DTD 3.0//en"
	"Http://www.hibernate.org/dtd /hibernate-mapping-3.0.dtd ">

Above is the specific data in the Users.hbm.xml, the configuration content can follow the following format:

Primary key: In Hibernate the persisted class will have an identity attribute that identifies the unique instance that maps to the primary key of the database table.

Primary key builder: <generator class= "build Mode"/>

The values for the build method are:

Increment: Gets the largest primary key value of the table in the database and is used as the new primary key after +1.

Identity: Automatic growth, mainly used in MYSQL,DB2.

Native: (Generally selected in the development of this method) automatic growth, Hibernate will be based on the database from the identity,sequence (sequence), Hilo (High and low algorithm) to choose a suitable way to generate the primary key, in MySQL, use identity, Select sequence (sequence) when in Oracle.

UUID: Use the UUID algorithm to generate a string type primary key (primarily for distributed deployment).

Data table fields configuration: <property name= "Property" column= "column name" type= "Data Type" not-null= "true"/> the attributes of the entity class correspond to the column names (field names) in the database table. It is best to write the package name (full name) of this data type for a given data type.





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.