Hibernate一對多雙向,多對多雙向,排序
阿新 • • 發佈:2018-11-27
<?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <hibernate-mapping package="cn.happy.hb03mapping.onetomanydouble"> <class name="Dept" table="Dept" schema="zyz"><id name="deptno" column="deptno"> <generator class="native"></generator> </id> <property name="deptname" /> <!--植入一個set標籤--> <set name="emps" cascade="save-update"> <!--多的一方表的外來鍵列--><key column="deptno"></key> <one-to-many class="Emp"></one-to-many> </set> </class> </hibernate-mapping>
<?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN""http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <hibernate-mapping package="cn.happy.hb03mapping.onetomanydouble"> <class name="Emp" table="Emp" schema="zyz"> <id name="empno" column="empno"> <generator class="native"></generator> </id> <property name="empname" /> <many-to-one name="dept" column="deptno" class="Dept"></many-to-one> </class> </hibernate-mapping>
@Test public void testOneToManyDouble(){ Configuration cfg=new Configuration().configure(); SessionFactory factory=cfg.buildSessionFactory(); Session session=factory.openSession(); List<Dept> list=session.createQuery("from Dept").list(); for(Dept dept:list){ System.out.println(dept.getDeptname()); System.out.println("====================="); for(Emp emp:dept.getEmps()){ System.out.println(emp.getEmpname()); } } }
排序
<?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <hibernate-mapping package="cn.happy.hb03mapping.onetomanydouble"> <class name="Dept" table="Dept" schema="zyz"> <id name="deptno" column="deptno"> <generator class="native"></generator> </id> <property name="deptname" /> <!--植入一個set標籤--> <set name="emps" cascade="save-update" inverse="true" order-by="empname desc"> <!--多的一方表的外來鍵列--> <key column="deptno"></key> <one-to-many class="Emp"></one-to-many> </set> </class> </hibernate-mapping>
<?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <hibernate-mapping package="cn.happy.hb03mapping.onetomanydouble"> <class name="Emp" table="Emp" schema="zyz"> <id name="empno" column="empno"> <generator class="native"></generator> </id> <property name="empname" /> <many-to-one name="dept" column="deptno" class="Dept"></many-to-one> </class> </hibernate-mapping>
@Test public void orderby(){ Configuration cfg=new Configuration().configure(); SessionFactory factory=cfg.buildSessionFactory(); Session session=factory.openSession(); Dept dept=session.get(Dept.class,10); for (Emp emp:dept.getEmps()){ System.out.println(emp.getEmpname()); } }
多對多雙向
<?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <hibernate-mapping package="cn.happy.hb03mapping.manytomany"> <class name="Employee" table="Employee" schema="zyz"> <id name="empid" column="empno"> <generator class="native"></generator> </id> <property name="ename" /> <!--table指的是中間表--> <set name="projects" table="PROEMP" inverse="true"> <key column="REMPID"></key> <many-to-many column="RPROID" class="Project"></many-to-many> </set> </class> </hibernate-mapping>
<?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <hibernate-mapping package="cn.happy.hb03mapping.manytomany"> <class name="Project" table="Project" schema="zyz"> <id name="proid" column="proid"> <generator class="native"></generator> </id> <property name="proname" /> </class> </hibernate-mapping>
@Test public void testPrintEmp(){ Configuration cfg=new Configuration().configure(); SessionFactory factory=cfg.buildSessionFactory(); Session session=factory.openSession(); Employee employee = session.get(Employee.class, 7369); System.out.println(employee.getEname()); System.out.println("=================="); for (Project project:employee.getProjects()){ System.out.println(project.getProname()); } }
<?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <hibernate-mapping package="cn.happy.hb03mapping.onetomanydouble"> <class name="Dept" table="Dept" schema="zyz"> <id name="deptno" column="deptno"> <generator class="native"></generator> </id> <property name="deptname" /> <!--植入一個set標籤--> <set name="emps" cascade="save-update"> <!--多的一方表的外來鍵列--> <key column="deptno"></key> <one-to-many class="Emp"></one-to-many> </set> </class> </hibernate-mapping>
<?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <hibernate-mapping package="cn.happy.hb03mapping.onetomanydouble"> <class name="Emp" table="Emp" schema="zyz"> <id name="empno" column="empno"> <generator class="native"></generator> </id> <property name="empname" /> <many-to-one name="dept" column="deptno" class="Dept"></many-to-one> </class> </hibernate-mapping>
@Test public void testOneToManyDouble(){ Configuration cfg=new Configuration().configure(); SessionFactory factory=cfg.buildSessionFactory(); Session session=factory.openSession(); List<Dept> list=session.createQuery("from Dept").list(); for(Dept dept:list){ System.out.println(dept.getDeptname()); System.out.println("====================="); for(Emp emp:dept.getEmps()){ System.out.println(emp.getEmpname()); } } }
排序
<?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <hibernate-mapping package="cn.happy.hb03mapping.onetomanydouble"> <class name="Dept" table="Dept" schema="zyz"> <id name="deptno" column="deptno"> <generator class="native"></generator> </id> <property name="deptname" /> <!--植入一個set標籤--> <set name="emps" cascade="save-update" inverse="true" order-by="empname desc"> <!--多的一方表的外來鍵列--> <key column="deptno"></key> <one-to-many class="Emp"></one-to-many> </set> </class> </hibernate-mapping>
<?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <hibernate-mapping package="cn.happy.hb03mapping.onetomanydouble"> <class name="Emp" table="Emp" schema="zyz"> <id name="empno" column="empno"> <generator class="native"></generator> </id> <property name="empname" /> <many-to-one name="dept" column="deptno" class="Dept"></many-to-one> </class> </hibernate-mapping>
@Test public void orderby(){ Configuration cfg=new Configuration().configure(); SessionFactory factory=cfg.buildSessionFactory(); Session session=factory.openSession(); Dept dept=session.get(Dept.class,10); for (Emp emp:dept.getEmps()){ System.out.println(emp.getEmpname()); } }
多對多雙向
<?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <hibernate-mapping package="cn.happy.hb03mapping.manytomany"> <class name="Employee" table="Employee" schema="zyz"> <id name="empid" column="empno"> <generator class="native"></generator> </id> <property name="ename" /> <!--table指的是中間表--> <set name="projects" table="PROEMP" inverse="true"> <key column="REMPID"></key> <many-to-many column="RPROID" class="Project"></many-to-many> </set> </class> </hibernate-mapping>
<?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <hibernate-mapping package="cn.happy.hb03mapping.manytomany"> <class name="Project" table="Project" schema="zyz"> <id name="proid" column="proid"> <generator class="native"></generator> </id> <property name="proname" /> </class> </hibernate-mapping>
@Test public void testPrintEmp(){ Configuration cfg=new Configuration().configure(); SessionFactory factory=cfg.buildSessionFactory(); Session session=factory.openSession(); Employee employee = session.get(Employee.class, 7369); System.out.println(employee.getEname()); System.out.println("=================="); for (Project project:employee.getProjects()){ System.out.println(project.getProname()); } }