<!--Employee.hbm.xml--> <?xml version="1.0" encoding="utf-8"?> <!DOCTYPE hibernate-mapping SYSTEM "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd" > <hibernate-mapping package="com.hsp.domain"> <class name="Employee" table="employee"> <!-- id元素用于指定主键属性 --> <id name="id" column="id" type="java.lang.Integer"> <!-- 该元素用于指定主键值生成策略hilo native increment sequence uuid --> <generator class="sequence"> <param name="sequence">emp_seq</param> </generator> </id> <!-- 对其它属性还有配置 --> <property name="name" type="java.lang.String"> <column name="name" not-null="false" /> </property> <property name="email" type="java.lang.String" > <column name="email" not-null="false"/> </property> <property name="hiredate" type="java.util.Date"> <column name="hiredate" not-null="false" /> </property> </class> </hibernate-mapping> //Employee.java package com.lh.domain; public class Employee { private Integer id; private String name; private String email; private java.util.Date hiredate; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } public java.util.Date getHiredate() { return hiredate; } public void setHiredate(java.util.Date hiredate) { this.hiredate = hiredate; } } <!--hibernate.cfg.xml--> <?xml version="1.0" encoding="utf-8"?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory> <!-- hibernate 设计者,给我们提供了一写常用的配置 --> <!-- 配置使用的driver --> <property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property> <property name="connection.username">scott</property> <property name="connection.password">Tiger222</property> <property name="connection.url">jdbc:oracle:thin:@127.0.0.1:1521:orcllh</property> <!-- 配置dialect方言,明确告诉hibernate连接是哪种数据库 --> <property name="dialect">org.hibernate.dialect.OracleDialect</property> <!-- 显示出对于sql --> <property name="show_sql">true</property> <!-- 指定管理的对象映射文件 --> <mapping resource="com/lh/domain/Employee.hbm.xml"/> </session-factory> </hibernate-configuration> package com.lh.view; import java.util.Date; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.Transaction; import org.hibernate.cfg.Configuration; import org.hibernate.service.ServiceRegistryBuilder; import com.lh.domain.Employee; public class TestMain { /** * @param args */ public static void main(String[] args) { //查询[load]->hql语句(hibernate query language) //我们使用hibernate完成crud操作[这里我们只见对象,不见表] //现在我们不是用service ,直接测试. //1。创建Configuration,该对象用于读取hibernate.cfg.xml,并完成初始化 Configuration configuration=new Configuration().configure(); //2.创建SessoinFactory[这是一个会话工厂,是一个重量级的对象] ServiceRegistryBuilder srb = new ServiceRegistryBuilder(); SessionFactory sessionFactory= configuration.buildSessionFactory(srb.buildServiceRegistry()); //SessionFactory sessionFactory=configuration.buildSessionFactory(); //3.创建Sessoin 相当于jdbc Connection[ servelt HttpSession ,也不是 jsp session] Session session=sessionFactory.openSession(); //4.对hiberate而言,要求程序员,在进行 增加,删除,修改的时候使用事务提交, Transaction transaction = session.beginTransaction(); //添加一个雇员 Employee employee=new Employee(); employee.setName("shunping"); employee.setEmail("shunping@sohu.com"); employee.setHiredate(new Date()); //insert ............. //保存 session.save(employee);//save employee就是持久化该对象 (把对象保存到了数据库中称为一条记录) //==>insert into ....[被hiberante封装] //提交 transaction.commit(); session.close(); } } 配置的hibernate版本是hibernate-release-4.2.4.Final.原本buildSessionFactory()方法被废弃后用新方法依然无法通过…报错如下 八月 17, 2013 8:53:43 下午 org.hibernate.annotations.common.Version <clinit> INFO: HCANN000001: Hibernate Commons Annotations {4.0.2.Final} 八月 17, 2013 8:53:43 下午 org.hibernate.Version logVersion INFO: HHH000412: Hibernate Core {4.2.4.Final} 八月 17, 2013 8:53:43 下午 org.hibernate.cfg.Environment <clinit> INFO: HHH000205: Loaded properties from resource hibernate.properties: {hibernate.connection.driver_class=org.h2.Driver, hibernate.dialect=org.hibernate.dialect.H2Dialect, hibernate.max_fetch_depth=5, hibernate.format_sql=true, hibernate.generate_statistics=true, hibernate.connection.username=sa, hibernate.connection.url=jdbc:h2:mem:db1;DB_CLOSE_DELAY=-1;MVCC=TRUE, hibernate.bytecode.use_reflection_optimizer=false, hibernate.jdbc.batch_versioned_data=true, hibernate.connection.pool_size=5} 八月 17, 2013 8:53:43 下午 org.hibernate.cfg.Environment buildBytecodeProvider INFO: HHH000021: Bytecode provider name : javassist 八月 17, 2013 8:53:43 下午 org.hibernate.cfg.Configuration configure INFO: HHH000043: Configuring from resource: /hibernate.cfg.xml 八月 17, 2013 8:53:43 下午 org.hibernate.cfg.Configuration getConfigurationInputStream INFO: HHH000040: Configuration resource: /hibernate.cfg.xml 八月 17, 2013 8:53:43 下午 org.hibernate.internal.util.xml.DTDEntityResolver resolveEntity WARN: HHH000223: Recognized obsolete hibernate namespace http://hibernate.sourceforge.net/. Use namespace http://www.hibernate.org/dtd/ instead. Refer to Hibernate 3.6 Migration Guide! 八月 17, 2013 8:53:43 下午 org.hibernate.cfg.Configuration addResource INFO: HHH000221: Reading mappings from resource: com/lh/domain/Employee.hbm.xml 八月 17, 2013 8:53:43 下午 org.hibernate.cfg.Configuration doConfigure INFO: HHH000041: Configured SessionFactory: null 八月 17, 2013 8:53:43 下午 org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure INFO: HHH000402: Using Hibernate built-in connection pool (not for production use!) Exception in thread "main" org.hibernate.service.classloading.spi.ClassLoadingException: Specified JDBC Driver org.h2.Driver could not be loaded at org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl.configure(DriverManagerConnectionProviderImpl.java:111) at org.hibernate.service.internal.StandardServiceRegistryImpl.configureService(StandardServiceRegistryImpl.java:75) at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:159) at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:131) at org.hibernate.engine.jdbc.internal.JdbcServicesImpl.buildJdbcConnectionAccess(JdbcServicesImpl.java:223) at org.hibernate.engine.jdbc.internal.JdbcServicesImpl.configure(JdbcServicesImpl.java:89) at org.hibernate.service.internal.StandardServiceRegistryImpl.configureService(StandardServiceRegistryImpl.java:75) at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:159) at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:131) at org.hibernate.cfg.Configuration.buildTypeRegistrations(Configuration.java:1818) at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1776) at com.lh.view.TestMain.main(TestMain.java:26) Caused by: org.hibernate.service.classloading.spi.ClassLoadingException: Unable to load class [org.h2.Driver] at org.hibernate.service.classloading.internal.ClassLoaderServiceImpl.classForName(ClassLoaderServiceImpl.java:149) at org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl.configure(DriverManagerConnectionProviderImpl.java:106) ... 11 more Caused by: java.lang.ClassNotFoundException: Could not load requested class : org.h2.Driver at org.hibernate.service.classloading.internal.ClassLoaderServiceImpl$AggregatedClassLoader.findClass(ClassLoaderServiceImpl.java:296) at java.lang.ClassLoader.loadClass(ClassLoader.java:424) at java.lang.ClassLoader.loadClass(ClassLoader.java:357) at java.lang.Class.forName0(Native Method) at java.lang.Class.forName(Class.java:270) at org.hibernate.service.classloading.internal.ClassLoaderServiceImpl.classForName(ClassLoaderServiceImpl.java:146) 菜鸟求解答,看的是韩顺平2011的hibernate视频,视频中用的版本是3,jar包有很多不一样的,就把目前4里有的jar包全都引入项目~~不知做法是否正确,顺便问一下hibernate普遍用的版本是多少~谢谢各位大神~~答案正确绝对按时结贴~ |
|
50分 |
目测Oracle的数据库jar包没有加进去
|
64位机器引入了ojdbc6.jar依然报错,请问一下org.h2.driver是什么包??还是我用的hibernate的版本不对的原因? |
|
50分 |
srb.applySettings(configuration.getProperties()).buildServiceRegistry();
|
谢谢两位,已经解决啦~纯散分给二位~ |
|
多谢楼主了
|
|
楼主,我也遇到了类似的问题,你是怎么解决的啊?
|