Getting started with Hibernate BLOG [9. Multiple-to-one Hibernate ing of Hibernate Object Relationships]

Source: Internet
Author: User

[Java]
Multiple employees-department 1
We can create an object model first. Then the database automatically creates the table. The following is a case study:
1. Create two moel objects: (the set and get methods are omitted here)
Dept. java
Private int id;
Private String deptname;

Employee. java
Private int id;
Private String empname;
// Multiple-to-one relationship. Set the attribute of one party to multiple parties
Private Dept depart;
2. Create a ing File
Dept. hbm. xml
This configuration and the configuration object mentioned in the previous blog have no complex attributes. The Code is as follows:
<? Xml version = "1.0"?>
<! DOCTYPE hibernate-mapping PUBLIC
"-// Hibernate/Hibernate Mapping DTD 3.0 // EN"
Http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd>
<! -- Specify the package of the mapped class -->
<Hibernate-mapping
Package = "cn. hibernate. model">
<! -- Table can be left empty. By default, the table name is similar to the class name. -->
<Class name = "Dept" table = "dept">
<! -- Id indicates the primary key, while name = id indicates the attributes in the class. That is, the attribute corresponding to the primary key.
Unsaved-value determines whether it is set as an instantaneous object. The following value is used by default.
<Id name = "id" unsaved-value = "-1">
-->
<Id name = "id">
<! -- Primary key generator -->
<Generator class = "native"/>
</Id>
<! -- Column can also be left empty by default. If not, the attribute name and column name are the same. No need to configure -->
<Property name = "deptname" column = "deptname"/>
</Class>

</Hibernate-mapping>
 
 
 
Employee. hbm. xml: Because Employee has a Dept attribute. It is a many-to-one relationship. Therefore, a tag called upload-to-one is used. The Code is as follows:
<? Xml version = "1.0"?>
<! DOCTYPE hibernate-mapping PUBLIC
"-// Hibernate/Hibernate Mapping DTD 3.0 // EN"
Http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd>
<! -- Specify the package of the mapped class -->
<Hibernate-mapping
Package = "cn. hibernate. model">
<! -- Table can be left empty. By default, the table name is similar to the class name. -->
<Class name = "Employee" table = "employee">
<! -- Id indicates the primary key, while name = id indicates the attributes in the class. That is, the attribute corresponding to the primary key.
Unsaved-value determines whether it is set as an instantaneous object. The following value is used by default.
<Id name = "id" unsaved-value = "-1">
-->
<Id name = "id">
<! -- Primary key generator -->
<Generator class = "native"/>
</Id>
<Property name = "empname"> </property>
<! -- The default ing primary key is the name attribute value plus _ id. In this way, the id of the Dept table can be associated.
If you want to manually specify the primary key, add a foreign key to specify the property-ref = ""
If the column attribute is not available, the default attribute is depart, which is the same as the name attribute. -->
<Role-to-one name = "depart" column = "depart_id"> </role-to-one>

</Class>
</Hibernate-mapping>
 
3. The model class and configuration file on the object are all mentioned in the previous blog. I will not talk about it here. First, write the code for generating the table. It is also an insert operation. Hibernate automatically generates related tables based on the relationship of the Object Model:
Static void addToEmpAndDept (){
Session s = null;
Transaction tx = null;
Try {
S = HibernateUtil. getSession ();
Tx = s. beginTransaction ();
Dept dept = new Dept ();
Employee emp = new Employee ();
Dept. setDeptname ("bumen1 ");
Emp. setEmpname ("emp1 ");
Emp. setDepart (dept );
S. save (dept );
S. save (emp );
Tx. commit ();
} Catch (HibernateException e ){
If (tx! = Null ){
Tx. rollback ();
}
Throw e;
} Finally {
If (s! = Null ){
S. close ();
}
}
}
 
<P> ing relationship description: </p> <p> Hibernate is An ORM ing framework. So how is it mapped? Next, I will briefly describe my thoughts on the above cases. Welcome to shoot bricks: </p> <p> the key to completing this series of operations is the configuration file bean Of the JavaBean and bean. hbm. xml </p> <p> database generation is based on bean. hbm. xml configuration file. The orm relationship is </p> <p> Class ---- table </p> <p> Property ----- column </p> <p> Class I have nothing to say about the Table. The correspondence between column is divided into simple column and property, that is, some basic types. You can get the results through the experiment. Therefore, the complex correspondence is directed-to-one (for the content of this BLOG) Because emp and dept are related to multiple parties. Then we should use sequence-to-one. Note: The name configuration in the complex attribute is the name of the object corresponding to dept in the JavaBean. Column corresponds to a dept_id, which is the default mechanism of hibernate. That is, it will find the object type corresponding to dept. Then, find the object type through the reflection mechanism. Then create a database and insert operations. </P>
Author: zhang6622056

Related Article

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.