< xml version="1.0" >
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
5、Test.java测试
[java]
package com.itmyhome;
import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.tool.hbm2ddl.SchemaExport;
public class Test {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
//new SchemaExport(new AnnotationConfiguration().configure()).create(true, true);
package com.itmyhome;
import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.tool.hbm2ddl.SchemaExport;
public class Test {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
//new SchemaExport(new AnnotationConfiguration().configure()).create(true, true);[java] view plaincopyprint Teacher t = new Teacher();
t.setName("张三");
Configuration cfg = new AnnotationConfiguration();
SessionFactory factory =cfg.configure().buildSessionFactory();
Session session = factory.openSession();
session.beginTransaction();
session.save(t);
session.getTransaction().commit();
}
}
Teacher t = new Teacher();
t.setName("张三");
Configuration cfg = new AnnotationConfiguration();
SessionFactory factory =cfg.configure().buildSessionFactory();
Session session = factory.openSession();
session.beginTransaction();
session.save(t);
session.getTransaction().commit();
}
}SchemaExport自动生成数据表
运行程序:
[html]
SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
Exception in thread "main" java.lang.NoClassDefFoundError: org/slf4j/impl/StaticLoggerBinder
SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
Exception in thread "main" java.lang.NoClassDefFoundError: org/slf4j/impl/StaticLoggerBinder
此错误是缺少slf4j-nop.x.jar包 添加一个该包(slf4j-nop-1.5.8.jar)
[html]
Exception in thread "main" java.lang.IllegalAccessError: tried to access field org.slf4j.impl.StaticLoggerBinder.SINGLETON from class org.slf4j.LoggerFactory
Exception in thread "main" java.lang.IllegalAccessError: tried to access field org.slf4j.impl.StaticLoggerBinder.SINGLETON from class org.slf4j.LoggerFactory
此错误是slf4j版本错误。slf4j-api是1.5.2而slf4j-nop是1.5.8
删除slf4j-api.1.5.2添加一个1.5.8的
[html]
Exception in thread "main" java.lang.NoClassDefFoundError: org/hibernate/annotations/common/reflection/ReflectionManager
Exception in thread "main" java.lang.NoClassDefFoundError: org/hibernate/annotations/common/reflection/ReflectionManager
此错误是少一个jar包,引入hibernate-commons-annotations.jar
[html]
Caused by: org.xml.