【问题标题】:Running Hibernate in an Eclipse plugin-based environment在基于 Eclipse 插件的环境中运行 Hibernate
【发布时间】:2013-09-20 21:30:47
【问题描述】:

最近,我一直在尝试使用 Hibernate 作为基于 Eclipse 包的项目的 O-R-Mapper。

由于 Eclipse 包的独特类加载,许多人建议使用 Eclipselink 而不是 Hibernate。

尝试了 Eclipselink 并不太满意,我很想知道:

有没有办法让 Hibernate 在我的 Eclipse 插件项目 中启动并运行?

【问题讨论】:

    标签: eclipse hibernate jpa plugins persistence


    【解决方案1】:

    这里是我如何让它工作的一个小演练。请随时提出问题并发布有关如何改进此问题的建议:

    下载带有 OSGi 支持的 Hibernate 4.2.5 或更高版本(请参阅Hibernate OSGi Documentation)。但是,那里的示例使用 Apache Felix 作为 OSGi 实现,而不是 equinox。

    从现有的 jar-archives 创建一个新的插件项目

    就我而言,我添加了以下罐子:

    • hibernate-core-4.2.5
    • hibernate-osgi-4.2.5
    • hibernate-commons-annotations-4.0.2(我正在使用注释)
    • hibernate-jpa-2.0(我正在使用 java persistence api 以获得更大的灵活性)
    • hibernate-entitymanager-4.2.5(也是更通用的 jpa entitymanager,而不是休眠会话)
    • org.osgi.core-4.3.1(用于 osgi 类)
    • jboss 日志记录
    • jboss-transaction-api
    • dom4j-1.6.1
    • antlr-2.7.7

    打开项目的MANIFEST.MF并添加以下内容:

    Bundle-Activator: org.hibernate.osgi.HibernateBundleActivator(这是来自 hibernate-osgi 包的 hibernate 包激活器)

    Bundle-ActivationPolicy: lazy(以便 osgi 在激活后将上下文传递给包)

    Eclipse-BuddyPolicy: registered(我们稍后需要它来使我们的实体类知道休眠,反之亦然)

    还要确保您的所有 jar 文件都在 Bundle-Classpath 上,并且插件的所有 都已导出

    现在,为您的休眠配置和 DAO 创建一个新的插件项目。

    将您的持久性配置文件(persistence.xmlhibernate.cfg.xml)放在插件根目录的 META-INF 文件夹中。这是persistence.xml 的示例:

    <persistence xmlns="http://java.sun.com/xml/ns/persistence"
                 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                 xsi:schemaLocation="http://java.sun.com/xml/ns/persistence persistence_2_1.xsd"
                 version="1.0">
        <persistence-unit name="TheNameOfMyPersistenceUnit" transaction-type="RESOURCE_LOCAL">
            <description>My Persistence Unit</description>
            <provider>org.hibernate.ejb.HibernatePersistence</provider>
    
            <class>de.eicher.jonas.SomeClass</class>
            <class>de.eicher.jonas.AnotherClass</class>
    
            <properties>
                <property name="hibernate.dialect" value="org.hibernate.dialect.DerbyDialect"/>
                <property name="hibernate.connection.url" value="jdbc:derby:C:/Temp/data;create=true"/>
                <property name="hibernate.connection.driver_class" value="org.apache.derby.jdbc.EmbeddedDriver"/>
                <property name="hibernate.connection.username" value="sa"/>
                <property name="hibernate.connection.password" value=""/>
                <property name="org.hibernate.FlushMode" value="commit" />
                <property name="hibernate.hbm2ddl.auto" value="update" />
                <property name="hibernate.current_session_context_class" value="thread"/>
                <property name="hibernate.cache.provider_class" value="org.hibernate.cache.NoCacheProvider"/>
                <property name="hibernate.show_sql" value="true"/>
            </properties>
        </persistence-unit>
    
    </persistence>
    

    org.eclipse.core.runtime 添加到您的依赖项中并创建一个Activator 以获得对BundleContext 的静态访问:

    import org.eclipse.core.runtime.Plugin;
    import org.osgi.framework.BundleContext;
    
    public class HibernateJpaActivator extends Plugin {
    
        private static BundleContext context;
    
        @Override
        public void start(BundleContext context)
            throws Exception {
            HibernateJpaActivator.context = context;
        }
    
        public static BundleContext getContext() {
            return context;
        }
    }
    

    在您的 DAO 或 Util 类中,使用以下代码获取 EntityManagerFactoryEntityManager

    BundleContext context =  HibernateJpaActivator.getContext(); 
    ServiceReference serviceReference = context.getServiceReference( PersistenceProvider.class.getName() );
    PersistenceProvider persistenceProvider = (PersistenceProvider) context.getService( serviceReference );
    emf = persistenceProvider.createEntityManagerFactory( "TheNameOfMyPersistenceUnit", null );
    EntityManager em = emf.createEntityManager();
    

    在它起作用之前只需要做几件事:

    打开 MANIFEST.MF 并确保您的捆绑包在激活时收到BundleContext

    Bundle-ActivationPolicy: lazy
    Bundle-Activator: my.package.name.HibernateJpaActivator
    

    打开包含您的实体的插件并使用您的 hibernate jars(我们创建的第一个)向插件添加依赖项。

    现在我们还需要在带有休眠 jar 的插件中知道实体。我们不能在那里添加依赖,因为这会产生循环依赖。幸运的是,Eclipse 为我们提供了一种解决方法:

    打开实体包的 MANIFEST.MF 并将您的 hibernate-jar 插件注册为好友

    Eclipse-RegisterBuddy: org.hibernate4.osgi(你的hibernate插件的名字,你设置Eclipse-Buddy-Policy的那个:注册的)

    现在 Hibernate 知道我们的类,我们的类也知道 Hibernate。我们还确保 Hibernate 找到我们的 persistence.xml(或 hibernate.cfg.xml)并创建我们易于配置的 EntityMangerFactory(或 Session)。

    【讨论】:

    • 对于 5.2.1 版的 Hibernate,必须修改 persistence.xml,将 provider-tag 替换为 &lt;provider&gt;org.hibernate.ejb.HibernatePersistence&lt;/provider&gt;
    猜你喜欢
    • 2021-04-28
    • 1970-01-01
    • 2018-11-19
    • 1970-01-01
    • 2011-03-16
    • 2011-01-11
    • 2011-11-02
    • 2016-01-07
    • 1970-01-01
    相关资源
    最近更新 更多