Java programmers from the stupid bird to the Rookie (53) detailed Hibernate (iv) Hibernate common configuration Files __java

Source: Internet
Author: User
Tags generator xml parser

Beginner hibernate children's shoes, just open should have this feeling, hibernate configuration file Good trouble, but also less than JDBC access to the database, directly write code, more convenient, with hibernate also write code, but also write configuration, too troublesome. At least that's what I thought when I first started studying. The configuration file does have his dull side, but when you really in-depth study, you can find his boring behind but hide a lot of powerful functions, oh, let me say so iffy, then let us take a look at it, let us witness these configuration files strong.

The configuration of hibernate is divided into two types: one includes basic connection information of hibernate and database, at the initial stage of hibernate work, the information is loaded into configuration and sessionfactory instance The other contains the basic mapping information of the hibernate, that is, the association information between each class in the system and its corresponding database table, at the initial stage of hibernate work, This information is loaded through the Hibernate.cfg.xml mapping node to the configuration and Sessionfactory instances. The two file information contains all the run-time parameters of the hibernate. Below we use a detailed example to illustrate the basic structure and content of these two files.

There are two ways to implement a configuration that contains basic connection information for hibernate and databases:

the first is to use the Hibernate.properties file as the configuration file.

the second is to use the Hibernate.cfg.xml file as the configuration file.

1. Use hibernateproperties as configuration file

For hibernate.properties as a configuration file, it is more suitable for beginners. Because it's often difficult for beginners to remember the format of XML configuration files and what properties need to be configured. Under the ETC path of the Hibernate release package, a hibernate.properties file is provided that lists all the properties of the hibernate. Each configuration segment gives a rough comment, and the user can quickly configure hibernate and database links by simply canceling the comment on the required configuration segment to give a way to create a configuration object using the Hibernate.properties file.

Instantiating a Configuration object

Configuration cfg = new Configuration ()

Call the AddResource () method multiple times to add a mapping file

Cfg.addresource ("Item.hbm.xml")

Cfg.addresource ("Bid.hbm.xml");

Viewing the hibernate.properties file found that the file did not provide a way to hibernate the mapping file. Therefore, when using the Hibernate.properties file as a configuration file, you must use the configuration. AddResource () method to add the mapping file.

Note: as shown in the preceding code, it is easy to configure Hibernate with hibernate.properties files, but because the mapping file is added manually, it is a very tear-tearing thing to do when the mapping file is extremely long. This is why the Hibernate.properties file is not commonly used as a configuration file in actual development.

There is, of course, another policy to add a profile, because the mapping file and the persistence class are one by one corresponding, you can add the persisted class by configuration the object to allow hibernate to search for the mapping file themselves.

Instantiating a Configuration object

Configuration cfg = new Configuration ()

Call the AddClass () method multiple times to add a persistent class directly

CFG. addclass (PPP. Item.class)

CFG. addclass (PPP. Bid.class);

2. Use hibernate.cfg.xml as configuration file

 

I don't feel the need to say much about XML configuration forms. The following example is sufficient to illustrate this configuration, and let's look at a hibernate.cfg.xml file with detailed comments:

 

<!--the starting line of the standard XML file, version= ' 1.0 ' indicates the version of XML, encoding= ' gb2312 ' indicates how the XML file is encoded--> <?xml version= ' 1.0 '

gb2312 '?> <!--indicates the location of the DTD document that resolves the xml file, the DTD is the abbreviation for the DocumentType definition, the definition of the document type, and the XML parser uses the DTD document to check the legality of the XML file.

HIBERNATE.SOURCEFORGE.NET/HIBERNATE-CONFIGURATION-3.0DTD can find this file in the Src\org\hibernate directory in the Hibernate3.1.3 package--> <! DOCTYPE hibernate-configuration Public "-//hibernate/hibernate configuration DTD 3.0//en" "Http://hibe RNATE.SOURCEFORGE.NET/HIBERNATE-CONFIGURATION-3.0.DTD > <!--Declare the start of Hibernate configuration file-->  


The above should be most commonly used profile properties, of course, many parts of it can be in the configuration of Hibernate development environment automatically generated, the beginning of the time or suggest that everyone manually to configure, you can reach the familiar time in the use of automatic generation

Let's continue to look at the configuration file that contains the basic mapping information for hibernate, which is the association information between each class in the system and its corresponding database table. This configuration file is generally named: Class name. Hbm.xml, let's look at the class name by a specific code example. Hbm.xml Structure:

<?xml version= "1.0"?> <! DOCTYPE hibernate-mapping Public "-//hibernate/hibernate mapping DTD 3.0//en" "http://hibernate.sourceforge.net/ Hibernate-mapping-3.0.dtd ">  

Let's take a look at the following tags:

1.hibernate-mapping
This element includes some optional attributes. Schema and Catalog properties that indicate the schema and/or catalog name of the table where the map is connected (refer). If this property is specified, the table name is extended to the fully qualified name, plus the specified schema and catalog name. If not specified, the table name does not use a fully qualified name. DEFAULT-CASCADE specifies what type of default cascading style the Java attribute and collection class Hibernate will take if the Cascade attribute is not explicitly indicated. The Auto-import property defaults to allow us to use the class name of a fully qualified name in the query language.

 


(1) schema (optional): The name of the database schema.
(2) catalog (optional): The name of the database catalog.
(3) Default-cascade (optional-default to None): The default cascading style.
(4) default-access (optional-default property): Hibernate the policy used to access all properties. Can be customized by implementing the PropertyAccessor interface.
(5) Default-lazy (optional-default is True): Specifies the Java attributes and collection classes that do not explicitly indicate the lazy attribute, and what type of default load style Hibernate will take
(6) Auto-import (optional-default is True): Specifies whether we can use the unqualified class name in the query language (only the classes in this mapping file).
(7) package (optional): Specifies a package prefix, which is used as the package name if the fully qualified class name is not specified in the mapping document.

2.class
Use the class element to define a persisted class:

<class
name= "ClassName" (1)
table= "tablename" (2)
discriminator-value= "Discriminator_value" (3)
mutable= "True|false" (4)
schema= "owner" (5)
catalog= "Catalog" (6)
proxy= "Proxyinterface" (7)
Dynamic-update= "True|false" (8)
dynamic-insert= "True|false" (9)
select-before-update= "True|false" (10)
polymorphism= "Implicit|explicit" (one)
where= "arbitrary SQL Where Condition" (a)
persister= " Persisterclass "(batch-size=)"
N "
optimistic-lock=" None|version|dirty|all "(lazy=)
False ()
entity-name= "EntityName"
check= "arbitrary SQL Check Condition" (a)
rowid= "rowID" (19 )
subselect= "SQL expression"
abstract= "True|false" node=
"Element-name"
/>


(1) name (optional): The Java fully qualified name of the persisted class (or interface). If this property does not exist, Hibernate will assume that this is a POJO entity mapping.
(2) Table (optional-default is a fully qualified name of the Class): The corresponding database table name.
(3) Discriminator-value (optional-Default and class name): A value that distinguishes between different subclasses and is used in polymorphic behavior. The values that it can accept include null and NOT NULL.
(4) mutable (optional, default value is True): Indicates that the instance of the class is mutable or immutable.
(5) schema (optional): Overwrites the schema name specified in the root (6) Catalog (optional): Overwrites the catalog name specified in the root (7) Proxy (optional): Specifies an interface to be used as an agent when loading is deferred. You can use this class's own name here.
(8) dynamic-update (optional, default to FALSE): Specifies that the SQL used for update will be dynamically generated at run time, and only those fields that have changed are updated.
(9) Dynamic-insert (optional, default to FALSE): Specifies that the SQL used for the insert will be dynamically generated at run time and contain only those Non-null value fields.
(Select-before-update (optional, default to FALSE): Specifies that the SQL update operation will not be performed unless the object is determined to have been actually modified (if the value is True) (Hibernate). On a particular occasion (in fact, it only takes effect in update () when an instantaneous object (transient object) is associated to a new session), which means that Hibernate performs an additional SQL SELECT operation before update To determine whether UPDATE should be performed.
(polymorphism) (optional, default value is implicit (implicit)): Defines whether to use polymorphic queries implicitly or explicitly (this is only

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.