最近学习hibernate,看书感觉可以,实际操作怎么就那么难呢!
我用的是:hibernate3.2 eclipse3.3 (没有Myeclipse插件) Mysql6.0
一:main()函数
package org.first;
import org.hibernate.*;
import org.hibernate.cfg.*;
public abstract class FirstM {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Configuration conf=new Configuration().configure();
SessionFactory sf=conf.buildSessionFactory();
Session sess=sf.openSession();
Transaction tx=sess.beginTransaction();
FirstBean fb=new FirstBean();
fb.setUserId(12);
fb.setUserName("thmei123");
sess.save(fb);
tx.commit();
sess.close();
}
}
二:持久化类:很简单,一个主键userId,和一个属性userName;
public class FirstBean {
private int userId;
private String userName;
public void setUserId(int userId)
{
this.userId=userId;
}
public int getUserid()
{
return this.userId;
}
public void setUserName(String userName)
{
this.userName=userName;
}
public String getUserName()
{
return this.userName;
}
}
三:影射文件和配置文件:
1:<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
2:
<?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">
org.gjt.mm.mysql.Driver
thmei123
jdbc:mysql://localhost/user
root
org.hibernate.dialect.MySQLDialect
20
1
5000
100
3000
2
true
说明;在主函数main()中运行后提示:
Exception in thread "main" java.lang.NoClassDefFoundError: org/dom4j/DocumentException
at org.first.FirstM.main(FirstM.java:12)
Caused by: java.lang.ClassNotFoundException: org.dom4j.DocumentException
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
提示的是要文件:
log4j-1.2.11.jar 和commons-logging-1.0.4.jar
这两个jar文件我都拷贝到了SRC下面了,也不可以阿
在网上查询说的是好像main()函数中不可以用日志文件,
希望大家帮我了,
另外 请大家给我一个详细的例子吧,最好是web工程的和java工程 各一个例子,
同时天涯沦落人啊!!
[b]问题补充:[/b]
谢谢 楼上兄弟的给你资料和建议,我现在除了拷贝了log4j-1.2.11.jar 和commons-logging-1.0.4.jar 这两个文件外,还在hibernate下拷贝了一个log4j.property的属性文件,都放置到了src下面,但是现在又出下面错误:
22:11:54,599 INFO Environment:514 - Hibernate 3.2.6
22:11:54,619 INFO Environment:547 - hibernate.properties not found
22:11:54,629 INFO Environment:681 - Bytecode provider name : cglib
22:11:54,639 INFO Environment:598 - using JDK 1.4 java.sql.Timestamp handling
22:11:54,850 INFO Configuration:1432 - configuring from resource: /hibernate.cfg.xml
22:11:54,850 INFO Configuration:1409 - Configuration resource: /hibernate.cfg.xml
22:11:55,501 INFO Configuration:559 - Reading mappings from resource : hib.hbm.xml
22:11:56,212 INFO HbmBinder:300 - Mapping class: FirstBean -> student
org.hibernate.InvalidMappingException: Could not parse mapping document from resource hib.hbm.xml
at org.hibernate.cfg.Configuration.addResource(Configuration.java:575)
at org.hibernate.cfg.Configuration.parseMappingElement(Configuration.java:1593)
其中:
一:22:11:54,619 INFO Environment:547 - hibernate.properties not found
提示没有找到hibernate.properties 是怎么会事?我已经放置hibernate.properties,这个文件不是自己写,因为我不会写,就在hibernate下拷贝的一个,请大家给出建议,谢谢
二:org.hibernate.InvalidMappingException: Could not parse mapping document from resource hib.hbm.xml
这个实际上是提示没有找到hib.hbm.xml映射文件,我很是奇怪,无论我怎么修改的映射路径,都是不可以的!请大家帮助!!
学习中,望大家指点 谢谢