Of many that can be converted to two one-to-many
<set name="students" table="teacher_student"> <key column="techer_id"/> <many-to-many column="student_id"/> </set>many-to-many data only from one end of the maintenance, if at both ends of maintenance, will be reported to the conflict between form the primary key of the error message.
Many-to-many query
Ways inquiries get no end of data, when the call when the other side of the data, there will be two query
select * from techer 查询一端 select * from t_s left outher join studen on ... 关联中间表和另一端查询。does not recommend direct multiterminal information call getStuentds () will cause performance problems, there is no paging, remove all.
Many-to-many update, delete TODO
12. Component relationship
Can be two one-on-one objects, mapped to a table
<component name="name"> <property name="firstName" column="first_name"/> </compoent>
13.Hiernate Set Types
Set: there is no duplication of values, there is no sequence
List: to repeat, has the order
<list name="emps"> <key column="department_id"/> <list-index column="order_col"/> 需要保存顺序 <one-to-many classs="Employee"/> </list>do not need to preserve the order necessary, you can use the bag
<bag name="emps"> <key column="depart_id"/> <one-to-many/> </bag>
bag are Hibernate support, the corresponding java's List
Map:
Map <String,Employee> emps;
Array collection
Collection selection
The vast majority of the use of set
If the habit of using the List, but do not order, can be mapped to bag
At the definition of the relationship between object model, the only use of Set, List interfaces marked relationship, and should not use an implementation can not be used (HashSet, ArrayList), because Hibernate within a treatment, the use of Hiberante implementation of set, list interface, increase in load late functions.
14 Cascade operation
cascade: multiple use "," separate
1) none: do not cascade, default
2) save-update: save / update the object at the same time save / update the linked object, when the main table are deleted (or call set (null), getxxx (). Clear () method) to set up foreign key association table for the null, the main table to delete records
3) all: all are concatenated, save-update, delete
4) delete: one-to-many situations: when delete one record, it will delete information associated table, delete the main table at the same time information. If in the case of many-to-one, multi-table delete a record, will delete the corresponding record in table 1, many other records related table will be update null. In the case of many-to-many in general do not use.
5) delete-orphan only at one-to-many situation, the relationship between father and son will delete orphaned node.
Department department = (Department)session.get(Department.class, 1); List<Employee> employees = department.getEmployees(); Iterator<Employee> iter = employees.iterator(); Employee employee = iter.next(); employee.setDepartment(null); employees.remove(employee); //employee 成为孤儿
If you use the save-update employee table will not be deleted, it will only update the foreign key will be null, if you use delete-orphan records will be deleted.
The use of
Many-to-one, many-to-many in general do not cascade
The use of one-to-many general save-update
One-on-one, the general increase in the main object can be set to cascade all
15. Inverse default false
Multiterminal maintaining the relationship will bring about better performance. Save time save one, in the preservation of the many-fold, you can dispense with update statement.
If one is ordered, such as List, should not set the inverse = "tue", because the order had not been preserved.
inverse only in the collection have
Many do not allow the use of one end of the inverse.
Many-to-many: one end can have any maintenance, only one end of the maintenance permit.







