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

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

2 新建web工程,添加hibernate的lib参考包(将第一步解压的文件里的lib文件夹下required文件夹下的jar包复制到web工程的webroot的web-inf的lib下)

3 文件结构如图web工程单独使用hibernate(用于测试以及hibernate入门)

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 logVersion

INFO: HHH000412: Hibernate Core {4.3.6.Final}

2014-10-21 11:05:31 org.hibernate.cfg.Environment <clinit>

INFO: HHH000206: hibernate.properties not found

2014-10-21 11:05:31 org.hibernate.cfg.Environment buildBytecodeProvider

INFO: HHH000021: Bytecode provider name : javassist

2014-10-21 11:05:31 org.hibernate.cfg.Configuration configure

INFO: HHH000043: Configuring from resource: hibernate.cfg.xml

2014-10-21 11:05:31 org.hibernate.cfg.Configuration getConfigurationInputStream

INFO: HHH000040: Configuration resource: hibernate.cfg.xml

2014-10-21 11:05:31 org.hibernate.cfg.Configuration addResource

INFO: HHH000221: Reading mappings from resource: com/sinosoft/config/Person.hbm.xml

2014-10-21 11:05:31 org.hibernate.cfg.Configuration doConfigure

INFO: HHH000041: Configured SessionFactory: null

2014-10-21 11:05:31 org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure

WARN: HHH000402: Using Hibernate built-in connection pool (not for production use!)

2014-10-21 11:05:31 org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator

INFO: 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 buildCreator

INFO: HHH000046: Connection properties: {user=hjs, password=****}

2014-10-21 11:05:31 org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator

INFO: HHH000006: Autocommit mode: false

2014-10-21 11:05:31 org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure

INFO: 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.OracleDialect

2014-10-21 11:05:32 org.hibernate.dialect.Oracle9Dialect <init>

WARN: HHH000063: The Oracle9Dialect dialect has been deprecated; use either Oracle9iDialect or Oracle10gDialect instead

2014-10-21 11:05:32 org.hibernate.dialect.OracleDialect <init>

WARN: HHH000064: The OracleDialect dialect has been deprecated; use Oracle8iDialect instead

2014-10-21 11:05:32 org.hibernate.engine.jdbc.internal.LobCreatorBuilder useContextualLobCreation

INFO: HHH000424: Disabling contextual LOB creation as createClob() method threw error : java.lang.reflect.InvocationTargetException

2014-10-21 11:05:32 org.hibernate.engine.transaction.internal.TransactionFactoryInitiator initiateService

INFO: 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();语句则会报错。
希望大家能够调试成功。

你可能感兴趣的:(Hibernate)