Hibernate's mapping file configuration

Source: Internet
Author: User

The mapping of object relationships is illustrated with an XML document. Mapping documents can be generated using tools such as Xdoclet,middlegen and Andromda. The mapping element is explained below from a map example, and the code for the mapping file is as follows:

<?xml version= "1.0"?><!--all XML mapping files need to define the DOCTYPE as shown below. Hibernate searches for the DTD file in its classpath (Classptah) first. --><! DOCTYPE hibernate-Mapping Public"-//hibernate/hibernate Mapping DTD 3.0//en" "Http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"><!--Hibernate-mapping has several optional properties: The Schema property indicates the schema name of the mapped table. default-The Cascade property specifies that the default cascading style values are none, save, update. AutoThe-import property defaults to allow us to use the class name of a non-fully qualified name in the query language to have a value oftrue、false. The Package property specifies a packet prefix. -->
    -mapping schema= "SchemaName"default-cascade= "None"Auto-Import= "true" Package= "Test"><!--define a persisted class with the class element--><className= "People" table= "person"><The!--ID element defines the mapping of the property to the primary key field of the database table. -->
    = "id"><!--used to generate a unique identity for an instance of the persisted class-->
    class= "Native"/></id><!--discriminator Recognizer is a mapping method for defining inheritance relationships-->
    = "Subclass" type= "character"/><!--property element declares a persisted, JavaBean-style attribute for the class-->
    = "Name" type= "string">
    = "Name" length= "not-"NULL= "true"/></Property>
    = "Sex" not-NULL= "true"Update= "false"/><!--Many-to-one mapping relationship>
    -to-one name= "Friend"column= "friend_id"Update= "false"/><!--Setting the correlation relationship-->
    = "Friends" inverse= "true" order-by= "id">
    = "friend_id"/><-one-to-many mappings-->
    -to-manyclass= "Cat"/></set></class></hibernate-mapping>

How components are applied

There are two types of components, component (component) and dynamic-component. In the configuration file, the component element is a mapping between the elements of the child object and the fields of the parent class's corresponding table. The components can then declare their own properties, components, or collections. The definition of the component element is as follows:

In this code, name refers to the property name, class is the name of the classes, insert refers to whether the mapped field appears in the SQL INSERT statement, upate indicates whether the mapped field appears in the SQL UPDATE statement, and access indicates the policy to access the property.

Basic configuration of Hiebernate

Hibernate database connection information is loaded from the configuration file. Hibernate's configuration file is available in two forms: a file in XML format and a Properties property file. The configuration file default file name for the properties form is hibernate.properties, and the contents of a configuration file in the Properties form are as follows:

The configuration file contains a list of properties that hibernate will use to connect to the database.

In an XML-formatted configuration file, in addition to the basic Hibernate configuration information, you can also specify a mapping file for a specific persisted class, which avoids hard-coding the configuration file of the persisted class into the program. The default file name for the configuration file in XML format is Hibernate.cfg.xml, and an example of an XML configuration file is shown below:

 
    
     
     
     
      !-- Displays the SQL statement executed-->
      
       true 
      !--connection string-->
      
       jdbc:mysql:// Localhost:3306/stu 
       The user name of the!--connection database-->
      
       root 
      !-- Database user password-->
      
       root 
      !--database driver-->
      
       com.mysql.jdbc.driver 
      !--Select dialect-->
      
       org.hibernate.dialect.mysqldialect 
      !--mapping file-->
      < !--mapping file-->
       
      
     

The Properties form the configuration file and the XML format configuration file can be used at the same time. When you use both types of profiles, the settings in the XML configuration file override the same properties of the properties configuration file.

Hibernate's mapping file configuration

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.