Hibernate O / R mapping of the foundation

  Hibernate (hereinafter referred to hiber) for the first study was completed.    Learning process, summed up some experience and 

  Learning notes, special and share out of, as I am limited capacity and can not be proficient hiber, also requested the 

  - Identification and if found any mistakes, but also look generous advice, thank you to the Fuzheng. 

  Hiber of the process, I bought the book: "easy to hibernate," Xia Xin, Cao Xiaogang, of the Tang Yong, since 

  I feel good writing, the real easy to do.    But I think the inadequacies of the few instances in actual combat poor.    Need 

  To everyone to think of more ways to achieve some applications, the only way to truly experience the essence of which, to his own use. 

  Gangchachu hiber time, online learning how to find a postcard hiber article seriously for reading, keep in mind that the author can not be 

  In order to study the hiber learning, and should be a higher level to understand its meaning.    Our article is also a jr 

  / Article/24993.htm, when we can look at the study. 

  Hiber's main task is to achieve data persistence layer operation, and what persistence layer?    Can be understood as: the level of the system logic 

  , The focus on achieving a lasting data of a relatively independent of the field.    Hiber is to be accomplished by achieving in the field 

  Data manipulation, and this is not complete as long as the general realization of the ultimate goal of complete if completed, the performance of its pursuit of elimination 

  Consumption small coupling of small, data encapsulation, and so on-is it really sense the completion of the data persistence layer operation. 

  Hiber the basis of the allocation and use of this no longer speak, if you ready for a school is not your hiber these obstacles.    Below I 

  Really belong to discuss some obstacles. 

  Hiber the o / r mapping 
  O: object, r: relational.    Put together is the relationship between objects.    Hiher itself and should be regarded as special java object-oriented 

  And the traditional relations of the contradiction between the product data.    Also on the web and hiber based on the basic understanding, then o / r mapping undoubtedly 

  Orm framework has become the most critical element. 

  Or mapping in the study, we first on the basic data types hiber understand. 
  Below is the link http://hi.baidu.com/rufeizi/blog/item/243279f49db50dd9f2d385fb.html 

  Hiber java data types and the type of data comparison, we can look at. 

  Entities and mapping technology as a form of the link between the realization orm plays a vital role.    With the hiber 

  Households, the mapping between more reflected in the configuration file maintenance process.    Hiber chosen as its mapping xml configuration file 

  Since the benefits Needless to say, as long as the xml knowledge understand, we can understand this point. 

  We configuration o / r mapping, the first is 
  1. Class name and the mapping table, 
  2. Followed by the primary key mapping, 
  3. Finally field mapping. 

  A complete o / r mapping is the basis of the establishment of the existence of the database table, and then have a database table by table-type configuration media 

  Referral. Hbm.xml document.    Finally write entity categories, namely from the database tables mapping out the java class. 

  I have here a complete o / r mapping, we can look at: 
  In accordance with the above steps, first of all, a database table: 
  CREATE TABLE `reg` ( 
  `Id` int (6) unsigned NOT NULL auto_increment, 
  `Username` varchar (16) default NULL, 
  `Password` varchar (20) default NULL, 
  “ Int phone (11) default NULL, 
  “ Email varchar (30) default NULL, 
  “ Address varchar (50) default NULL, 
  `Sex` varchar (4) default NULL, 
  PRIMARY KEY ( `Id`) 
  ) InnoDB DEFAULT CHARSET ENGINE = = gbk; 
  In the data table on the basis of the establishment. Hbm.xml documents: 
  <? Xml version = "1.0" encoding = "UTF-8"> 
  <! DOCTYPE hibernate-mapping PUBLIC 
  "- / / Hibernate / Hibernate Mapping DTD 3.0 / / EN" 
  "Http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> 
<hibernate-mapping>

  <class Name="mypack.Customer" table="reg"> 
  <comment> Users may bid for or sell auction items. </ Comment> 

  <Id name = "id" 
  Column = "id" 
  Type = "int"> 
  <generator Class="native"/> 
  </ Id> 

  <Property name = "username" 
  Not-null = "true" 
  Column = "username" 
  Type = "string" /> 

  <Property name = "password" 
  Not-null = "true" 
  Column = "password" 
  Type = "string" 
/>

  <Property name = "phone" 
  Not-null = "true" 
  Column = "phone" 
  Type = "string" 
/>

  <Property name = "email" 
  Not-null = "true" 
  Column = "email" 
  Type = "string" 
/>

  <Property name = "address" 
  Not-null = "true" 
  Column = "address" 
  Type = "string" 
/>

  <Property name = "sex" 
  Not-null = "true" 
  Column = "sex" 
  Type = "string" 
/>
  </ Class> 

  </ Hibernate-mapping> 

  Then established entities categories: 
  Package mypack; 

  Import java.io.Serializable; 

  Public class Customer implements Serializable ( 

  Private int id; 
  Private String username; 
  Private String email; 
  Private String password; 
  Private String phone; 
  Private String sex; 
  Private String address; 

  Public Customer () () 

  Public int getId () ( 
  Return id; 
  ) 
  Public void setId (int id) ( 
  This.id = id; 
  ) 

  Public String getUsername () ( 
  Return this.username; 
  ) 
  Public void setUsername (String username) ( 
  This.username = username; 
  ) 

  Public String getPassword () ( 
  Return password; 
  ) 
  Public void setPassword (String password) ( 
  This.password = password; 
  ) 

  Public String getPhone () ( 
  Return this.phone; 
  ) 
  Public void setPhone (String phone) ( 
  This.phone = phone; 
  ) 

  Public String getEmail () ( 
  Return email; 
  ) 
  Public void setEmail (String email) ( 
  This.email = email; 
  ) 

  Public String getAddress () ( 
  Return this.address; 
  ) 
  Public void setAddress (String address) ( 
  This.address = address; 
  ) 

  Public String getSex () ( 
  Return this.sex; 
  ) 
  Public void setSex (String sex) ( 
  This.sex = sex; 
  ) 
  ) 

  <class Name="mypack.Customer" table="reg"> 

  Below on this to be a document mapping analysis: 
  1. Category-table should be configured: 
  <class Name="mypack.Customer" table="reg"> 
  Name designated mapping class names. 
  Table designated by the corresponding database table 
  2.id mapping configuration 
  <id Name="id" column="id" type="int"> 
  <generator Class="native"/> 
  </ Id> 
  Name specified in the current mapping of the properties: id, the corresponding database table in the main table reg key fields 
  Column designated the current mapping table reg id for the unique identifier for this field. 
  Type designation of the current field data type 
  Generator designated a primary key generation, in the main on hiber bond formation mechanism of the relevant here, we can 

  To see: http://blog.csdn.net/watano_cc/archive/2006/02/28/612286.aspx 
  3. Attribute / database field mapping configuration: 
  <Property name = "address" 
  Not-null = "true" 
  Column = "address" 
  Type = "string" /> 
  Believe that this would not have introduced the small phase?    We look at to understand [:)] 

  Finally, we said that this path, 
  . Hbm.xml default on documents generated by the entities of the class where the paper package.    Such a complete o / r mapping on the establishment of a fact, the crux of the problem is not operating itself, but rather whether the idea is clear, as long as a clear line of thinking, then others stated in accordance with the configuration methods, operation can definitely be successful. 

Share and Enjoy: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Digg
  • del.icio.us
  • Facebook
  • DotNetKicks
  • DZone
  • Netvouz
  • Propeller

Tags:

Releated Java Articles

Comments

Leave a Reply