<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ThinkChat2.0新版上線,更智能更精彩,支持會話、畫圖、視頻、閱讀、搜索等,送10W Token,即刻開啟你的AI之旅 廣告
                # 用Jersey構建RESTful服務6--Jersey+SQLServer+Hibernate4.3 ## 一、總體說明 本例運行演示了用Jersey構建RESTful服務中,如何同過Hibernate將數據持久化進SQLServer的過程 ## 二、環境 1. 上文的項目RestDemo 2. SQLServer2005 3. jtds數據庫連接驅動:下載地址[最新版本](http://sourceforge.net/projects/jtds/files/latest/download?source=files),替換掉上文項目中的`mysql-connector` ## 三、配置 1. 與上文mysql的配置不同點主要在hibernate.cfg.xml文件; 配置如下: ``` <?xml version='1.0' encoding='utf-8'?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory> <!-- Database connection settings --> <property name="connection.driver_class">net.sourceforge.jtds.jdbc.Driver</property> <property name="connection.url">jdbc:jtds:sqlserver://192.168.1.10:1433;RestDemo</property> <property name="connection.username">sa</property> <property name="connection.password">aA123456</property> <property name="hibernate.default_schema">RestDemo</property> <!-- JDBC connection pool (use the built-in) --> <property name="connection.pool_size">1</property> <!-- SQL dialect --> <property name="dialect">org.hibernate.dialect.SQLServerDialect</property> <!-- Enable Hibernate's automatic session context management --> <property name="current_session_context_class">thread</property> <!-- Disable the second-level cache --> <property name="cache.provider_class">org.hibernate.cache.internal.NoCacheProvider</property> <!-- Echo all executed SQL to stdout --> <property name="show_sql">true</property> <!-- Drop and re-create the database schema on startup --> <property name="hbm2ddl.auto">update</property> <mapping resource="com/waylau/rest/bean/User.hbm.xml"/> </session-factory> </hibernate-configuration> ``` 2. 修改于mysql不兼容的sql語句`com.waylau.rest.dao.impl`中的`UserDaoImpl`: getUserById修改成如下: ``` @Override public User getUserById(String id) { SessionFactory sessionFactory = HibernateUtil.getSessionFactory(); Session s = null; Transaction t = null; User user = null; try{ s = sessionFactory.openSession(); t = s.beginTransaction(); String hql = "from User where userId='"+id+"'"; Query query = s.createQuery(hql); user = (User) query.uniqueResult(); t.commit(); }catch(Exception err){ t.rollback(); err.printStackTrace(); }finally{ s.close(); } return user; } ``` getAllUsers給成如下: ``` @Override public List<User> getAllUsers() { SessionFactory sessionFactory = HibernateUtil.getSessionFactory(); Session s = null; Transaction t = null; List<User> uesrs = null; try{ s = sessionFactory.openSession(); t = s.beginTransaction(); String hql = "select * from [RestDemo].dbo.t_user"; Query query = s.createSQLQuery(hql).addEntity(User.class); //query.setCacheable(true); // 設置緩存 uesrs = query.list(); t.commit(); }catch(Exception err){ t.rollback(); err.printStackTrace(); }finally{ s.close(); } return uesrs; } ``` 或者如下: ``` @Override public List<User> getAllUsers() { SessionFactory sessionFactory = HibernateUtil.getSessionFactory(); Session s = null; Transaction t = null; List<User> uesrs = null; try{ s = sessionFactory.openSession(); t = s.beginTransaction(); String hql = " from User"; Query query = s.createQuery(hql); //query.setCacheable(true); // 設置緩存 uesrs = query.list(); t.commit(); }catch(Exception err){ t.rollback(); err.printStackTrace(); }finally{ s.close(); } return uesrs; } ``` ## 四、問題 ### 可能會出現如下錯誤 ``` ERROR: 指定的架構名稱 "RestDemo" 不存在,或者您沒有使用該名稱的權限。 三月 26, 2014 3:38:43 下午 org.hibernate.tool.hbm2ddl.SchemaUpdate execute INFO: HHH000232: Schema update complete Hibernate: insert into RestDemo.T_USER (userName, age, USERID) values (?, ?, ?) 三月 26, 2014 3:38:43 下午 org.hibernate.engine.jdbc.spi.SqlExceptionHelper logExceptions WARN: SQL Error: 208, SQLState: S0002 三月 26, 2014 3:38:43 下午 org.hibernate.engine.jdbc.spi.SqlExceptionHelper logExceptions ERROR: 對象名 'RestDemo.T_USER' 無效。 ``` ### 解決方案: 將配置文件中的`hibernate.default_schema`值修改為如下即可: ``` <property name="hibernate.default_schema">RestDemo.dbo</property> ``` 或者去掉上面的配置,在“User.hbm.xml”修改如下 ``` <class name="User" table="T_USER" schema="RestDemo.dbo"> ``` **本章源碼**:[https://github.com/waylau/RestDemo/tree/master/jersey-demo6-sqlserver-hibernate](https://github.com/waylau/RestDemo/tree/master/jersey-demo6-sqlserver-hibernate) [https://github.com/waylau/RestDemo/tree/master/jersey-demo6.2-sqlserver-hibernate](https://github.com/waylau/RestDemo/tree/master/jersey-demo6.2-sqlserver-hibernate)
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看