首页 > 代码库 > web工程单独使用hibernate(用于测试以及hibernate入门)

web工程单独使用hibernate(用于测试以及hibernate入门)

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入门)