Association relationships Many-to-many
Chestnuts: An employee can write multiple projects, and a project is done by multiple programmers, which is a typical many-to-many instance
The following gives the two entity class employee (Employee entity), Project (Project entity), based on an instance.
One. One-way, many-to-many: (Add a project and add two employees to the project.) )
Implant project properties in the employee table
To configure the Employee.hbm.xml file:
Start one side:
@Test //bidirectional Many-to-many association relationship 2. Add multiple employees to a department under public Void Manytomanyt1 () { Session session = Hibernateutil.getsession ( ); Transaction tx=session.begintransaction (); Employee Emp=new employee (); Emp.setempname ("pheasant"); Project Pro1=new project (); Pro1.setproname ("Security Department"); Project Pro2=new project (); Pro2.setproname ("Finance Department"); Emp.getprojects (). Add (Pro1); Emp.getprojects (). Add (Pro2); Session.save (EMP); Session.save (Pro1); Session.save (PRO2); Tx.commit (); }
Results:
Two. Many-to-many two-way (query project under a few employees/query each employee has several projects)
To populate an employee's Employee attribute in the project table:
Configuration Project.hbm.xml file (IBID.):
Test:
@Test //bidirectional Many-to-many association relationship 1. Retrieve the employee name and find out the project name public void T () { Session session = Hibernateutil.getsession (); Employee employee = Session.get (Employee.class, +); SYSTEM.OUT.PRINTLN ("Employee Name:" +employee.getempname ()); SYSTEM.OUT.PRINTLN ("Employee's Project:"); For (Project project:employee.getProjects ()) { System.out.println (Project.getproname ()); } }
Results:
Hibernate: Select employee0_.empid as empid1_2_0_, Employee0_.empname as empname2_2_0_ from ANNAN. Employee employee0_ where employee0_.empid=? Employee Name: Pheasant Staff Project: Hibernate: select Projects0_.eid as eid1_5_0_, projects0_.pid as pid2_5_0_, project1_.proid as proid1_3_1_, project1_.proname as proname2_ 3_1_ from empandpro projects0_ inner join ANNAN. Project project1_ on projects0_.pid=project1_.proid where projects0_.eid=? Security Department Finance Department
Lazy Loading:
Lazy Loading is when the data is really needed , The SQL statement is executed to query, avoiding the unnecessary performance overhead.
Deferred load classification:
query policy at the class level
the query strategy for one-to-many and many-to-many associations
A. a multiple-to-one correlation query strategy
A. Class-level query policy : immediate Retrieval and deferred retrieval , default to deferred retrieval
Class-Level lazy loading @Test public void T3 () {
The default lazy is true session session = Hibernateutil.getsession (); Employee employee = Session.load (Employee.class, +); System.out.println (); }
Effect:
When the class's lazy is false:
Effect:
One -to-many and many-to-many correlated query strategies:
::::::: One -to-many or many-to-many retrieval strategies are determined by lazy and fetch : ::::::
in the mapping file, the <set> element is used to configure a one-to-many association and many-to-many association relationships. <set> elements have lazy and fetch attributes
When the value of 1.lazy is true: Lazy loading
Results:
When the value of 2.lazy is false: Load immediately
Fetch FETCH value
Join: Urgent left OUTER join
Select: Multiple simple SQL(default)
Subselect: Sub-query
Fetch and lazy combinations
Parse:fetch= "join" lazy will be ignored, urgent left outer join immediate retrieval
Fetch= "Select" Lazy= "false" multiple simple SQL retrieve now
Fetch= "Select" Lazy= "true" multiple statements deferred retrieval
Fetch= "Select" lazy= "Extra" multiple statements and their lazy search
Fetch= "Subselect" lazy= "false" subquery immediate Retrieval
Fetch= "Subselect" lazy= "true" subquery deferred retrieval
Fetch= "Subselect" lazy= "Extra" subquery and its lazy search
Join:
<set name= "Projects" table= "Empandpro" fetch= "join" >
Hibernate: Select employee0_.empid as empid1_2_0_, employee0_.empname as empname2_2_0_, projects1_ . Eid as Eid1_5_1_, project2_.proid as pid2_5_1_, project2_.proid as proid1_3_2_, Project2_.proname as Proname2_3_2_ from ANNAN. Employee employee0_ left outer join Empandpro projects1_ on employee0_.empid=projects1_.eid Left outer join ANNAN. Project project2_ on projects1_.pid=project2_.proid where employee0_.empid=? Employee Name: Pheasant Staff Project: Finance Department Security Department
Default Select:
<set name= "Projects" table= "Empandpro" fetch= "select" >
Hibernate: Select employee0_.empid as empid1_2_0_, Employee0_.empname as empname2_2_0_ from ANNAN. Employee employee0_ where employee0_.empid=? Employee Name: Pheasant Staff Project: Hibernate: select Projects0_.eid as eid1_5_0_, projects0_.pid as pid2_5_0_, project1_.proid as proid1_3_1_, project1_.proname as proname2_ 3_1_ from empandpro projects0_ inner join ANNAN. Project project1_ on projects0_.pid=project1_.proid where projects0_.eid=? Security Department Finance Department
When the value of 3.lazy is extra: Enhanced lazy loading: (
only the properties of the collection object are loaded, when the properties of the collection itself are accessed (for example, the collection size, build count), are not loaded immediately.
)
Results:
System.out.println (Projects.size ());
Hibernate: Select employee0_.empid as empid1_2_0_, Employee0_.empname as empname2_2_0_ from ANNAN. Employee employee0_ where employee0_.empid=? Hibernate: Select count (PID) from Empandpro where Eid =?2
Many-to-many and deferred loading of hibernate associations