【问题标题】:Error when trying to use hibernate annotations尝试使用休眠注释时出错
【发布时间】:2010-03-03 12:49:35
【问题描述】:

我收到的错误列在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,但没有解决我的问题。

【问题讨论】:

    标签: java hibernate orm log4j


    【解决方案1】:

    java.lang.IllegalAccessError 清楚地表明您正在使用的 Log4J 版本存在不兼容问题(预期不存在的构造函数),这并不奇怪,因为(我在这里并不是要粗鲁)您的整个依赖管理(如屏幕截图所示)似乎是一个BIG混乱:

    • 您有许多未版本化的库,因此无法知道您正在使用什么(在什么版本中)。
    • 您似乎混合了来自不同来源(至少两个)的东西。
    • 您有多个版本的同一个库(例如 cglib)。
    • 您正在混合使用 can be problematic and is strongly discouraged 的不同版本的 slf4j 工件。

    因此,由于您显然没有使用任何依赖项管理解决方案,我的建议是使用 Hibernate 发行版提供的 JAR(将它们放在一个干净的目录中)或至少对齐slf4j 工件(例如,在版本 1.5.10 上,即休眠注释 3.4 使用的版本)并使用 log4j-1.2.14.jar。您当前的解决方法是隐藏真正问题的临时解决方案。

    【讨论】:

      【解决方案2】:

      我解决了这个问题,只使用注释。

      使用了以下罐子:

      附上代码:

      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();
          }
      }
      

      我希望这对某人有所帮助。

      【讨论】:

        猜你喜欢
        • 2017-12-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-07-12
        • 1970-01-01
        • 2015-04-05
        • 1970-01-01
        相关资源
        最近更新 更多