我的同学说用注解的话AnnotationConfiguration().buildSessionFactory()就可以,而不需要AnnotationConfiguration()
.configure().buildSessionFactory()但是我的一个测试小项目用前者报错,用后者就OK。而且我查了文档确实注解用的是前者。什么原因呢?求高手帮忙啊。

解决方案 »

  1.   

    我也是前者,可以的啊 private static String CONFIG_FILE_LOCATION = "/hibernate.cfg.xml";
    private static final ThreadLocal<Session> threadLocal = new ThreadLocal<Session>();
        private static AnnotationConfiguration configuration = new AnnotationConfiguration();    private static org.hibernate.SessionFactory sessionFactory;
        private static String configFile = CONFIG_FILE_LOCATION; static {
         try {
    configuration.configure(configFile);
    sessionFactory = configuration.buildSessionFactory();
    } catch (Exception e) {
    System.err
    .println("%%%% Error Creating SessionFactory %%%%");
    e.printStackTrace();
    }
        }
        private HibernateSessionFactory() {
        }
      

  2.   

    我也懒得去看源文件了,应该是这样的。我用的是hibernate-jpa-2.0的JAR包,而你的估计是hibernate-annotation3.4什么的JAR包。
    configuration.configure(configFile);
    sessionFactory = configuration.buildSessionFactory();

    这是你反编译的结果吧,你再编译hibernate-jpa-2.0这个包看下,我估计它没有前面那一句(我没用过SDK反编译,你试试吧,顺便告诉我下~~谢谢)