java.lang.AbstractMethodError当buildind会话工厂时

时间:2015-08-23 10:00:53

标签: hibernate session sessionfactory

我尝试构建会话工厂对象时遇到异常。我的hibernate.cfg.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
  <session-factory>
    <property name="hibernate.connection.driver_class">com.microsoft.sqlserver.jdbc.SQLServerDriver</property>
    <property name="hibernate.connection.url">jdbc:sqlserver://localhost:1433;databaseName=lotusinvest_db</property>
    <property name="hibernate.connection.username">sa</property>
    <property name="hibernate.connection.password">123456</property>
    <property name="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</property>
    <property name="hibernate.show_sql">true</property>
    <property name="hibernate.current_session_context_class">thread</property>
  </session-factory>
</hibernate-configuration>

HibernateConfiguration类:

public class HibernateConfiguration {

    private static final SessionFactory sessionFactory = buildSessionFactory();

    private static SessionFactory buildSessionFactory() {
        SessionFactory SessionFactory = null;
        try {
            Configuration configuration = new Configuration();
            configuration.configure();
            StandardServiceRegistryBuilder ssrb = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties());
            StandardServiceRegistry registry=ssrb.build();
            SessionFactory = configuration.buildSessionFactory(registry);
        } catch (Throwable ex) {
            System.err.println("Failed to create sessionFactory object." + ex);
            ex.printStackTrace();
        }
        return SessionFactory;
    }

    public static SessionFactory getSessionFactory() {
        return sessionFactory;
    }

    public static Session openSession() {
        return sessionFactory.openSession();
    }

    public static void shutdown() {
        getSessionFactory().close();
    }

    public static void main(String[] args) {
    }
}

当我尝试运行该程序时,我在行java.lang.AbstractMethodError处收到异常SessionFactory = configuration.buildSessionFactory(registry)。我该如何解决?

1 个答案:

答案 0 :(得分:0)

您的班级和您的实例变量是相同的。 我重命名了实例

private static SessionFactory buildSessionFactory() {
    SessionFactory sessionFactoryInstance = null;
    try {
        Configuration configuration = new Configuration();
        configuration.configure();
        StandardServiceRegistryBuilder ssrb = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties());
        StandardServiceRegistry registry=ssrb.build();
        sessionFactoryInstance = configuration.buildSessionFactory(registry);
    } catch (Throwable ex) {
        System.err.println("Failed to create sessionFactory object." + ex);
        ex.printStackTrace();
    }
    return sessionFactoryInstance;
}