我编写了一个简单的hibernate应用程序,这让我有错误。以下是我的代码。
Contact.java
public class Contact {
private String firstName;
private String lastName;
private String email;
private long id;
public String getEmail() {
return email;
}
public String getFirstName() {
return firstName;
}
public String getLastName() {
return lastName;
}
public void setEmail(String string) {
email = string;
}
public void setFirstName(String string) {
firstName = string;
}
public void setLastName(String string) {
lastName = string;
}
public long getId() {
return id;
}
public void setId(long l) {
id = l;
}
}
FirstExample.java
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
public class FirstExample {
public static void main(String[] args) {
Session session = null;
try{
SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
session =sessionFactory.openSession();
System.out.println("Inserting Record");
Contact contact = new Contact();
contact.setId(3);
contact.setFirstName("Tausif");
contact.setLastName("rahmathullah");
contact.setEmail("t.r@vl.net");
session.save(contact);
System.out.println("updated");
}catch(Exception e){
System.out.println(e.getMessage());
e.printStackTrace();
}finally{
if(session!=null){
session.flush();
session.close();
}
}
}
}
hibernate.cfg.xml中
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost/simpHiber</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">root</property>
<property name="hibernate.connection.pool_size">10</property>
<property name="show_sql">true</property>
<property name="dialect">org.hibernate.dialect.MySQL5Dialect</property>
<property name="hibernate.hbm2ddl.auto">update</property>
<mapping resource="contact.hbm.xml"/>
</session-factory>
</hibernate-configuration>
contact.hbm.xml
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="Contact" table="CONTACT">
<id name="id" type="long" column="ID" >
<generator class="assigned"/>
</id>
<property name="firstName">
<column name="FIRSTNAME" />
</property>
<property name="lastName">
<column name="LASTNAME"/>
</property>
<property name="email">
<column name="EMAIL"/>
</property>
</class>
</hibernate-mapping>
错误
D:\TausifProjects\simpHiber>java FirstExample
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/log4j/Level
at org.slf4j.LoggerFactory.<clinit>(LoggerFactory.java:60)
at org.hibernate.cfg.Configuration.<clinit>(Configuration.java:197)
at FirstExample.main(FirstExample.java:15)
Caused by: java.lang.ClassNotFoundException: org.apache.log4j.Level
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)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
... 3 more
构建路径中的jar文件
antlr-2.7.6.jar
commons-collections-3.1.jar
dom4j-1.6.1.jar
hibernate3.jar
hibernate3.3.1-slf4j-api-1.5.2.jar
hibernate-jpa-2.0-api-1.0.1.Final.jar
javassist-3.4.ga.jar
jcl-over-slf4j-1.5.2.jar
jta-1.1.jar
mysql-connector-java-5.0.0-bin.jar
slf4j-log4j12-1.5.5.jar
slf4j-jdk14-1.5.2-sources.jar
请帮帮我。提前谢谢。
答案 0 :(得分:1)
如果包含slf4j-log4j.jar,则还必须包含log4j jar。 Slf4j是一个日志记录外观,这意味着它为您提供了与多个其他日志记录API的统一接口。
答案 1 :(得分:0)
在类路径中添加log4j.jar或从类路径中删除slf4j-log4j12-1.5.5.jar