Java 如何在 MS Access 中使用休眠?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1749464/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 22:16:04  来源:igfitidea点击:

How can I use hibernate with MS Access?

javadatabasehibernatems-accessorm

提问by firstthumb

I want to use hibernate with MS Access. Which dialect should I use and can you give me a sample hibernate configuration file with MS Access?

我想在 MS Access 中使用休眠。我应该使用哪种方言,你能给我一个带有 MS Access 的示例休眠配置文件吗?

采纳答案by Pascal Thivent

For MS Access, you'll need the dialect from HXTT. You'll need to use the hibernate support packageprovided by HXTT. There is also a sample projectthat you can check for a fully working example.

对于 MS Access,您需要来自HXTT的方言。您需要使用HXTT 提供的休眠支持包。还有一个示例项目,您可以检查一个完整的工作示例。

Below, a sample minimal configuration:

下面是一个示例最小配置:

# Hxtt Access dialect sample
hibernate.dialect=com.hxtt.support.hibernate.HxttAccessDialect
hibernate.connection.driver_class=com.hxtt.sql.access.AccessDriver
hibernate.connection.url=jdbc:access:///c:/yourAccessDirectory

PS: If MS Access is not a written in stone requirement, maybe you should consider using something else like... well, anything.

PS:如果 MS Access 不是一成不变的要求,也许您应该考虑使用其他类似的东西......好吧,任何东西。

回答by Syd

@Firstthumb. Sorry, this is not really an answer but just expressing a comment. I was in the same boat - considering using MS Access to develop a local tipping competition for the Football World Cup 2010. I could access MS Access directly using the Oledb but as a training exercise to reskill a bunch of Java developers moving to .NET, I wanted to show the usage of nHibernate as the DAO layer.

@第一拇指。抱歉,这不是真正的答案,而只是发表评论。我在同一条船上 - 考虑使用 MS Access 为 2010 年足球世界杯开发本地小费竞赛。我可以使用 Oledb 直接访问 MS Access,但作为培训练习,重新培训一群迁移到 .NET 的 Java 开发人员,我想展示 nHibernate 作为 DAO 层的用法。

After investigating the non-availability of a robust and supported dialect from JBoss/Hibernate team and other considerations, I decided to forego the exercise on using MSAccess. I downloaded the free SQL Express 2008 instead.

在调查了来自 JBoss/Hibernate 团队的强大且受支持的方言的不可用性以及其他考虑因素之后,我决定放弃使用 MSAccess 的练习。我下载了免费的 SQL Express 2008。

For those who still want to use MSAccess in .NET, please see this link)

对于那些仍然想在 .NET 中使用 MSAccess 的人,请参阅此链接

回答by Quentin T.

You can use an other strategy and a free strategy to resolve this problem (HXTT is not free) :

您可以使用其他策略和免费策略来解决此问题(HXTT 不是免费的):

http://www.programmingforfuture.com/2011/06/how-to-use-ms-access-with-hibernate.html

http://www.programmingforfuture.com/2011/06/how-to-use-ms-access-with-hibernate.html

Personaly I got an exception when I try to reverse engineering the ms database (org.hibernate.exception.GenericJDBCException: Error while reading primary key meta data), yet it works for many people.

就我个人而言,当我尝试对 ms 数据库进行逆向工程时遇到了一个异常(org.hibernate.exception.GenericJDBCException:读取主键元数据时出错),但它适用于许多人。

回答by Gergely Kovács

Actual solution here!

实际解决方案在这里!

After spending 1 day trying out different solutions ODBC, HXTT, etc. I found this beauty :) http://ucanaccess.sourceforge.net/site.html.

在花了 1 天的时间尝试了不同的解决方案 ODBC、HXTT 等之后。我发现了这个美妙之处 :) http://ucanaccess.sourceforge.net/site.html

It couldn't be any simpler: just add the jars from the site to your project libs and.

再简单不过了:只需将站点中的 jar 添加到您的项目库中,然后将其添加到您的项目库中。

META-INF/persistence.xml

META-INF/persistence.xml

<?xml version="1.0" encoding="utf-8"?>
<persistence>
    <persistence-unit name="traderMandate">
        <provider>org.hibernate.ejb.HibernatePersistence</provider>
        <properties>
            <property name="hibernate.dialect" value="org.hibernate.dialect.SQLServerDialect" />
            <property name="hibernate.connection.url" value="jdbc:ucanaccess://C:/MY.accdb;" />
            <property name="hibernate.connection.driver_class" value="net.ucanaccess.jdbc.UcanaccessDriver"/>
            <property name="hibernate.archive.autodetection" value="class" />
        </properties>
    </persistence-unit>
</persistence>          

Spring config:

弹簧配置:

<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    <property name="persistenceUnitName" value="traderMandate"/>
</bean>

<tx:annotation-driven/>

<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
    <property name="entityManagerFactory" ref="entityManagerFactory"/>
</bean>

And you're good to go. ;)

你很高兴去。;)

回答by jon martin solaas

As a late note (commenting is not available for me): UCanAccess surely works, although I don't (yet) have experience with writing data. Anyways, regarding the dialect to use, I run with

作为一个迟到的说明(我无法评论):UCanAccess 肯定有效,尽管我(还)没有编写数据的经验。无论如何,关于使用的方言,我运行

            <property name="hibernate.dialect" value="org.hibernate.dialect.HSQLDialect"/>

because UCanAccess use HSQLDB, and I suppose that dialect is the best match. There isn't any sql actually hitting ms-access in the picture here, Hymancess reads and writes directly to the access mdb-file, there is no microsoft code interpreting any sql anywhere, as far as I can understand, and therefore using SQLServerDialect will only confuse HSQLDB code trying to understand it.

因为 UCanAccess 使用 HSQLDB,我认为方言是最好的匹配。这里的图片中实际上没有任何 sql 访问 ms-access,Hymancess 直接读取和写入访问 mdb 文件,据我所知,任何地方都没有微软代码解释任何 sql,因此使用 SQLServerDialect 将只会混淆试图理解它的 HSQLDB 代码。