zoukankan      html  css  js  c++  java
  • hibernate 各种坑啊!

    今天用hibernate连数据库遇到了各种奇葩的问题。

    1.connect time out

    这是因为我是用了Middlegen工具 他默认是2.0版本要把左右的 .htm.xml和hibernate.cfg.xml上面版本号改为3.0

    "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >

    明显可以感觉到3.0之后连接速度快了很多

    2.修改hibernate.cfg.xml的”dialect“默认的是org.net啥啥,修改正确的是

    <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
    <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
    <property name="connection.username">root</property>
    <property name="connection.password">passw0rd</property>
    <property name="connection.url">jdbc:mysql://localhost:3306/newsdb?useUnicode=true&amp;characterEncoding=UTF-8</property>
    <property name="show_sql">true</property>

    3.关于XX is not mapped的问题

    首先,检查对应的表的.htm.xml是否把class和包名关联独立。例如我的Newslist的bean 所对应的的配置文件是Newslist.htm.xml里面的配置应该是

    <class
    name="com.hibernate.beans.Newslist"
    table="newslist"
    >

    ps:对于主键要把htm.xml的自增方式改为native: <generator class="native" />

    第二步,检查hibernate.cfg.xml中是否map对了路径,

    <mapping resource="com/hibernate/beans/Newslist.hbm.xml" />

    第三步,检查在applicationContext.xml是否配置了相应的DAO和service

    <!-- 配置DAO -->
    <bean id="newslistDAO"
    class="com.hibernate.dao.NewslistDAOImpl">
    </bean>

    <!-- 配置Service -->
    <bean id="newslistService"
    class="com.hibernate.service.NewslistServiceImpl">
    <property name="newslistDAO">
    <ref local="newslistDAO" />
    </property>
    </bean>

    第四步,一定要记住无论是用hql查询还是Criteria映射的都是类名而不是数据库里的表名

    我的表在数据库里是newslist 所对应的javabean的类名是Newslist

    所以如果用hql就应该是:

    String hql="from Newslist as newslist ORDER BY newslist.id DESC";
    Query query = session.createQuery(hql);

    如果用Criteria应该是:

    Criteria criteria=session.createCriteria(Newslist.class);

    OK!搞定!终于正确查询了!java的配置真是搞死人啊!

  • 相关阅读:
    如何使用ASP.NET2.0的“嵌入的资源”
    多核心计算机运算
    [翻译]注册协议(Register Protocol)
    [翻译]关于“异步可插协议”(About Asynchronous Pluggable Protocols(APPs))
    [ASP.NET]runat="server" causes the problem (< or &lt;)
    [翻译]将应用程序注册为URL协议(Registering an Application to a URL Protocol)
    【C# 转换编码格式】 唉,总是忘记的一个方法。
    在WinForm中借助WebBrowser控件使用 tinymce 总结
    sqlite 资料整理(一)
    sqlite 性能优化
  • 原文地址:https://www.cnblogs.com/doublesong/p/2977944.html
Copyright © 2011-2022 走看看