zxlcd 2008-12-30 08:58
浏览 188
已采纳

关于hibernate的joined-subclass继承问题

各位大大:想请教一个hibernate继承问题

有一个User类,类Administrator继承User类,映射表采用joined-subclass继承方式

现在我从数据库里取Administrator数据怎么是NULL值啊

//下面是我取数据的一断代码,查询数据库是成功了,但是所取到类的相关字断是NULL值,想不通,各位指教一下~

try{

   Session s=HibernateUtil.currentSession();

   HibernateUtil.beginTransaction();

   Query query =s.createQuery("from Administrator a where a.username='" + username+ "'");

   Iterator it=query.iterate(); 

   while (it.hasNext()) {

   User u=(Administrator)it.next(); System.out.print(u.getUsername()); 



//User类的映射文件如下,事实上还有个Customer类以同样的方式继承了User类    

<hibernate-mapping>

  <class name="org.easyshop.domain.User">

    <id unsaved-value="null" name="userId" column="userId">

      <generator class="native">

    </id>

    <property name="email" length="64" not-null="true">

    <property name="registerTime" update="false" not-null="true">

    <property name="lastLoginTime" not-null="true">

    <property name="lastModifyTime" not-null="true">

    <property name="loginCount" not-null="true">

    <property name="username" length="32" update="false" not-null="true">

    <property name="password" length="16" not-null="true">

    <joined-subclass name="org.easyshop.domain.Administrator">

      <key column="userId">

      <property name="departement" length="32" not-null="true">

    </joined-subclass>

    <joined-subclass name="org.easyshop.domain.Customer">

      <key column="userId">

      <property name="name" length="32" not-null="true">

      <property name="gender" length="2" not-null="true">

      <property name="phone" length="16">

      <property name="cellphone" length="16" not-null="true">

      <component name="homeAddress">

        <property name="country" length="32" update="false" not-null="true">

        <property name="province" length="32" not-null="true">

        <property name="city" length="32" not-null="true">

        <property name="zipcode" length="8" not-null="true">

        <property name="detailAddress" not-null="true">

      </component>

    </joined-subclass>

  </class>

</hibernate-mapping>



//补充下:重启Tomcat后第一次查询好像没有结果即s.createQuery查询无果,要第二次查询才会输出NULL值,且打印ID是正确的即System.out.print(u.getUserId());会输出正确值。这是怎么回事啊??
问题补充:
楼下所说的缓存很有道理,我弟一次用不是很熟。

今天我试了一下将数据用session保存到MYSQL,然后再执行查询就就完全正确了,(说明:开始我测试的数据是直接手动在数据库里添加的,会不会是有些字断不能为NULL造成的??因为后面我将lastLoginTime等都附上了值后也完全正确。但问题是没有付值数据库默认的也是00:00:00啊)

  • 写回答

2条回答 默认 最新

  • wzkyy 2008-12-30 12:51
    关注

    很明显,缓存呗。

    缓存里的对象缓存,即在所谓的执行查询,得到对象后,并没有真正的执行查询,当你在得到它的属性时,才会去执行查询。

    hibernate3中默认是使用缓存的,hibernate2默认不使用。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料