问题描述
我通常在Web应用程序中使用spring休眠模式,因此我使用DI和maven进行配置,现在我想在不使用maven或spring的桌面/Swing应用程序中使用休眠模式,我想知道以下几点:/p>
i usually use hibernate with spring in web applications so i use DI and maven for configuration, now i want to use hibernate in desktop/swing application that doesn't use maven or spring, and i was wondering about the following:
- 我需要什么罐子?
- 如何配置休眠模式,以及如何进行示例查询?
请告知,谢谢.
推荐答案
我不了解Spring,因此,如果您真的想使用它,则必须稍微更新一下我的答案(例如,Spring有其自己的机制用于初始化实体管理器.
I don't know about Spring, so you will have to update my answer a little if you really want to use it (for instance Spring has its own mechanism for initializing the entity manager).
依赖项
这是我最近在我的桌面项目中使用的配置(此后可能会有所发展),使用基于JPA的Hibernate (即它使用EntityManager):
This is the configuration I used for a recent desktop project of mine (some versions may have evolved since), that uses Hibernate over JPA (i.e. it uses an 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中心中找到所有这些文件.
You can find all them on maven central.
配置
您需要在META-INF文件夹中提供有效的persistence.xml:
You need a valid persistence.xml in META-INF folder :
<?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>
使用您自己的数据库信息/驱动程序进行更新.
Update it with your own database info/driver.
用法
创建实体类的通常方法是为 persitence.xml 文件中注册的 EntityClass1 , EntityClass2 , EntityClass3 以上.
Create Entity classes the usual way for EntityClass1, EntityClass2, EntityClass3 registered in the persitence.xml file above.
然后,对于 EntityManager ...,因为您不在EE环境中,所以必须从 EntityManagerFactoty 获取它的一个实例:
Then, for the EntityManager... since your are not in a EE environment, you must get an instance of it from the EntityManagerFactoty :
EntityManagerFactory emf = Persistence.createEntityManagerFactory("NONJTAPersistenceUnit");
EntityManager em = emf.createEntityManager();
(同样,Spring可能提供了另一种方式来获取它,请查阅文档).
(Again, Spring may provide an other way to get it, check on the documentation).
您可以通过这种方式从那里执行例如持久操作:
From there you can perform, for instance a persist operation, this way :
em.getTransaction().begin();
em.persist(entity);
em.getTransaction().commit();
您需要阅读一些文档,以使整个过程与Spring保持一致.
You have a little documentation reading to do to make the whole thing sticks with 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
...
这篇关于如何在Eclipse中的独立(Swing)应用程序中配置休眠模式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!