最近准备学习hibernate。于是就在网络上搜索有关的文章。还好上面的资料还算丰富。于是就照葫芦画瓢了,择抄了一个网友的代码。可是在我的环境中编译却老是出错。让我好是郁闷啊!幸好经过查资料,一步步地走出重围。终于跑了起来。
原始的代码:
java代码: |
package com.javamodel.hibernate;
import java.io.IOException; import java.io.InputStream; import java.util.Properties;
import net.sf.hibernate.HibernateException; import net.sf.hibernate.MappingException; import net.sf.hibernate.Session; import net.sf.hibernate.SessionFactory; import net.sf.hibernate.Transaction; import net.sf.hibernate.cfg.Configuration;
public class Example{ private static SessionFactory _sessions = null; private static Properties pops = new Properties(); static{ try { InputStream stream = Example.class.getResourceAsStream("hibernate.properties"); try { pops.load(stream); } catch (IOException e1) { e1.printStackTrace(); } Configuration cfg = new Configuration(); cfg.addClass(Person.class); cfg.setProperties(pops); _sessions = cfg.buildSessionFactory(); } catch (MappingException e) { e.printStackTrace(); } catch (HibernateException e) { e.printStackTrace(); } } public static void main(String[] args) throws HibernateException { Person person = new Person(); person.setName("smallduzi"); person.setEmail("smallduzi@sohu.com"); Session session = _sessions.openSession(); Transaction tx = null; try{ tx = session.beginTransaction(); session.save(person); tx.commit(); }catch(HibernateException he){ if(tx != null) tx.rollback(); throw he; } finally{ session.close(); } } }
|
Person.java
java代码: |
package com.javamodel.hibernate;
public class Person { private String id = null; private String name = null; private String email = null; public Person(){}
/** * @return */ public String getEmail() { return email; }
/** * @return */ public String getId() { return id; }
/** * @return */ public String getName() { return name; }
/** * @param string */ public void setEmail(String string) { email = string; }
/** * @param string */ public void setId(String string) { id = string; }
/** * @param string */ public void setName(String string) { name = string; }
}
|
Person.hbm.xml
java代码: |
<?xml version="1.0"?> <!DOCTYPE hibernate-mapping SYSTEM "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd" > <hibernate-mapping> <class name="com.javamodel.hibernate.Person" table="person"> <id name="id"> <column name="id" length="40"/> <generator class="uuid.hex"/> </id> <property name="name" column="name" /> <property name="email" column="email" /> </class> </hibernate-mapping>
|
hibernate.properties
java代码: |
## Oracle
hibernate.dialect net.sf.hibernate.dialect.OracleDialect hibernate.connection.driver_class oracle.jdbc.driver.OracleDriver hibernate.connection.username XXX hibernate.connection.password XXX #hibernate.connection.url jdbc:oracle:thin:@192.168.0.28:1521:orcl hibernate.connection.url jdbc:oracle:oci8:@XXX
|
-------------------------------------------
因为我这里使用的是mysql。我就重新编辑了一下数据库连接的参数配置:
## MySQL
hibernate.dialect net.sf.hibernate.dialect.MySQLDialect
#hibernate.dialect org.hibernate.dialect.MySQLInnoDBDialect
#hibernate.dialect org.hibernate.dialect.MySQLMyISAMDialect
hibernate.connection.driver_class com.mysql.jdbc.Driver
hibernate.connection.url jdbc:mysql://172.16.232.118/hibernate_test
hibernate.connection.username root
hibernate.connection.password pass
但是我刚开始的在eclipse执行的时候居然提示没有net.sf这个包。怎么搞的,我明明是在网络山下载的最新的hibernate3.0.5的jar包啊。前思后想,不得要领。郁闷的要吐血。
后来我在网络上看到他们开发的东西都是使用的hibernate2的包。只得重新下载了。(提示:我发现在3.0的包里面package的名字都已经变了。原来的net.sf.hibernate --> org.hibernate。好混啊,不知道怎么会有这么大的变化,可是害苦了,我们这些初学的人)。
最后编译,却出现了一个又一个的问题。
最初出现的问题是提示:Person.hbm.xml文件找不到,狂晕啊。我不是已经把他放在class文件在一起的吗?有折磨了好久得时间。我用style studio xml工具,仔细检查了这个文件,好好的。显示也完全正常。可还是提示找不到。吗的。无疑之间,我用editorplus打开这个文件,居然发现里面有乱码存在。考,去掉那些非法的字符。终于不在提示这个错误了。但是新的问题又出现了。
问题:
Exception in thread "main" net.sf.hibernate.HibernateException: could not instantiate CacheProvider: org.hibernate.cache.HashtableCacheProvider
at net.sf.hibernate.cfg.SettingsFactory.buildSettings(SettingsFactory.java:134)
at net.sf.hibernate.cfg.Configuration.buildSettings(Configuration.java:1155)
at net.sf.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:789)
at testhibernate.Client2.main(Client2.java:24)
Caused by: java.lang.ClassNotFoundException: org.hibernate.cache.HashtableCacheProvider
at java.net.URLClassLoader$1.run(URLClassLoader.java:200)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:268)
at java.lang.ClassLoader.loadClass(ClassLoader.java:251)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:319)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:164)
at net.sf.hibernate.util.ReflectHelper.classForName(ReflectHelper.java:102)
at net.sf.hibernate.cfg.SettingsFactory.buildSettings(SettingsFactory.java:131)
... 3 more
找到问题的所在了。
修改一下hibernate.properties文件
修改前:
## choose a cache implementation
#hibernate.cache.provider_class org.hibernate.cache.EhCacheProvider
#hibernate.cache.provider_class org.hibernate.cache.EmptyCacheProvider
hibernate.cache.provider_class org.hibernate.cache.HashtableCacheProvider
#hibernate.cache.provider_class org.hibernate.cache.TreeCacheProvider
#hibernate.cache.provider_class org.hibernate.cache.OSCacheProvider
#hibernate.cache.provider_class org.hibernate.cache.SwarmCacheProvider
修改后:
## choose a cache implementation
#hibernate.cache.provider_class org.hibernate.cache.EhCacheProvider
#hibernate.cache.provider_class org.hibernate.cache.EmptyCacheProvider
#hibernate.cache.provider_class org.hibernate.cache.HashtableCacheProvider
#hibernate.cache.provider_class org.hibernate.cache.TreeCacheProvider
#hibernate.cache.provider_class org.hibernate.cache.OSCacheProvider
#hibernate.cache.provider_class org.hibernate.cache.SwarmCacheProvider
小结:
经过一番的周折,总算把第一个例子跑起来了。现在回头想想,感慨颇多啊。
首先要注意的是,在网络上找到例子要看清楚他们运行的环境和要求。这样就不会出现驴唇不对马嘴的情况;
其次,从网络上copy代码的时候要仔细检查代码是否有错误。最好就是用比较专业的ide或编辑器。主要是由于一些网站上对代码的
处理有错误。
Exception in thread "main" net.sf.hibernate.HibernateException: could not instantiate CacheProvider: org.hibernate.cache.HashtableCacheProvider
at net.sf.hibernate.cfg.SettingsFactory.buildSettings(SettingsFactory.java:134)
at net.sf.hibernate.cfg.Configuration.buildSettings(Configuration.java:1155)
at net.sf.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:789)
at testhibernate.Client2.main(Client2.java:24)
Caused by: java.lang.ClassNotFoundException: org.hibernate.cache.HashtableCacheProvider
at java.net.URLClassLoader$1.run(URLClassLoader.java:200)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:268)
at java.lang.ClassLoader.loadClass(ClassLoader.java:251)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:319)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:164)
at net.sf.hibernate.util.ReflectHelper.classForName(ReflectHelper.java:102)
at net.sf.hibernate.cfg.SettingsFactory.buildSettings(SettingsFactory.java:131)
... 3 more
找到问题的所在了。
修改一下hibernate.properties文件
修改前:
## choose a cache implementation
#hibernate.cache.provider_class org.hibernate.cache.EhCacheProvider
#hibernate.cache.provider_class org.hibernate.cache.EmptyCacheProvider
hibernate.cache.provider_class org.hibernate.cache.HashtableCacheProvider
#hibernate.cache.provider_class org.hibernate.cache.TreeCacheProvider
#hibernate.cache.provider_class org.hibernate.cache.OSCacheProvider
#hibernate.cache.provider_class org.hibernate.cache.SwarmCacheProvider
修改后:
## choose a cache implementation
#hibernate.cache.provider_class org.hibernate.cache.EhCacheProvider
#hibernate.cache.provider_class org.hibernate.cache.EmptyCacheProvider
#hibernate.cache.provider_class org.hibernate.cache.HashtableCacheProvider
#hibernate.cache.provider_class org.hibernate.cache.TreeCacheProvider
#hibernate.cache.provider_class org.hibernate.cache.OSCacheProvider
#hibernate.cache.provider_class org.hibernate.cache.SwarmCacheProvider
小结:
经过一番的周折,总算把第一个例子跑起来了。现在回头想想,感慨颇多啊。
首先要注意的是,在网络上找到例子要看清楚他们运行的环境和要求。这样就不会出现驴唇不对马嘴的情况;
其次,从网络上copy代码的时候要仔细检查代码是否有错误。最好就是用比较专业的ide或编辑器。主要是由于一些网站上对代码的
处理有错误。