There are two basic configuration files for hibernate:hibernate.cfg.xml and model.hbm.xml files.
hibernate.cfg.xml contains the basic connection information of hibernate and database, which is loaded into the configuration and Sessionfactory instances in the initial stage of hibernate work;
model.hbm.xml contains the basic mapping information of hibernate, which is the association information between each class in the system and its corresponding database table, in the initial stage of hibernate work, This information is loaded into the configuration and Sessionfactory instances through the Hibernate.cfg.xml mapping node.
Both file information contains all the run-time parameters for hibernate. Below we use detailed examples to illustrate the basic structure and content of the two files.
I.. hibernate.cfg.xml file:
<!--the beginning of the file, for hibernate, this type of file basically begins:)--<?xml version= ' 1.0 ' encoding= ' UTF-8 '?> <! DOCTYPE hibernate-configuration Public "-//hibernate/hibernate configuration DTD 3.0//en" "Http://hibernate. Sourceforge.net/hibernate-configuration-3.0.dtd "> <!--body Start-up
Ii.. hbm.xml file:
Since Hibernate's association includes a pair of one or one-to-many, many-to-one, and many-to-many four types, there are four. hbm.xml files that correspond to each other.
In the following example, we will introduce the basic structure and contents of the. hbm.xml file by comparing the common two-way "one-to-many" relationship.
For more detailed information about Hibernate, please refer to the relevant literature.
In this example, there are two database tables: one for the "Province" table and the other for the "City" table, and the database used for MySQL. The table statements are as follows:
CREATE TABLE IF not EXISTS province ( Guid int. not null auto_increment, provincename VARCHAR (+) not null , PRIMARY KEY (Guid) ) Type=innodb; CREATE TABLE IF not EXISTS city ( Guid int. not NULL auto_increment, cityname VARCHAR (+) not NULL, Provinceid INT not NULL, PRIMARY KEY (Guid) ) Type=innodb; ALTER TABLE City ADD CONSTRAINT cityrfprovince FOREIGN KEY (Provinceid)
Province table is the main control side, the city table is the accused side, there is a two-way relationship between them. Table city is associated with table province through foreign key Provinceid: When a record in the table province is deleted, the relevant record in table city is deleted, and the relevant record in table city is not changed when there is a record in the table province is saved or updated.
Modify the Province.hbm.xml file generated by the Hibernate self-middlegen tool to read as follows:
<!--the beginning of the file, for hibernate, this type of file basically begins:)--<?xml version= "1.0"?> <! DOCTYPE hibernate-mapping Public "-//hibernate/hibernate mapping DTD 2.0//en" "Http://hibernate.sourceforge. NET/HIBERNATE-MAPPING-2.0.DTD > <!--mapping file start-up
Make a modification to the City.hbm.xml file generated by the Hibernate Middlegen tool, as follows: <!--the beginning of the file, for hibernate, this type of file basically begins:)--<?xml vers Ion= "1.0"?> <! DOCTYPE hibernate-mapping Public "-//hibernate/hibernate mapping DTD 2.0//en" "Http://hibernate.sourceforge. NET/HIBERNATE-MAPPING-2.0.DTD > <!--mapping file start-up
Multi-to-many bidirectional correlation (intermediate table)
In a many-to-many association, you can split into two one-to-many relationships, which is to add an association table to the middle of two tables and record the association between the two tables. If you throw away the correlation table, there is no relationship between the original 2 tables.
For example, consider the test category and exam subjects, and explain in detail the hibernate settings between two tables in the case of adding the associated table.
Exam Category table: Exam_sort_enum
Id:integer
Name:string
Examination Chart: Subject
Id:integer
Name:string
Exam Category Account Association table
Exam_sort_enum_id:integer ID of the corresponding exam category table
Subject_id:integer ID of the corresponding Exam account table
ExamSortEnum.hbm.xml
<?xml Version= "1.0"? ><! DOCTYPE hibernate-mapping Public "-//hibernate/hibernate mapping DTD 3.0//en" "Http://hibernate.sourceforge.net/hi Bernate-mapping-3.0.dtd ">
Examsortenum.java
Package Model;import Java.io.serializable;import Java.util.hashset;import java.util.set;public class ExamSortEnum implements serializable{private static final long serialversionuid = 1l;private Integer id;private String Name;priva Te Set subject = new HashSet ();p ublic examsortenum () {}public examsortenum (Integer id) { setId (ID);} public void Setsubject (Set subject) { this.subject = subject;} Public Set Getsubject () { return this.subject;} Public Integer getId () { return ID;} public void SetId (Integer id) { this.id = ID;} Public String GetName () { return name;} public void SetName (String name) { this.name = name;}}
About mapping Files:
To set the mapping file,
Name=subject stores multiple subject types of containers for set objects defined in the Examsortenum.java file.
Table= "Exam_sort_enum_subject" is the table name of the corresponding intermediate table, because the two business tables can not deal directly with each other, only through the intermediate table to be associated.
Lazy= "true" delay loading
Cascade= "Save-update" cascade save Update, if set to All,delete,all-delete-orphans, when a Examsortenum object is deleted, the associated subject object is also deleted. The subject object of this association may be referenced by other Examsortenum objects.
<key column= "exam_sort_enum_id"/> Specifies that the foreign key in the intermediate table referenced as Exam_sort_enum table is exam_sort_enum_id
<many-to-many>
Column= "subject_id" exam_sort_enum table references subject table's foreign key is subject_id, that is Exam_sort_enum table is associated with subject_id table via subject
Class= "model. Subject "Specifies that the model is stored in Name=subject in set. The Subject object.
Subject.hbm.xml
<?xml version= "1.0"? ><! DOCTYPE hibernate-mapping public "-//hibernate/hibernate mapping DTD 3.0//en" "http// Hibernate.sourceforge.net/hibernate-mapping-3.0.dtd ">
Subject.java
Package Model;import Java.io.serializable;import Java.util.hashset;import java.util.set;public class Subject Implements Serializable{private static final long serialversionuid = 1l;private Integer id;private String name;private Set Examsortenum = new HashSet ();p ublic Subject () {}public Subject (Integer id) { setId (ID);} public void Setexamsortenum ( Set examsortenum) { this.examsortenum = examsortenum;} Public Set Getexamsortenum () { return this.examsortenum;} Public Integer getId () { return ID;} public void SetId (Integer id) { this.id = ID;} Public String GetName () { return name;} public void SetName (String name) {
About mapping Files:
Content is similar to ExamSortEnum.hbm.xml, just a inverse= "true", tell hibernate control is not here, two mapping files can only be set one.Test class:
Package Model;import Org.hibernate.session;import Org.hibernate.sessionfactory;import org.hibernate.Transaction; Import Org.hibernate.cfg.configuration;public class Examsortenumtest {public static sessionfactory SF; static{try{C onfiguration cfg = new Configuration (). Configure (); SF = Cfg.buildsessionfactory (); }catch (Exception e) {e.printstacktrace (); }}public void Insert1 () {Session sess = sf.opensession (); Transaction tx = Sess.begintransaction (); Subject sa = new Subject (); Sa.setname ("A1"); Subject sb = new Subject (); Sb.setname ("B1"); Subject sc = new Subject (); Sc.setname ("C1"); Examsortenum Esea = new Examsortenum (); Esea.setname ("A"); Esea.getsubject (). Add (SA); Esea.getsubject (). Add (SC); Examsortenum eseb = new Examsortenum (); Eseb.setname ("B"); Eseb.getsubject (). Add (SB); Eseb.getsubject (). Add (SC); Sess.save (ESEA); Sess.save (ESEB); Tx.commit (); Sess.close (); }public static void Main (string[] args) {examsortenumtestET = new examsortenumtest (); Et.insert1 ();}}
Results after execution:
Exam Category table: Exam_sort_enum
ID Name
3 A
3 E
Examination Chart: Subject
ID Name
3 C1
4 A1
5 B1
Exam Category Account Association table
exam_sort_enum_id subject_id
3 3
3 4
4 3
4 5
Hibernate configuration file