日期:2014-05-17  浏览次数:20675 次

hibernate突然取不到某个字段的值
PS:在ITeye上面问了两次都没能解决问题,不给力啊。

很简单的一个测试;用Speak s = (Speak)session.get(Speak.class, 1);得到Speak对象之后,s.getAttachmentFlag()竟然取不到值,确切的说是取到的值为"";真要我小命!

相关代码如下:

 

1、数据库中有speak表,speak表中有attachment_flag字段;

2、attachment_flag字段的getter和setter方法hibernate自动生成的,应该没问题;

3、数据库的配置<mapping resource="com/shaiyaya/dao/Speak.hbm.xml" /> 应该也没问题;

4、测试语句:


Java code

Session session = HibernateSessionFactory.getSession();     
Transaction tran = session.beginTransaction();     
     
Speak s = (Speak)session.get(Speak.class, 1);     
System.out.println("at:"+s.getAttachmentFlag());     
tran.commit();     
session.close();     
return "success";   



其它字段的值都能取到, 但就是取不到attachment_flag字段的值;前几天还是好的啊!好像是加了索引什么的就这样了,不知道是不是原因。不过删除了索引speak的所有索引也没解决问题……

 
speak.hbm.xml里面attachmentFlag字段的配置 
XML code

<property name="attachmentFlag" type="java.lang.String">     
         <column name="attachment_flag" length="20"></column>     
</property>  



 其他字段都是能取出值来的,就是这个字段取不出值;并且我用session.update()方法的时候,不管传什么值,这个字段都是变成"";而其它字段都更新了的。

speak表的DDL:
Java code

-- Table "speak" DDL     
     
CREATE TABLE `speak` (     
  `speak_id` int(11) NOT NULL auto_increment,     
  `user_id` int(11) default NULL,     
  `content` mediumtext,     
  `create_time` timestamp NULL default NULL on update CURRENT_TIMESTAMP,     
  `last_comm_time` timestamp NULL default NULL COMMENT '后评论最时间',     
  `cate_flag` varchar(50) default NULL COMMENT 'twitter,style,topic',     
  `x_id` int(11) default NULL COMMENT '现表示的是forum的id',     
  `attachment_flag` varchar(20) default '0' ,     
  `comment_sum` int(11) default '0' COMMENT '应相的speak被评论的次数',     
  `love_sum` int(11) default '0' COMMENT '对应的speak被喜欢的次数',     
  `forward_sum` int(11) default '0' COMMENT '对应的speak被转发的次数',     
  PRIMARY KEY  (`speak_id`),     
  KEY `user_id` (`user_id`),     
  KEY `love_sum` (`love_sum`),     
  KEY `cate_flag` (`cate_flag`),     
  KEY `attachment_flag` (`attachment_flag`),     
  CONSTRAINT `speak_ibfk_1` FOREIGN KEY (`user_id`) REFERENCES `user` (`user_id`) ON DELETE CASCADE ON UPDATE CASCADE     
) ENGINE=InnoDB DEFAULT CHARSET=gbk;  



speak.java
Java code

package com.shaiyaya.dao;     
     
import java.util.Date;     
/**   
 * Speak entity.   
 *    
 * @author MyEclipse Persistence Tools   
 */     
     
public class Speak implements java.io.Serializable {     
     
    // Fields     
     
    private Integer speakId;     
    private User user;     
    private String content;     
    private Date createTime;     
    private Date lastCommTime;     
    private String cateFlag;     
    private Integer XId;     
    private String attachmentFlag;     
    private Integer commentSum;     
    private Integer loveSum;     
    private Integer forwardSum;     
     
    // Constructors     
     
    /** default constructor */     
    public Speak() {     
    }     
     
    /** full constructor */     
    public Speak(User user, String content, Date createTime, Date lastCommTime,     
            String cateFlag, Integer XId, String attachmentFlag,     
            Integer commentSum, Integer loveSum, Integer forward