zoukankan      html  css  js  c++  java
  • java_hibernate

    1. 入门http://jingyan.baidu.com/article/cbf0e500965a352eab289368.html
    2. 步骤
      1、查看是否hibernate支持:file-->plugins-->hibernate(搜索)
      2、新增web project,勾选web application、hibernate、create default hibernate
      3、点击左下角框框,弹出database,读取sqlserver数据库数据自动生成配置文件
      4、编写测试程序实现插入数据操作
      public class HTest {
          public static void main(String[] args) {
              TableName log = new TableName();
      //        log.setId(10001);
              log.setAid(1);
              log.setRelatedId("111");
      
              try {
                  String remark="111的备注";
                  String ret = new String(remark.getBytes("ISO-8859-1"),"GB2312");
                  log.setRemark(ret);
              } catch (UnsupportedEncodingException e) {
                  e.printStackTrace();
              }
      
              DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
              Date date = new Date();
              String dateStr = sdf.format(date);
              log.setCreated(Timestamp.valueOf(dateStr));
      
             SessionFactory fac=  new AnnotationConfiguration().configure().buildSessionFactory();
      
      //        Configuration configuration = new Configuration();
      //        SessionFactory fac = configuration.configure().buildSessionFactory();
      
              Session session =fac.getCurrentSession();
              session.beginTransaction();
              session.save(log);
              session.getTransaction().commit();
          }
    3. 过程中遇到的问题及解决方案
          1、 Could not load requested class : com.microsoft.sqlserver.jdbc.SQLServerDriver
       解决方法:缺少sqlserver驱动导致
        1、下载sqljdbc4.jar 
        2、file-->project structure-->modules-->dependencies-->引入sqljdbc4.jar
     
       2、No CurrentSessionContext configured 
              解决方法:hibernate.cfg.xml配置文件中加上节点:<property name="current_session_context_class">thread</property>
     
        3、当 IDENTITY_INSERT 设置为 OFF 时,不能为表 '' 中的标识列插入显式值
      解决方法:sqlserver中主键为自增长导致,数据库对应的实体类加上:
      @GeneratedValue(strategy = GenerationType.IDENTITY)
     
       4、hibernate写入数据库时出现乱码
              解决方法:暂未找到解决方法





  • 相关阅读:
    lucene初探
    直接插入排序算法(java)
    快速排序优化算法
    大根堆
    学习资料地址
    Lucene:基于Java的全文检索引擎简介
    开关按钮
    微信小程序—如何获取用户输入文本框的值
    微信小程序—获取用户网络状态和设备的信息
    Bootstrap 导航栏
  • 原文地址:https://www.cnblogs.com/gossip/p/4482629.html
Copyright © 2011-2022 走看看