2010-03-03 78 views
1

我收到的錯誤列於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,但並沒有解決我的問題。

回答

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

java.lang.IllegalAccessError清楚地表明您正在使用的Log4J版本存在不兼容性問題(構造函數預期沒有),這並不令人意外,因爲(並且我不是故意在這裏粗魯)整個依賴管理(通過截圖所示)似乎是一個BIG爛攤子:

  • 你有很多未版本控制庫,所以這是不可能知道你正在使用(在什麼版本)的東西。
  • 你似乎混合了各種來源的東西(至少兩個)。
  • 你有幾個版本的相同的庫(例如cglib)。
  • 您正在混合不同版本的slf4j文物,其中can be problematic and is strongly discouraged

所以,既然你明明沒有使用任何依賴關係管理解決方案,我的建議是使用帶有Hibernate發行的JAR文件(把它們放在一個乾淨的目錄),或至少對準SLF4J工件(例如版本1.5.10,即hibernate註釋3.4使用的版本)以及使用log4j-1.2.14.jar。您當前的解決方法是隱藏真正問題的臨時解決方案。