现在又一个pojo
class Student{
    int id ;
    String name;
    Birthday birthday;
    
    //geter setter}class Birthday {    Date year;
    Date month;
    Date day;    //geter setter
}表 id    int    10    主键
   name  vachar 10
   year  date   10
   month date   10
   day   date   10这时候mapping文件如何写,属于关联映射吗?

解决方案 »

  1.   

    它们属于多对一的关系,一个学生有一个生日日期,但是一个生日日期可以对应多个学生,两个数据库表应该有主外键关联吧student.hbm.xml:
    <hibernate-mapping package="pojo"> <class name="student" table="student">
    <id name="id" column="id">
      <generator class="native"></generator>
    </id>
    <property name="name" type="java.lang.String"></property>
    <many-to-one name="birthday" column="birthdayId"     
                     class="Brithday">
                    </many-to-one>
    </class>

    </hibernate-mapping>
    Birthday.hbm.xml
    <hibernate-mapping 
    package="pojo"> <class name="Brithday" table="Birthday">
    <id name="birthdayId">
      <generator class="native"></generator>
    </id>
    <property name="year" type="java.sql.Date"></property>
                    <property name="month" type="java.sql.Date"></property>
                    <property name="day" type="java.sql.Date"></property>
    </class>

    </hibernate-mapping>
    然后在hibernate主配置文件中引入即可
      

  2.   


    只有一个student表,Birthday没有表
      

  3.   


    hibernate 是一个vo对应一个表,你写两个vo不就2张, 你想一张表就把字段放在一个vo里面
      

  4.   

    Birthday没有表?那是 组件关系 映射文件这样写<class name="Student" table="t_student">
        <id name="id" column="pid" type="string">
            <generator class="native"/>   
        </id>
        <property name="name" type="string"/>
        <component name="Birthday" class="Birthday"> <!-- class attribute optional -->
            <property name="year"/>
            <property name="month"/>
            <property name="day"/>
        </component>
    </class>
    大概就是这个样子 
      

  5.   

    哦,组件关系,刚刚学hibernate还没有看到组件,只是突然想到这个问题,谢谢大家