|
锁定老贴子 主题:hibernate入门篇之新增功能!
该帖已经被评为精华帖
|
|
|---|---|
| 作者 | 正文 |
|
最后更新时间:2004-04-26
版本:hibernate-2.1final。
javaIDE:eclipse 2.1.2 所有文件存放在:com.javamodel.hibernate目录下 Example.java [code:1] 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(); } } } [/code:1] Person.java [code:1] 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; } } [/code:1] Person.hbm.xml [code:1] <?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> [/code:1] hibernate.properties [code:1] ## 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 [/code:1] 我没有对他进行进一步的封装。那样初学者可能不理解。 注意:在工程里要引用hibernate的jar。 声明:JavaEye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
|
|
| 返回顶楼 | |
|
最后更新时间:2003-12-20
謝了
|
|
| 返回顶楼 | |
|
最后更新时间:2003-12-20
InputStream stream = Example.class.getResourceAsStream("hibernate.properties");
这个语句是不是没有必要呢?我可以直接把hibernate.properties这个文件放在.\src目录下啊! |
|
| 返回顶楼 | |
|
最后更新时间:2003-12-22
条条大路通罗马,不是吗?^-^
例子中那样做是不是更灵活呢? |
|
| 返回顶楼 | |
|
最后更新时间:2003-12-25
[quote="lingychen"]InputStream stream = Example.class.getResourceAsStream("hibernate.properties");
quote] 这个函数是所有的配置文件都可以读进来么? 我没有用过 帮忙解答一下 |
|
| 返回顶楼 | |
|
最后更新时间:2003-12-25
相当于io一样,读文件。可以把"hibernate.properties" 启称别的名字如:hibernate_bak.properties等等。
|
|
| 返回顶楼 | |
|
最后更新时间:2003-12-29
这个例子相当简单,不过对不熟悉hibernate的朋友,应该还有一些难度(我本身就笨笨的),所以再向还没配置的朋友(大家应该都行吧),做一些解释。
1. 如果对DataBase不熟悉的朋友,可以在import net.sf.hibernate.cfg.Configuration;后面多import 一行: import net.sf.hibernate.tool.hbm2ddl.SchemaExport; // 这个是用来建立Table的。 ^^" 2. 在第30行,cfg.setProperties(pops); 后 加入 /* 这个是建立table的,第1次执行才需要跑,如果想要加入新的资料,此行必须remark起来(就是在前面加入//),否则将会将本来的table全部移除再建立1次....当然DataBase里头的资料也会全部砍光。 */ new SchemaExport(cfg).create(true, true); 3. hibernate.properties 这个档案在解压缩后的hibernate.jar里头的src里头可以找到,此例子必须把此档案放在和Example.class同一个folder。 4. Person.xml必须放在Person.class同一个folder。 我是在JBuilder里头执行此例子的,如果想要了解如何在JBuilder配置,请参考 十分钟在jb里面运行hibernate的简单例子。我是从这个例子开始进入hibernate的。感谢 getdown 兄。 也很感激 smallduzi 兄写了一系列 hibernate 的讲解。(smallduzi??....small=小,duzi=肚子,小肚子 兄??...开个玩笑...莫见怪... ^^") .................... |
|
| 返回顶楼 | |
|
最后更新时间:2003-12-29
感谢kitleong的补充!谢谢
|
|
| 返回顶楼 | |
|
最后更新时间:2004-01-15
我运行是提示错误:
[code:1]log4j:WARN No appenders could be found for logger (net.sf.hibernate.cfg.Environment). log4j:WARN Please initialize the log4j system properly. net.sf.hibernate.HibernateException: The dialect was not set. Set the property hibernate.dialect. at net.sf.hibernate.dialect.Dialect.getDialect(Dialect.java:337) at net.sf.hibernate.dialect.Dialect.getDialect(Dialect.java:358) at net.sf.hibernate.tool.hbm2ddl.SchemaExport.<init>(SchemaExport.java:57) at net.sf.hibernate.tool.hbm2ddl.SchemaExport.<init>(SchemaExport.java:48) at com.javamodel.hibernate.Example.<clinit>(Example.java:31) java.lang.NullPointerException at com.javamodel.hibernate.Example.main(Example.java:47) Exception in thread "main" [/code:1] 应该怎样设置log4j; 另外配置hibernate.properties是是否除了自己需要的数据库,其它数据库设置都删除,都该保留哪些默认设置。 |
|
| 返回顶楼 | |
|
最后更新时间:2004-01-15
log4j 我没有设定,但是可以跑。hibernate.properties中,除了自己要的database之外,其他的可以用#remark起来。要删除也可以。
|
|
| 返回顶楼 | |








