web工程單獨使用hibernate(用於測試以及hibernate入門)

來源:互聯網
上載者:User

標籤:style   blog   http   color   io   os   ar   使用   java   

1 首先下載hibernate包地址:http://hibernate.org/orm/

2 建立web工程,添加hibernate的lib參考包(將第一步解壓的檔案裡的lib檔案夾下required檔案夾下的jar包複製到web工程的webroot的web-inf的lib下)

3 檔案結構

4 Test類用作測試效果:代碼:

package com.sinosoft;import org.hibernate.Session;import org.hibernate.SessionFactory;import org.hibernate.Transaction;import org.hibernate.cfg.Configuration;public class Test {public static void main(String[] args) {    Configuration cfg=new Configuration();    cfg.configure("hibernate.cfg.xml");    @SuppressWarnings("deprecation")    SessionFactory sf=cfg.buildSessionFactory();    Session session = sf.openSession();    Transaction tx=session.beginTransaction();        Person person = new Person();    person.setName("lxy");    session.save(person);    tx.commit();    session.close();    sf.close();    }}

將Test類以java application運行即可檢測效果。

5 注意事項: *<property name = "dialect">org.hibernate.dialect.OracleDialect</property>這個語句一定要和jar包驅動對應,不然會報hibernate_sequence找不到。

                 *以下報錯可以忽略,仍能達成實驗效果。

2014-10-21 11:05:31 org.hibernate.annotations.common.reflection.java.JavaReflectionManager <clinit>INFO: HCANN000001: Hibernate Commons Annotations {4.0.5.Final}2014-10-21 11:05:31 org.hibernate.Version logVersionINFO: HHH000412: Hibernate Core {4.3.6.Final}2014-10-21 11:05:31 org.hibernate.cfg.Environment <clinit>INFO: HHH000206: hibernate.properties not found2014-10-21 11:05:31 org.hibernate.cfg.Environment buildBytecodeProviderINFO: HHH000021: Bytecode provider name : javassist2014-10-21 11:05:31 org.hibernate.cfg.Configuration configureINFO: HHH000043: Configuring from resource: hibernate.cfg.xml2014-10-21 11:05:31 org.hibernate.cfg.Configuration getConfigurationInputStreamINFO: HHH000040: Configuration resource: hibernate.cfg.xml2014-10-21 11:05:31 org.hibernate.cfg.Configuration addResourceINFO: HHH000221: Reading mappings from resource: com/sinosoft/config/Person.hbm.xml2014-10-21 11:05:31 org.hibernate.cfg.Configuration doConfigureINFO: HHH000041: Configured SessionFactory: null2014-10-21 11:05:31 org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl configureWARN: HHH000402: Using Hibernate built-in connection pool (not for production use!)2014-10-21 11:05:31 org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreatorINFO: HHH000401: using driver [oracle.jdbc.driver.OracleDriver] at URL [jdbc:oracle:thin:@127.0.0.1:1521:orcl]2014-10-21 11:05:31 org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreatorINFO: HHH000046: Connection properties: {user=hjs, password=****}2014-10-21 11:05:31 org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreatorINFO: HHH000006: Autocommit mode: false2014-10-21 11:05:31 org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl configureINFO: HHH000115: Hibernate connection pool size: 20 (min=1)2014-10-21 11:05:31 org.hibernate.dialect.Dialect <init>INFO: HHH000400: Using dialect: org.hibernate.dialect.OracleDialect2014-10-21 11:05:32 org.hibernate.dialect.Oracle9Dialect <init>WARN: HHH000063: The Oracle9Dialect dialect has been deprecated; use either Oracle9iDialect or Oracle10gDialect instead2014-10-21 11:05:32 org.hibernate.dialect.OracleDialect <init>WARN: HHH000064: The OracleDialect dialect has been deprecated; use Oracle8iDialect instead2014-10-21 11:05:32 org.hibernate.engine.jdbc.internal.LobCreatorBuilder useContextualLobCreationINFO: HHH000424: Disabling contextual LOB creation as createClob() method threw error : java.lang.reflect.InvocationTargetException2014-10-21 11:05:32 org.hibernate.engine.transaction.internal.TransactionFactoryInitiator initiateServiceINFO: HHH000399: Using default transaction strategy (direct JDBC transactions)2014-10-21 11:05:32 org.hibernate.hql.internal.ast.ASTQueryTranslatorFactory <init>

  *上述Test類的main方法裡的

 Transaction tx=session.beginTransaction();這句意為已開啟事務 如若添加tx.begin();語句則會報錯。
希望大家能夠調試成功。

web工程單獨使用hibernate(用於測試以及hibernate入門)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.