Getting started with Hibernate-configuration file,

Source: Internet
Author: User

Getting started with Hibernate-configuration file,
1. Introduction to Hibernate

Hibernate is an open-source, lightweight, and persistent ORM framework.

Hibernate is an open-source object relationship ing framework that encapsulates JDBC very lightweight objects. It establishes ing between POJO and database tables and is a fully automated orm framework, hibernate can automatically generate and execute SQL statements, so that Java programmers can use the object programming thinking to manipulate the database as they wish. Hibernate can be used in any situation where JDBC is used. It can be used in Java client programs or Servlet/JSP Web applications. The most revolutionary is that, hibernate can replace CMP in the J2EE architecture of application EJB to fulfill the task of data persistence.

POJO (Plain Ordinary Java Object) is a simple Java Object, which is actually a common JavaBeans. It is short for avoiding confusion with EJB. The POJO name is used to avoid confusion with ejbs, and its abbreviation is direct. some attributes and Their getter setter methods have no business logic. They can be used as VO (value-object) or dto (Data Transform Object. of course, if you have a simple operation attribute, you can do the same, but you cannot have business methods or methods such as connection. Ii. entity class and configuration file 2.1 entity class 2.1.1 customer entity

Public ClassCustomer {

PrivateLong cust_id;

PrivateString cust_name;

PrivateString cust_source;

// Set of contacts: one customer corresponds to multiple contacts linkman

PrivateSet <LinkMan> linkMans =NewHashSet <LinkMan> ();

// Provides the get and set methods for the above attributes, which are omitted to save Layout

}

2.1.2 Contact entity class

Public ClassLinkMan {

PrivateLong lkm_id;

PrivateString lkm_name;

PrivateString lkm_gender;

PrivateString lkm_phone;

PrivateString lkm_mobile;

PrivateString lkm_email;

PrivateString lkm_qq;

PrivateString lkm_position;

PrivateString lkm_memo;

// Private Long lkm_cust_id;

PrivateCustomer customer; // The Customer object corresponding to the contact

// Generate the get set Method for the above attributes. This method is omitted to save the Layout

}

2.2 Customer. hbm. xml ing configuration file for the object class Customer 2.2.1 Customer's Customer ing File

<? Xml version ="1.0"Encoding =UTF-8"?>

<! DOCTYPE hibernate-mapping PUBLIC

"-// Hibernate/Hibernate Mapping DTD 3.0 // EN"

Http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd>

<Hibernate-mapping>

<! -- ORM: Object Relational Mapping, which establishes ing between Object class O and database table R -->

<Class name ="Com. itheima. hibernate. domain. Customer"Table ="Cst_customer">

<! -- The attribute in the class corresponds to the primary key in the table -->

<! --

Attributes in the id tag: name, column, length, type

Name: the name of the oid in the object class (required)

Column: primary key field in the database table. (This attribute can be omitted if the oid in the object class is the same as the primary key field name in the table)

Length: field length, which is used when hibernate automatically creates a table (not required );

Type: data type (optional)

Java data type ----> type = "java. lang. String"

Hibernate data type ----> type = "String"

SQL data type ----> type = "varchar (32 )"

-->

<Id name ="Cust_id"Column ="Cust_id">

<! -- Primary Key Generation Policy in hibernate: increment, identity, sequence, native, uuid, assigned

      

-->

<Generator class ="Native"/>

</Id>

<! -- Attributes in the class correspond to fields in the table -->

<! -- If the attribute name in the class is the same as the field name in the database, column can be omitted -->

<! --

The field names in the property tag are the same as those in the id tag: name (required), column (not required), length (not required), type (not required)

-->

<Property name ="Cust_name"Column ="Cust_name"/>

<Property name ="Cust_source"Column ="Cust_source"/>

  

<! -- Configure one-to-many Association ing -->

<! --

Set Tag: used to describe the set of multiple parties stored by one party.

* Name: set attribute name of multiple parties

-->

<Set name ="LinkMans">

<! --

Key Tag:

* Name of the foreign key of multiple columns

-->

<Keycolumn ="Lkm_cust_id"/>

<! --

One-to-many label: describes one-to-many relationships.

* Class: full path of classes on multiple sides

-->

<One-to-manyclass ="Com. itheima. hibernate. domain. LinkMan"/>

</Set>

</Class>

</Hibernate-mapping>

2.2.2 contact linkman ing File

<? Xml version ="1.0"Encoding =UTF-8"?>

<! DOCTYPE hibernate-mappingPUBLIC

"-// Hibernate/Hibernate Mapping DTD 3.0 // EN"

Http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd>

<Hibernate-mapping>

<Class name ="Com. itheima. hibernate. domain. LinkMan"Table ="Cst_linkman">

<Id name ="Lkm_id">

<Generator class ="Native"> </Generator>

</Id>

<Property name ="Lkm_name"/>

<Property name ="Lkm_gender"/>

<Property name ="Lkm_phone"/>

<Property name ="Lkm_mobile"/>

<Property name ="Lkm_email"/>

<Property name ="Lkm_qq"/>

<Property name ="Lkm_position"/>

<Property name ="Lkm_memo"/>

<! -- Configure allow-to-one on multiple sides -->

<! --

Allow-to-one label: used to describe the relationship configuration of multiple-to-one.

* Name: attribute name of the object on the first side.

* Class: full path of one class

* Column: name of the foreign key

-->

<Audio-to-onename ="Customer"Class ="Com. rodge. hibernate. domain. Customer"Column ="Lkm_cust_id"/>

</Class>

</Hibernate-mapping>

Iii. hibernate. cfg. xml, the core configuration file of hibernate

(During ssh integration, the core configuration file of hibernate can be written to the spring configuration file)

<? Xml version ="1.0"Encoding =UTF-8"?>

<! DOCTYPE hibernate-configuration PUBLIC

"-// Hibernate/Hibernate Configuration DTD 3.0 // EN"

Http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd>

<Hibernate-configuration>

<Session-factory>

<! -- Database connection information -->

<Property name ="Hibernate. connection. driver_class"> Com. mysql. jdbc. Driver </property>

<Property name ="Hibernate. connection. url"> Jdbc: mysql: // hibernate_day01 </property>

<Property name ="Hibernate. connection. username"> Root </property>

<Property name ="Hibernate. connection. password"& Gt; 123 </property>

<! -- Database Dialect: Different SQL statements are generated based on the underlying database -->

<Property name ="Hibernate. dialect"> Org. hibernate. dialect. MySQLDialect </property>

<! -- Configure and display SQL -->

<Property name ="Hibernate. show_ SQL"> True </property>

<! -- Configure and format SQL -->

<Property name ="Hibernate. format_ SQL"> True </property>

<! -- Configure hbm2ddl -->

<Property name ="Hibernate. hbm2ddl. auto"> Update </property>

 

<! -- Configure the C3P0 connection pool -->

<Property name = "connection. provider_class"> org. hibernate. connection. C3P0ConnectionProvider </property>

<! -- The minimum number of available database connections in the connection pool -->

<Property name = "c3p0. min_size"> 5 </property>

<! -- Maximum number of connections to all databases in the connection pool -->

<Property name = "c3p0. max_size"> 20 </property>

<! -- Set the expiration time of the database connection, in seconds,

If the idle time of a database connection in the connection pool exceeds the timeout time, it is cleared from the connection pool. -->

<Property name = "c3p0. timeout"> 120 </property>

<! -- Check all idle connections in the connection pool every 3000 seconds in seconds -->

<Property name = "c3p0. idle_test_period"> 3000 </property>

<! -- Load the ing file -->

<Mapping resource ="Com/rodge/hibernate/domain/Customer. hbm. xml"/>

<Mapping resource ="Com/rodge/hibernate/domain/LinkMan. hbm. xml"/>

</Session-factory>

</Hibernate-configuration>

<! -- Configure allow-to-one on multiple sides -->

<! --

Allow-to-one label: used to describe the relationship configuration of multiple-to-one.

* Name: attribute name of the object on the first side.

* Class: full path of one class

* Column: name of the foreign key

-->

<Audio-to-onename ="Customer"Class ="Com. itheima. hibernate. domain. Customer"Column ="Lkm_cust_id"/>

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.