Hibernate 맵 2

1.2                 ,    ,    ;    ,    ;            

  public class Department {
	private int deptid;
	private String dname;
	private String location;
	private Set<Employee> employees=new HashSet<Employee>(); //                    " "
  }

  public class Employee {
	private int empid;
	private String ename;
	private Department department;  //                 " "
  }

                    Department  {Employee..}   many  Employee     
Department    many   
<hibernate-mapping 
	package="com.bjsxt.po">
	<class name="Employee" table="t_employee" >
		<id name="empid">
			<generator class="native"></generator>
		</id>
		<property name="ename"></property>
		<many-to-one name="department" class="Department" cascade="all" >
			<column name="dept_id"></column>  <!--                --!>
		</many-to-one>
	</class>
</hibernate-mapping>
 Department       set          set  

<hibernate-mapping package="com.bjsxt.po">
	<class name="Department" table="t_department" >
		<id name="deptid">
			<generator class="native"></generator>
		</id>
		<property name="dname"></property>
		<property name="location"></property>
		<set name="employees" cascade="save-update" inverse="false" >
			<key>
				<column name="dept_id"></column> <!--   employee      --!>			
			</key>
			<one-to-many class="Employee" />
		</set>
	</class>
</hibernate-mapping>
   cascade     inverse       
 cascade         ,                        .          . 
    Hibernate        ,          。                  .
  cascase = all            
            save-update       
            delete      
           all-delete-orpnan            ,                  ,             ,                    ,    
inverse    =false =true               false        ,true       .true   
  ,                    ,          ,             
1.n
   public class Student2 {
	private int id;
	private String name;
	private Set courses=new HashSet();  //     set    ,          
   }

   public class Course {
	private Long id;
	private String name;
	private Set students =new HashSet(); //            
   }

           hibernate        ,              student_course
     student_id,course_id          。           

 <hibernate-mapping package="com.bjsxt.po">
	<class name="Student2" table="t_student2" lazy="true">
		<id name="id">
			<generator class="native"></generator>
		</id>
		<property name="name"/>
                   <!-- set    ,  stu_course      --!>
		<set name="courses" table="stu_course" cascade="save-update">
			<key column="stuId" />		 <!-- key           --!>
			<many-to-many class="Course" column="courseId" />
		</set>
	</class>
 </hibernate-mapping>
        Course.hbm.xml       ,         
<hibernate-mapping package="com.bjsxt.po">
	<class name="Course" table="t_course" lazy="true">
		<id name="id">
			<generator class="native"></generator>
		</id>
		<property name="name"/>
		<set name="students" table="stu_course" cascade="save-update">
			<key column="courseId" />		//t_sourse   
			<many-to-many class="Student2" column="stuId" />
		</set>
	</class>
</hibernate-mapping>

               。            。            。



좋은 웹페이지 즐겨찾기