2014-04-01 49 views
1

我知道有很多關於解釋如何在項目中使用Swing + Hibernate和Maven的信息,但我認爲所有這些信息都過時了。 我只是想跟着this而我剛剛結束了很多導入問題,一旦顯然新版本的Hibernate-core沒有這個教程時的一些類(當我看到時我有點笑了使用向量( - :)的代碼Java Swing + Hibernate + Maven

問題是,是否有可能使用最新版本的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. 

將不勝感激所有幫助,甚至指着我向好的文檔...

乾杯

UPDATE: 堆棧跟蹤

--- 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 
+0

添加你的錯誤板塊 – BDR

回答

2

事情我已經試過/檢查:

如果下載的是本教程中介紹,然後檢查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無法完成。我必須恢復到經典才能完成該步驟,並在最後將其更改回內部。

希望以上所有內容對您有用。

+0

嗨zlinks!我對你的接吻者非常滿意,看起來你已經付出了很多努力,並且已經爲此付出了努力。我嘗試從未使用maven的鏈接下載和構建項目,但我不知道如何在Maven項目中轉換此項目,因此我可以聲明hibernate庫。我使用NB 8.0,但是當我導入項目時,它抱怨說它找不到一些hibernate庫(例如HibernateException,Query和Session)。將看看周圍,看看我怎樣才能在maven項目中轉換這個項目。 –

+0

謝謝你,讓它工作......不得不工作一些東西,但總的來說你的帖子是一個救世主。我還有一個問題,是否可以做所有這些,而無需手動完成所有映射?我想要遷移到Swing的項目是一個JSF項目,它具有所有必要的註釋以避免所有映射。感謝你的幫助! –

+0

Hi TW。我很高興它幫助你。我相信你可以從你的JSF項目中獲取所有註釋類,它們描述你的模型結構,並將它們放在你的Swing應用程序中。然後,您可能需要更改項目的xml文件。嘗試再次執行逆向工程步驟,但是這次請檢查'JDK 5語言特性'和'EJB3批註'以瞭解項目如何與註釋一起工作。 – zlinks