每日一得

不求多得,只求一得 about java,hibernate,spring,design,database,Ror,ruby,快速开发
最近关心的内容:SSH,seam,flex,敏捷,TDD
本站的官方站点是:颠覆软件

  BlogJava :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理 ::
  220 随笔 :: 9 文章 :: 421 评论 :: 0 Trackbacks
keyword:MySql字段,Hibernate session

一.MySql字段敏感

这几天怪问题真是不少,这不刚建的一个数据库的表用MiddleGen批量生成hbm.xml文件居然和数据库的数据类型不一致.
MySql建表语句如下:
drop table if exists book;

/*==============================================================*/
/* Table: book                                                  */
/*==============================================================*/
create table book
(
   id                             
int                            not null,
   name                           
varchar(100),
   author                         
varchar(100),
   date                           date,
   price                          
int,
   
primary key (id)
)
comment
="Book table"
type 
= InnoDB;

生成的hbm.xml文件如下:

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
    "-//Hibernate/Hibernate Mapping DTD 2.0//EN"
    "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd" 
>
    
<hibernate-mapping>
<!-- 
    Created by the Middlegen Hibernate plugin 2.1

    http://boss.bekk.no/boss/middlegen/
    http://www.hibernate.org/
-->

<class 
    
name="net.foxlog.prj.Book" 
    table
="book"
>
    
<meta attribute="class-description" inherit="false">
       @hibernate.class
        table="book"
    
</meta>

    
<id
        
name="id"
        type
="java.lang.Long"
        column
="id"
    
>
        
<meta attribute="field-description">
           @hibernate.id
            generator-class="assigned"
            type="java.lang.Long"
            column="id"


        
</meta>
        
<generator class="assigned" />
    
</id>

    
<property
        
name="name"
        type
="java.lang.String"
        column
="name"
        length
="100"
    
>
        
<meta attribute="field-description">
           @hibernate.property
            column="name"
            length="100"
        
</meta>    
    
</property>
    
<property
        
name="author"
        type
="java.lang.String"
        column
="author"
        length
="100"
    
>
        
<meta attribute="field-description">
           @hibernate.property
            column="author"
            length="100"
        
</meta>    
    
</property>
    
<property
        
name="date"
        type
="java.sql.Date"
        column
="date"
        length
="10"
    
>
        
<meta attribute="field-description">
           @hibernate.property
            column="date"
            length="10"
        
</meta>    
    
</property>
    
<property
        
name="price"
        type
="java.lang.String"
        column
="price"
        length
="10"
    
>
        
<meta attribute="field-description">
           @hibernate.property
            column="price"
            length="10"
        
</meta>    
    
</property>

    
<!-- Associations -->
  

</class>
</hibernate-mapping>

注意到没有,id的类型变成了Long型了,而price居然变成了String了,晕啊.

各位碰到过这个问题么?

最后还是找到问题所在了,实际上是建表有问题,问题就出在字段的名称上,把id改为ID,price改为PRICE就没问题了!  MySql对id和price敏感? 不得而知,目前看来好像是这样.只是提醒我以后建表养成一个习惯,都用大写的就没问题了.
没想到一次测试中随便建的一个表发现了这么个有趣的事情  :)

二.Hibernate的session关闭问题
用hibernate的工具类获得session有没有碰到过session is closed的错误提示? 我又幸运的碰到这个问题了,呵呵,我怎么有那么多问题啊,晕了,我看来是问题先生了,我的一个同事上次也跟我说过这个事情,后来他没有正面解决这个,绕过去了,他用Spring去替自己解决了,呵呵,也够狠的.不过问题实际上是获得session的时候需要增加一个判断. 即 session.isOpen() == false;
 
DBUtil.java代码如下:
/**
     * 返回一个可用的数据库Session连接
     * 
@return Hibernate中对数据库的Session连接
     * 
@throws HibernateException
     
*/
    
public static Session currentSession() throws HibernateException
    {
        Session s 
= (Session) session.get();
        
// Open a new Session, if this Thread has none yet
        if (null==|| s.isOpen()==false)//注意这里
        {
            s 
= sessionFactory.openSession();
            session.set(s);
        }
        
return s;
    }


posted on 2006-02-24 00:25 Alex 阅读(1365) 评论(0)  编辑  收藏 所属分类: java

只有注册用户登录后才能发表评论。


网站导航: