簡體   English   中英

如何在eclipse中的獨立(Swing)應用程序中配置hibernate?

[英]How to configure hibernate in standalone (Swing) application in eclipse?

我通常在Web應用程序中使用帶有spring的hibernate,所以我使用DI和maven進行配置,現在我想在不使用maven或spring的桌面/ swing應用程序中使用hibernate,我想知道以下內容:

  1. 我需要什么罐子?
  2. 如何配置hibernate,以及如何進行示例查詢?

請指教,謝謝。

我不知道Spring,所以如果你真的想要使用它,你將不得不更新我的答案(例如Spring有自己的機制來初始化實體管理器)。

依賴

這是我用於最近的桌面項目的配置(某些版本可能已經發展), 它使用Hibernate而不是JPA (即它使用EntityManager):

org.hibernate:hibernate:3.2.7.ga
org.hibernate:hibernate-annotations:3.4.0.GA
org.hibernate:hibernate-entitymanager:3.4.0.GA

您可能還需要:

commons-collections:commons-collections:3.2.1
asm:asm:3.2
cglib:cglib:2.2
dom4j:dom4j:1.6.1
antlr:antlr:2.7.7
c3p0:c3p0:0.9.1.2

你可以在maven中心找到它們。

組態

您需要META-INF文件夾中的有效persistence.xml:

<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence" version="2.0">

    <persistence-unit name="NONJTAPersistenceUnit" transaction-type="RESOURCE_LOCAL">

        <class>com.yourpackage.EntityClass1</class>
        <class>com.yourpackage.EntityClass2</class>
        <class>com.yourpackage.EntityClass3</class>

        <properties>
            <property name="hibernate.show_sql" value="true"/>
            <property name="hibernate.format_sql" value="true"/>
            <property name="hibernate.connection.driver_class" value="org.hsqldb.jdbcDriver"/>
            <property name="hibernate.connection.url" value="jdbc:hsqldb:hsql://hostXYZ/yourdatabase"/>
            <property name="hibernate.connection.username" value="sa"/>
            <property name="hibernate.connection.password" value=""/>
            <property name="hibernate.dialect" value="org.hibernate.dialect.HSQLDialect"/>
            <property name="hibernate.hbm2ddl.auto" value="update"/>
        </properties>
    </persistence-unit>
</persistence>

使用您自己的數據庫信息/驅動程序更新它。

用法

在上面的persitence.xml文件中注冊的EntityClass1EntityClass2EntityClass3的常用方法創建實體類。

然后,對於EntityManager ...因為您不在EE環境中,所以必須從EntityManagerFactoty獲取它的實例

EntityManagerFactory emf = Persistence.createEntityManagerFactory("NONJTAPersistenceUnit");
EntityManager em = emf.createEntityManager();

(同樣,Spring可能會提供另一種方法來獲取它,請查看文檔)。

從那里你可以執行,例如持久化操作,這樣:

em.getTransaction().begin();
em.persist(entity);
em.getTransaction().commit();

你有一些文檔閱讀要做,以使整個事情堅持使用Spring。

編輯

示例查詢:

Query query = em.createQuery("select e from EntityClass1 where e.name = :name");
query.setParameter(:name, "foo");
List results = query.getResultList();

編輯

更新版本:

hibernate-core-3.5.1-Final
hibernate-entitymanager-3.5.1-Final
hibernate-jpa-2.0-api-1.0.0.Final
hibernate-annotations-3.5.1-Final
hibernate-commons-annotations-3.2.0.Final
dom4j-1.6.1
slf4j-api-1.6.4
slf4j-log4j12-1.6.4
...

WebApp和使用Spring / Hibernate進行配置的StandAlone應用程序之間沒有區別。 你需要為hibernate提供相同的JAR文件。 對於spring,你可以取消面向Web的JAR,但是否則需要所有核心內容。

將JAR放在“/ lib”文件夾中,並通過在Manifest.mf中指定它作為Maven構建的一部分將其添加到類路徑中。

要在命令行上引導Java應用程序,只需調用/加載ApplciationContext即可從Main類開始,如下所示......

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

String[] contextXml = new String[]{ "resources/spring-context.xml", "resources/spring-db.xml" };

ApplicationContext ctx = new ClassPathXmlApplicationContext(contextXml);

// invoke your business logic

MyBusinessService bean = getBean(MyBusinessService.class);

bean.doSomething();
...
}

這是DAO的一個例子

import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import org.springframework.stereotype.Repository;

@Repository
public class MyDaoHibernate extends HibernateDaoSupport {


    /*** Functional methods ***/

    public void save(MyValueObject vo) throws DatabaseException {       
        getHibernateTemplate().saveOrUpdate(vo);
    }

    public MyValueObject get(Long id) throws DatabaseException {
        return getHibernateTemplate().get(MyValueObject.class, id);
    }

    /*** Getter & Setters ***/

    @Autowired
    public void initSessionFactory(SessionFactory sessionFactory) {
        this.setSessionFactory(sessionFactory);
    }   
}

[編輯]

Yanflea提出的好處是,您將希望使用數據庫連接池庫,因為在Web應用程序中不提供此類功能。 下載commons-dbcp.jar並將以下內容添加到Spring配置中......

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
    <property name="driverClassName"    value="com.sybase.jdbc2.jdbc.SybDriver" />
    <property name="url"                value="${jdbc.url}" />
    <property name="username"           value="${jdbc.username}" />
    <property name="password"           value="${jdbc.password}" />
</bean> 

SessionFactory是在任何(基於Web或桌面)應用程序中使用Hibernate的最佳方式。 只需從http://sourceforge.net/projects/hibernate/files/hibernate4/下載最新的hibernate發行包。 在lib文件夾下有一個必需的文件夾。 包括所有這些罐子,因為它們對於hibernate項目都是強制性的。 然后定義一個類,它將為您提供SessionFactory的單例實例。 您可以在Configuration實例上使用buildSessionFactory()方法獲取此實例(它是一個可以讀取和處理hibernate配置文件的類)。 您必須在文件hibernate.cfg.xml中定義所有連接和其他參數(您還可以使用任何其他名稱)。 有關更多詳細信息,請訪問http://javabrains.koushik.org/ 在Hibernate部分,只需要閱讀前幾個教程。 那里的解釋非常好。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM