gpt4 book ai didi

Java Swing + Hibernate + Maven

转载 作者:行者123 更新时间:2023-11-30 03:57:34 26 4
gpt4 key购买 nike

我知道有很多信息解释如何在项目中使用 Swing + Hibernate 和 Maven,但我认为所有这些信息都已经过时了。我只是试图关注this我最终遇到了很多导入问题,显然新版本的 Hibernate-core 没有本教程时所具有的一些类(当我看到使用 Vector 的代码时,我笑了一下(-: )

问题是,是否可以使用最新版本的 Hibernate 和 Swing 创建项目?有没有人有 3 年前以外的关于如何做到这一点的信息?

我遇到的问题:

1 - 无法让 HQL 查询在 Netbeans 上运行...它提示

<property name="hibernate.query.factory_class">org.hibernate.hql.internal.classic.ClassicQueryTranslatorFactory</property>

找不到它..尝试了其他选择但没有运气(我真的不在乎这个)

2 - 运行代码时,它会提示

Caused by: java.lang.ClassNotFoundException: org.hibernate.util.DTDEntityResolver
Which is a package from early days of hibernate.

非常感谢所有的帮助,甚至为我指出一个好的文档......

干杯

更新:堆栈跟踪

--- exec-maven-plugin:1.2.1:exec (default-cli) @ SwingProject ---
SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
Apr 01, 2014 4:48:07 PM org.hibernate.Version logVersion
INFO: HHH000412: Hibernate Core {4.3.4.Final}
Apr 01, 2014 4:48:07 PM org.hibernate.cfg.Environment <clinit>
INFO: HHH000206: hibernate.properties not found
Apr 01, 2014 4:48:07 PM org.hibernate.cfg.Environment buildBytecodeProvider
INFO: HHH000021: Bytecode provider name : javassist
Initial SessionFactory creation failed.java.lang.NoClassDefFoundError: org/hibernate/util/DTDEntityResolver
Exception in thread "AWT-EventQueue-0" java.lang.ExceptionInInitializerError
at padaria.util.NewHibernateUtil.<clinit>(NewHibernateUtil.java:30)
at br.com.vaast.main.AdminMain.executeHQLQuery(AdminMain.java:43)
at br.com.vaast.main.AdminMain.runQueryBasedOnFirstName(AdminMain.java:34)
at br.com.vaast.main.AdminMain.jButton1ActionPerformed(AdminMain.java:206)
at br.com.vaast.main.AdminMain.access$200(AdminMain.java:21)
at br.com.vaast.main.AdminMain$3.actionPerformed(AdminMain.java:120)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2018)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2341)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
at java.awt.Component.processMouseEvent(Component.java:6505)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3320)
at java.awt.Component.processEvent(Component.java:6270)
at java.awt.Container.processEvent(Container.java:2229)
at java.awt.Component.dispatchEventImpl(Component.java:4861)
at java.awt.Container.dispatchEventImpl(Container.java:2287)
at java.awt.Component.dispatchEvent(Component.java:4687)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4832)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4492)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4422)
at java.awt.Container.dispatchEventImpl(Container.java:2273)
at java.awt.Window.dispatchEventImpl(Window.java:2719)
at java.awt.Component.dispatchEvent(Component.java:4687)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:735)
at java.awt.EventQueue.access$200(EventQueue.java:103)
at java.awt.EventQueue$3.run(EventQueue.java:694)
at java.awt.EventQueue$3.run(EventQueue.java:692)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87)
at java.awt.EventQueue$4.run(EventQueue.java:708)
at java.awt.EventQueue$4.run(EventQueue.java:706)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:705)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)
Caused by: java.lang.NoClassDefFoundError: org/hibernate/util/DTDEntityResolver
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:800)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:449)
at java.net.URLClassLoader.access$100(URLClassLoader.java:71)
at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
at org.hibernate.cfg.Configuration.reset(Configuration.java:353)
at org.hibernate.cfg.AnnotationConfiguration.reset(AnnotationConfiguration.java:289)
at org.hibernate.cfg.Configuration.<init>(Configuration.java:289)
at org.hibernate.cfg.Configuration.<init>(Configuration.java:293)
at org.hibernate.cfg.AnnotationConfiguration.<init>(AnnotationConfiguration.java:168)
at padaria.util.NewHibernateUtil.<clinit>(NewHibernateUtil.java:26)
... 41 more
Caused by: java.lang.ClassNotFoundException: org.hibernate.util.DTDEntityResolver
at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
... 59 more

最佳答案

我尝试过/检查过的事情:

如果您下载教程中描述的项目,然后检查 pom.xml,您会发现使用的库相当旧。我相信该项目是使用旧版本的 Netbeans 构建的。如果您查看本教程:https://netbeans.org/kb/docs/java/hibernate-java-se.html ,实际上是同一个,但没有使用 maven,其中提到“NetBeans IDE 7.4 和更早版本捆绑了 Hibernate 3”。

如果您尝试按照教程在不使用 Maven 的情况下构建项目:https://netbeans.org/kb/docs/java/hibernate-java-se.html当您使用 NetBeans 8.0 时,您将了解所需的 Hibernate 4.x 库。这是因为 IDE 默认情况下与 Hibernate 4.x 库捆绑在一起。返回并使用 Maven 构建项目,这次我在 pom.xml 中得到了以下内容

<repositories>
<repository>
<id>unknown-jars-temp-repo</id>
<name>A temporary repository created by NetBeans for libraries and jars it could not identify. Please replace the dependencies in this repository with correct ones and delete this repository.</name>
<url>file:${project.basedir}/lib</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>4.1.8.Final</version>
</dependency>
<dependency>
<groupId>unknown.binary</groupId>
<artifactId>hibernate-jpa-2.0-api-1.0.1.Final</artifactId>
<version>SNAPSHOT</version>
</dependency>
<dependency>
<groupId>unknown.binary</groupId>
<artifactId>mysql-connector-java-5.1.23-bin</artifactId>
<version>SNAPSHOT</version>
</dependency>
</dependencies>

对我有用的解决方案。

上面的 pom.xml 提供了一些见解,但有一些部分让我不高兴: <groupId>unknown.binary</groupId>temporary repository .

所以我开始搜索,最后在这里:http://hibernate.org/orm/documentation/getting-started/在这里我得到了我需要的东西:http://hibernate.org/orm/documentation/getting-started/ .

我将 pom.xml 更改为:

<dependencies>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>4.3.5.Final</version>
</dependency>

<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.30</version>
</dependency>

最新版本的 Hibernate 一切正常。

注意:

正如您从上面的链接中看到的,您有两个选择。您可以使用 <artifactId>hibernate-entitymanager</artifactId><artifactId>hibernate-core</artifactId>

我相信这些引用资料可以提供一些答案:

注2:
我在 org.hibernate.hql.internal.classic.ClassicQueryTranslatorFactory 方面遇到了一些麻烦。为了构建该项目,我必须使用没有内部的版本。如果我确实使用内部版本,则步骤 Creating Hibernate Mapping Files and POJOs From a Database无法完成。我必须恢复到经典模式才能完成该步骤,并在最后将其改回内部模式。

希望以上内容对您有用。

关于Java Swing + Hibernate + Maven,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22778450/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com