实体Customer类:
package wei.peng;
import java.io.Serializable;
public class Customer implements Serializable{
private static final long serialVersionUID = 4054639727225043549L;
private int id;
private String name;
private int age;
private String job;
public Customer() {
super();
}
public Customer(int id, String name, int age, String job) {
super();
this.id = id;
this.name = name;
this.age = age;
this.job = job;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getJob() {
return job;
}
public void setJob(String job) {
this.job = job;
}
}
Customer的Hibernate配置文件(和Customer类在一个目录下):
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <hibernate-mapping package="wei.peng"> <class name="Customer" table="customer"> <meta attribute="sync–DAO">false</meta> <id name="id" type="integer" column="id"> <generator class="increment"></generator> </id> <property name="name" column="name" type="string" not-null="true" length="50"> </property> <property name="age" column="age" type="integer"> </property> <property name="job" column="job" type="string" not-null="true" length="50"> </property> </class> </hibernate-mapping>
Hibernate配置文件:
<?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> <property name="hibernate.connection.url"> jdbc:mysql://192.168.235.26:3306/mysql </property> <property name="hibernate.connection.driver_class"> com.mysql.jdbc.Driver </property> <property name="hibernate.connection.username">root</property> <property name="hibernate.connection.password">mysql</property> <property name="hibernate.connection.pool_size">10</property> <property name="dialect"> org.hibernate.dialect.MySQLDialect </property> <property name="hibernate.show_sql">true</property> <property name="hibernate.hbm2ddl.auto">update</property> <property name="hibernate.transaction.factory_class"> org.hibernate.transaction.JDBCTransactionFactory </property> <mapping resource="wei/peng/Customer.hbm.xml" /> </session-factory> </hibernate-configuration>
测试类CustomerTest类:
package wei.peng.test;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import wei.peng.Customer;
public class CustomerTest {
public static void main(String[] args) {
try {
SessionFactory sf = new Configuration().configure().buildSessionFactory();
Session session = sf.openSession();
Transaction tx = session.beginTransaction();
Customer cs = new Customer();
cs.setName("wei.peng");
cs.setAge(28);
cs.setJob("Developer");
session.save(cs);
tx.commit();
session.close();
} catch (HibernateException e) {
e.printStackTrace();
}
}
}
Exception总结:
log4j:WARN No appenders could be found for logger (org.hibernate.cfg.Environment). log4j:WARN Please initialize the log4j system properly. org.hibernate.HibernateException: Could not parse configuration: /hibernate.cfg.xml at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1494) at org.hibernate.cfg.Configuration.configure(Configuration.java:1428) at org.hibernate.cfg.Configuration.configure(Configuration.java:1414) at wei.peng.test.CustomerTest.main(CustomerTest.java:19) Caused by: org.dom4j.DocumentException: Error on line 2 of document : The document type declaration for root element type "hibernate" must end with '>'. Nested exception: The document type declaration for root element type "hibernate" must end with '>'. at org.dom4j.io.SAXReader.read(SAXReader.java:482) at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1484) ... 3 more
Fuck ***,
拷贝网上的实例,有的家伙,也不知道有没有在直接的机器上跑过
NND,问题就是一些符号Dom4j不能解析,譬如:- > <
看上去,都是一样的,没有问题。
解决的办法:
1、将XML文件在IE浏览器中打开,如果有错误,IE会提示的(相当给力啊)
2、输入法切换到英文输入状态,IE点到哪里,你就删除IE点到的,再输入一次,就OK了
【我不知道,你们的问题是不是这个,反正我是这么搞定的,希望对你们有帮助】
Exception in thread "main" java.lang.NoClassDefFoundError: javax/persistence/EntityListeners at org.hibernate.cfg.annotations.reflection.JPAMetadataProvider.getDefaults(JPAMetadataProvider.java:96) at org.hibernate.annotations.common.reflection.java.JavaReflectionManager.getDefaults(JavaReflectionManager.java:226) at org.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:1355) at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1826) at wei.peng.test.CustomerTest.main(CustomerTest.java:19) Caused by: java.lang.ClassNotFoundException: javax.persistence.EntityListeners 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:303) at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301) at java.lang.ClassLoader.loadClass(ClassLoader.java:248) at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:316) ... 5 more
原因:
没有引入hibernate-distribution-3.6.0.Final\lib\jpa下面的hibernate-jpa-2.0-api-1.0.0.Final.jar