我收到的错误列在here。
这是我的HibernateUtil.java
package com.rosejapan;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;;
public class HibernateUtil
{
private static final SessionFactory sessionFactory;
static
{
try
{
// Create the SessionFactory from hibernate.cfg.xml
sessionFactory = new AnnotationConfiguration().configure().buildSessionFactory();
} catch(Throwable e)
{
System.err.println("Initial sessionFactory creation failed. " + e);
throw new ExceptionInInitializerError(e);
}
}
public static SessionFactory getSessionFactory()
{
return sessionFactory;
}
}
一切看起来都很好......我已经在CLASSPATH中包含了 log4j-boot.jar ,但没有解决我的问题。
答案 0 :(得分:1)
我只使用注释解决了这个问题。
使用了以下罐子:
alt text http://img202.imageshack.us/img202/5838/jars.png
使用代码:
Users.java
package firsthibernateapp;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name = "users")
public class Users implements java.io.Serializable
{
private static final long serialVersionUID = -7960806792183842504L;
@Id
private Integer id;
@Column(name = "name")
private String myName;
public Users()
{
}
public Integer getId()
{
return id;
}
public void setId(Integer id)
{
this.id = id;
}
public String getMyName()
{
return myName;
}
public void setMyName(String myName)
{
this.myName = myName;
}
@Override
public boolean equals(Object obj)
{
if (obj == null)
{
return false;
}
if (getClass() != obj.getClass())
{
return false;
}
final Users other = (Users) obj;
if (this.id != other.id && (this.id == null || !this.id.equals(obj)))
{
return false;
}
return true;
}
@Override
public int hashCode()
{
int hash = 3;
hash = 53 * hash + (this.id != null ? this.id.hashCode() : 0);
return hash;
}
}
Main.java
package firsthibernateapp;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;
public class Main
{
public static void main(String[] args)
{
SessionFactory sessionFactory = new AnnotationConfiguration()
.setProperty("hibernate.dialect","org.hibernate.dialect.MySQLDialect")
.setProperty("hibernate.connection.driver_class","com.mysql.jdbc.Driver")
.setProperty("hibernate.connection.url","jdbc:mysql://localhost:3306/test_hibernate")
.setProperty("hibernate.connection.username", "root")
.setProperty("hibernate.connection.password", "root")
.setProperty("hibernate.show_sql", "true")
.setProperty("hibernate.format_sql", "true")
.setProperty("hibernate.c3p0.acquire_increment", "1")
.setProperty("hibernate.c3p0.idle_test_period", "100")
.setProperty("hibernate.c3p0.max_size", "10")
.setProperty("hibernate.c3p0.max_statements", "0")
.setProperty("hibernate.c3p0.min_size", "5")
.setProperty("hibernate.c3p0.timeout", "100")
.addAnnotatedClass(Users.class)
.buildSessionFactory();
Session session = sessionFactory.openSession();
session.beginTransaction();
// Get the persistent instance of the given entity class with the given identifier
// and prints out its member (myName)
Users user = (Users) session.get(Users.class, 1);
System.out.println("The user is " + user.getMyName() + "\n");
// commit the changes, and close
session.getTransaction().commit();
session.close();
sessionFactory.close();
}
}
我希望这对某人有帮助。
答案 1 :(得分:1)
java.lang.IllegalAccessError
清楚地表明你正在使用的Log4J版本存在不兼容问题(预期不存在的构造函数),这并不奇怪,因为(我不是故意在这里粗鲁)您的整个依赖关系管理(如屏幕截图所示)似乎是一个 BIG 混乱:
因此,由于您显然没有使用任何依赖关系管理解决方案,我的建议是使用Hibernate发行版提供的 (将它们放在干净的目录中)或至少对齐slf4j工件(例如版本1.5.10,即hibernate注释3.4使用的版本)并使用log4j-1.2.14.jar。您当前的解决方法是隐藏真正问题的临时修复。