1

我想我已经遵循了 Hibernate Search 和 Hibernate for OSGI 的所有示例。我似乎一切正常并连接好了,但是我永远无法获得 SessionFactory,基本上在我的 HibernateUtil 中,

sf = (SessionFactory) context.getService( sr ); 

sf 始终为空。我正在调试和设置,看到 SR 不为空,但 SF 为空并且没有要调试的内容。以下是详细信息,包括我的 features.xml:

<features xmlns="http://karaf.apache.org/xmlns/features/v1.2.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://karaf.apache.org/xmlns/features/v1.2.0"
        name="hdscores">

    <feature name="hibernate-search" version="${versions.hibernate-search-orm}">
        <!-- JTA -->
        <bundle dependency="true" start-level="30">mvn:org.apache.geronimo.specs/geronimo-jta_1.1_spec/1.1.1</bundle>
        <bundle start-level="30">mvn:org.apache.aries.transaction/org.apache.aries.transaction.blueprint/1.0.0</bundle>
        <bundle start-level="30">mvn:org.apache.aries.transaction/org.apache.aries.transaction.manager/1.0.1</bundle>
        <!-- JPA -->
        <bundle start-level="30">mvn:org.hibernate.javax.persistence/hibernate-jpa-2.1-api/1.0.0.Final</bundle>
        <!-- JBoss Logging and Hibernate Commons Annotations -->
        <bundle>mvn:org.jboss.logging/jboss-logging/${versions.jboss-logging}</bundle>
        <bundle>mvn:org.hibernate.common/hibernate-commons-annotations/${versions.hibernate-commons-annotation}</bundle>
        <!--
        Lucene dependencies - Unfortunately, Lucene does not publish artifacts with OSGi metadata, so we are on our own.
        I tried the bundles provided by Apache ServiceMix, but they are broken. They don't contains the ServiceLoader files.
        That is correct from an OSGi point of view, but Lucene falls over without them.
        Instead falling back to Karaf's built-in wrapping functionality which also needs customizing. Plain wrapping
        does not work and additional properties (work in progress there) needs to be specified. In particular the
        export configuration.
        -->
        <bundle>mvn:org.apache.servicemix.bundles/org.apache.servicemix.bundles.jakarta-regexp/1.4_1</bundle>
        <bundle>wrap:mvn:org.apache.lucene/lucene-core/${versions.lucene}$Bundle-SymbolicName=org.hibernate.org.apache.lucene.core&amp;Export-Package=org.apache.lucene;version="${versions.lucene}",org.apache.lucene.analysis;version="${versions.lucene}",org.apache.lucene.analysis.tokenattributes;version="${versions.lucene}",org.apache.lucene.codecs;version="${versions.lucene}",org.apache.lucene.codecs.compressing;version="${versions.lucene}",org.apache.lucene.codecs.lucene3x;version="${versions.lucene}",org.apache.lucene.codecs.lucene40;version="${versions.lucene}",org.apache.lucene.codecs.lucene41;version="${versions.lucene}",org.apache.lucene.codecs.lucene42;version="${versions.lucene}",org.apache.lucene.codecs.lucene45;version="${versions.lucene}",org.apache.lucene.codecs.lucene46;version="${versions.lucene}",org.apache.lucene.codecs.perfield;version="${versions.lucene}",org.apache.lucene.document;version="${versions.lucene}",org.apache.lucene.index;version="${versions.lucene}",org.apache.lucene.search;version="${versions.lucene}",org.apache.lucene.search.payloads;version="${versions.lucene}",org.apache.lucene.search.similarities;version="${versions.lucene}",org.apache.lucene.search.spans;version="${versions.lucene}",org.apache.lucene.store;version="${versions.lucene}",org.apache.lucene.util;version="${versions.lucene}",org.apache.lucene.util.automaton;version="${versions.lucene}",org.apache.lucene.util.fst;version="${versions.lucene}",org.apache.lucene.util.mutable;version="${versions.lucene}",org.apache.lucene.util.packed;version="${versions.lucene}"</bundle>
        <bundle>wrap:mvn:org.apache.lucene/lucene-queryparser/${versions.lucene}$Bundle-SymbolicName=org.hibernate.org.apache.lucene.queryparser</bundle>
        <bundle>wrap:mvn:org.apache.lucene/lucene-queries/${versions.lucene}$Bundle-SymbolicName=org.hibernate.org.apache.lucene.queries&amp;Export-Package=org.apache.lucene.queries;version="${versions.lucene}",org.apache.lucene.queries.function;version="${versions.lucene}",org.apache.lucene.queries.mlt;version="${versions.lucene}"</bundle>
        <bundle>wrap:mvn:org.apache.lucene/lucene-analyzers-common/${versions.lucene}$Bundle-SymbolicName=org.hibernate.org.apache.lucene.analyzers.common&amp;Export-Package=org.apache.lucene.analysis.ar;version="${versions.lucene}",org.apache.lucene.analysis.bg;version="${versions.lucene}",org.apache.lucene.analysis.br;version="${versions.lucene}",org.apache.lucene.analysis.ca;version="${versions.lucene}",org.apache.lucene.analysis.charfilter;version="${versions.lucene}",org.apache.lucene.analysis.cjk;version="${versions.lucene}",org.apache.lucene.analysis.cn;version="${versions.lucene}",org.apache.lucene.analysis.commongrams;version="${versions.lucene}",org.apache.lucene.analysis.compound;version="${versions.lucene}",org.apache.lucene.analysis.core;version="${versions.lucene}",org.apache.lucene.analysis.cz;version="${versions.lucene}",org.apache.lucene.analysis.da;version="${versions.lucene}",org.apache.lucene.analysis.de;version="${versions.lucene}",org.apache.lucene.analysis.el;version="${versions.lucene}",org.apache.lucene.analysis.en;version="${versions.lucene}",org.apache.lucene.analysis.es;version="${versions.lucene}",org.apache.lucene.analysis.eu;version="${versions.lucene}",org.apache.lucene.analysis.fa;version="${versions.lucene}",org.apache.lucene.analysis.fi;version="${versions.lucene}",org.apache.lucene.analysis.fr;version="${versions.lucene}",org.apache.lucene.analysis.ga;version="${versions.lucene}",org.apache.lucene.analysis.gl;version="${versions.lucene}",org.apache.lucene.analysis.hi;version="${versions.lucene}",org.apache.lucene.analysis.hu;version="${versions.lucene}",org.apache.lucene.analysis.hunspell;version="${versions.lucene}",org.apache.lucene.analysis.hy;version="${versions.lucene}",org.apache.lucene.analysis.id;version="${versions.lucene}",org.apache.lucene.analysis.in;version="${versions.lucene}",org.apache.lucene.analysis.it;version="${versions.lucene}",org.apache.lucene.analysis.lv;version="${versions.lucene}",org.apache.lucene.analysis.miscellaneous;version="${versions.lucene}",org.apache.lucene.analysis.ngram;version="${versions.lucene}",org.apache.lucene.analysis.nl;version="${versions.lucene}",org.apache.lucene.analysis.no;version="${versions.lucene}",org.apache.lucene.analysis.path;version="${versions.lucene}",org.apache.lucene.analysis.pattern;version="${versions.lucene}",org.apache.lucene.analysis.payloads;version="${versions.lucene}",org.apache.lucene.analysis.position;version="${versions.lucene}",org.apache.lucene.analysis.pt;version="${versions.lucene}",org.apache.lucene.analysis.query;version="${versions.lucene}",org.apache.lucene.analysis.reverse;version="${versions.lucene}",org.apache.lucene.analysis.ro;version="${versions.lucene}",org.apache.lucene.analysis.ru;version="${versions.lucene}",org.apache.lucene.analysis.shingle;version="${versions.lucene}",org.apache.lucene.analysis.sinks;version="${versions.lucene}",org.apache.lucene.analysis.snowball;version="${versions.lucene}",org.apache.lucene.analysis.standard;version="${versions.lucene}",org.apache.lucene.analysis.sv;version="${versions.lucene}",org.apache.lucene.analysis.synonym;version="${versions.lucene}",org.apache.lucene.analysis.th;version="${versions.lucene}",org.apache.lucene.analysis.tr;version="${versions.lucene}",org.apache.lucene.analysis.util;version="${versions.lucene}",org.apache.lucene.analysis.wikipedia;version="${versions.lucene}"</bundle>
        <bundle>wrap:mvn:org.apache.lucene/lucene-facet/${versions.lucene}$Bundle-SymbolicName=org.hibernate.org.apache.lucene.facet&amp;Export-Package=org.apache.lucene.facet.collections;version="${versions.lucene}"</bundle>
        <!-- Hibernate Search -->
        <bundle>mvn:org.hibernate/hibernate-search-engine/${versions.hibernate-search-orm}</bundle>
        <bundle>mvn:org.hibernate/hibernate-search-orm/${versions.hibernate-search-orm}</bundle>
        <!-- Test DB bundles -->
        <bundle>mvn:commons-collections/commons-collections/3.2.1</bundle>

        <bundle>mvn:commons-pool/commons-pool/1.5.4</bundle>
        <bundle>mvn:commons-dbcp/commons-dbcp/1.4</bundle>
        <bundle>mvn:commons-lang/commons-lang/2.6</bundle>
        <bundle>wrap:mvn:net.sourceforge.serp/serp/1.13.1$Bundle-SymbolicName=org.hibernate.net.sourceforge.serp</bundle>

        <bundle>mvn:mysql/mysql-connector-java/${versions.mysql-connector}</bundle>

        <bundle>mvn:com.h2database/h2/1.3.170</bundle>

        <!-- ORM dependencies -->
        <bundle>mvn:org.apache.servicemix.bundles/org.apache.servicemix.bundles.antlr/2.7.7_5</bundle>
        <bundle>mvn:org.apache.servicemix.bundles/org.apache.servicemix.bundles.dom4j/1.6.1_5</bundle>

        <bundle>mvn:org.jboss/jandex/1.2.0.Final</bundle>

        <bundle>mvn:com.fasterxml/classmate/0.8.0</bundle>
        <bundle>mvn:org.javassist/javassist/3.18.1-GA</bundle>

        <!-- Hibernate ORM -->
        <bundle>mvn:org.hibernate/hibernate-core/${versions.hibernate-orm}</bundle>
        <bundle>mvn:org.hibernate/hibernate-entitymanager/${versions.hibernate-orm}</bundle>
        <!--<bundle>mvn:org.hibernate/hibernate-envers/${versions.hibernate-orm}</bundle> -->
        <bundle>mvn:org.hibernate/hibernate-osgi/${versions.hibernate-orm}</bundle>
    </feature>


    <feature name="hdscores-test" version="${versions.features}">


        <feature>hibernate-search</feature>
        <feature>ipojo</feature>
        <feature>ipojo-command</feature>


        <bundle>wrap:mvn:com.github.davidmoten/geo/${versions.geo}</bundle>

        <bundle>mvn:com.fasterxml.jackson.core/jackson-databind/${versions.jackson}</bundle>
        <bundle>mvn:com.fasterxml.jackson.core/jackson-core/${versions.jackson}</bundle>
        <bundle>mvn:com.fasterxml.jackson.core/jackson-annotations/${versions.jackson}</bundle>

        <bundle>mvn:com.hdscores.bundles/common.rest/${versions.common.rest}</bundle>
        <bundle>mvn:com.hdscores.bundles/inspection.persistence.production/${versions.inspection.persistence.production}</bundle>

        <bundle>mvn:com.hdscores.bundles/commands.test/${versions.commands.test}</bundle>

    </feature>

然后是我的 HibernateUtil 类:

public class HibernateUtil {
    private SessionFactory sf;

    public Session getSession() {
        return getSessionFactory().openSession();
    }

     public SessionFactory getSessionFactory() {
        if ( sf == null ) {
            Bundle thisBundle = FrameworkUtil.getBundle(HibernateUtil.class);
            // Could get this by wiring up OsgiTestBundleActivator as well.
            BundleContext context = thisBundle.getBundleContext();
            ServiceReference sr = context.getServiceReference( SessionFactory.class.getName() );
            sf = (SessionFactory) context.getService( sr );
        }
        return sf;
    }

}

和我的hiberate.cfg.xml:

 <session-factory>

      <property name="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</property>
      <!--<property name="hibernate.connection.datasource">java:comp/env/jdbc/hdscores</property> -->

      <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
      <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/hdscores</property>
      <property name="hibernate.connection.username">----</property>
      <property name="hibernate.connection.password">----</property>



      <property name="hibernate.search.default.directory_provider">filesystem</property>
      <property name="hibernate.search.default.indexBase">/var/hdscores/indexes</property>
      <property name="hibernate.search.default.exclusive_index_use">false</property>
      <property name="hibernate.search.lucene_version">${versions.lucene}</property>

      <!-- <property name="hibernate.search.jmx_enabled">true</property> -->

      <property name="hibernate.query.substitutions">true 1, false 0</property>

      <!-- <property name="show_sql">true</property> -->

      <mapping class="com.hdscores.inspection.persistence.production.InspectionEntity"/>
      <mapping class="com.hdscores.inspection.persistence.production.ViolationEntity"/>
      <mapping class="com.hdscores.inspection.persistence.production.ComplianceEntity"/>
      <mapping class="com.hdscores.inspection.persistence.production.TemperatureEntity"/>
      <mapping class="com.hdscores.inspection.persistence.production.EstablishmentEntity"/>
      <mapping class="com.hdscores.inspection.persistence.production.StagingLogEntity"/>



      <!-- DB schema will be updated if needed -->

<!-- <property name="hbm2ddl.auto">update</property> -->
  </session-factory>
</hibernate-configuration>

最后是当我尝试运行测试命令时的 karaf 日志:

2015-02-17 23:44:38,874 | INFO  | l for user karaf | Version                          | 230 - org.jboss.logging.jboss-logging - 3.1.4.GA | HCANN000001: Hibernate Commons Annotations {4.0.5.Final}
2015-02-17 23:44:38,889 | INFO  | l for user karaf | Version                          | 230 - org.jboss.logging.jboss-logging - 3.1.4.GA | HHH000412: Hibernate Core {4.3.8.Final}
2015-02-17 23:44:38,895 | INFO  | l for user karaf | Environment                      | 230 - org.jboss.logging.jboss-logging - 3.1.4.GA | HHH000206: hibernate.properties not found
2015-02-17 23:44:38,898 | INFO  | l for user karaf | Environment                      | 230 - org.jboss.logging.jboss-logging - 3.1.4.GA | HHH000021: Bytecode provider name : javassist
2015-02-17 23:44:39,932 | INFO  | l for user karaf | Configuration                    | 230 - org.jboss.logging.jboss-logging - 3.1.4.GA | HHH000043: Configuring from resource: /hibernate.cfg.xml
2015-02-17 23:44:39,933 | INFO  | l for user karaf | Configuration                    | 230 - org.jboss.logging.jboss-logging - 3.1.4.GA | HHH000040: Configuration resource: /hibernate.cfg.xml
2015-02-17 23:44:40,063 | INFO  | l for user karaf | Configuration                    | 230 - org.jboss.logging.jboss-logging - 3.1.4.GA | HHH000041: Configured SessionFactory: null
2015-02-17 23:44:40,306 | WARN  | l for user karaf | verManagerConnectionProviderImpl | 230 - org.jboss.logging.jboss-logging - 3.1.4.GA | HHH000402: Using Hibernate built-in connection pool (not for production use!)
2015-02-17 23:44:40,318 | INFO  | l for user karaf | verManagerConnectionProviderImpl | 230 - org.jboss.logging.jboss-logging - 3.1.4.GA | HHH000401: using driver [com.mysql.jdbc.Driver] at URL [jdbc:mysql://localhost:3306/hdscores]
2015-02-17 23:44:40,319 | INFO  | l for user karaf | verManagerConnectionProviderImpl | 230 - org.jboss.logging.jboss-logging - 3.1.4.GA | HHH000046: Connection properties: {user=root, password=****}
2015-02-17 23:44:40,319 | INFO  | l for user karaf | verManagerConnectionProviderImpl | 230 - org.jboss.logging.jboss-logging - 3.1.4.GA | HHH000006: Autocommit mode: false
2015-02-17 23:44:40,322 | INFO  | l for user karaf | verManagerConnectionProviderImpl | 230 - org.jboss.logging.jboss-logging - 3.1.4.GA | HHH000115: Hibernate connection pool size: 20 (min=1)
2015-02-17 23:44:40,806 | INFO  | l for user karaf | Dialect                          | 230 - org.jboss.logging.jboss-logging - 3.1.4.GA | HHH000400: Using dialect: org.hibernate.dialect.MySQL5InnoDBDialect
2015-02-17 23:44:42,127 | INFO  | l for user karaf | TransactionFactoryInitiator      | 230 - org.jboss.logging.jboss-logging - 3.1.4.GA | HHH000399: Using default transaction strategy (direct JDBC transactions)
2015-02-17 23:44:42,137 | INFO  | l for user karaf | ASTQueryTranslatorFactory        | 230 - org.jboss.logging.jboss-logging - 3.1.4.GA | HHH000397: Using ASTQueryTranslatorFactory
2015-02-17 23:44:42,322 | INFO  | l for user karaf | Version                          | 230 - org.jboss.logging.jboss-logging - 3.1.4.GA | HSEARCH000034: Hibernate Search 5.0.1.Final
2015-02-17 23:44:44,448 | WARN  | l for user karaf | SessionFactoryImpl               | 230 - org.jboss.logging.jboss-logging - 3.1.4.GA | HHH000008: JTASessionContext being used with JDBCTransactionFactory; auto-flush will not operate correctly with getCurrentSession()
2015-02-17 23:44:45,081 | INFO  | l for user karaf | verManagerConnectionProviderImpl | 230 - org.jboss.logging.jboss-logging - 3.1.4.GA | HHH000030: Cleaning up connection pool [jdbc:mysql://localhost:3306/hdscores]
2015-02-17 23:44:50,593 | ERROR | l for user karaf | ShellUtil                        | 25 - org.apache.karaf.shell.console - 3.0.3 | Exception caught while executing command
java.lang.NullPointerException
4

1 回答 1

0

有时,当未在清单中导入正确的 Hibernate 包时,会发生这些错误。如果您使用休眠导入至少以下内容:

org.hibernate;version=${hibernate.core.version},
org.hibernate.cfg;version=${hibernate.core.version},
org.hibernate.service;version=${hibernate.core.version},
org.hibernate.proxy;version=${hibernate.core.version},
org.hibernate.mapping;version=${hibernate.core.version},
org.hibernate.annotations;version=${hibernate.core.version},
org.hibernate.envers.*;version=${hibernate.core.version},
org.hibernate.boot.registry;version=${hibernate.core.version},
org.hibernate.internal.util;version=${hibernate.core.version},
org.hibernate.criterion;version=${hibernate.core.version},
org.hibernate.dialect;version=${hibernate.core.version},
org.hibernate.dialect.function;version=${hibernate.core.version},
org.hibernate.exception;version=${hibernate.core.version},
org.hibernate.jdbc;version=${hibernate.core.version},
org.hibernate.property;version=${hibernate.core.version},
org.hibernate.tool.hbm2ddl;version=${hibernate.core.version},
org.hibernate.type;version=${hibernate.core.version},
org.hibernate.osgi;version=${hibernate.core.version},
于 2017-02-13T18:49:51.917 回答