使用netbeans从数据库生成Hibernate映射文件和POJO

时间:2013-05-17 02:12:50

标签: hibernate netbeans entity hibernate-mapping annotated-pojos

我刚刚重新安装了计算机上的所有内容,不得不重新设置Netbeans。 我已经打开了一个hibernate项目,我刚刚在数据库中添加了3个新表。 我试图通过使用“Hibernate Mapping Files和POJO from Database”来映射这些文件。 但是,Netbeans给了我以下错误:

INFO [org.netbeans.modules.hibernate.wizards.HibernateCodeGenWizardDescriptor]: access to hibernate fails.
org.hibernate.HibernateException: could not instantiate QueryTranslatorFactory: org.hibernate.hql.internal.classic.ClassicQueryTranslatorFactory
    at org.hibernate.cfg.SettingsFactory.createQueryTranslatorFactory(SettingsFactory.java:391)
    at org.hibernate.cfg.SettingsFactory.buildSettings(SettingsFactory.java:222)
    at org.hibernate.cfg.Configuration.buildSettings(Configuration.java:2005)
    at org.hibernate.cfg.JDBCMetaDataConfiguration.readFromJDBC(JDBCMetaDataConfiguration.java:40)
    at org.netbeans.modules.hibernate.wizards.HibernateCodeGenWizardDescriptor.checkConfig(HibernateCodeGenWizardDescriptor.java:282)
    at org.netbeans.modules.hibernate.wizards.HibernateCodeGenWizardDescriptor.isValid(HibernateCodeGenWizardDescriptor.java:177)
    at org.openide.WizardDescriptor.updateStateOpen(WizardDescriptor.java:872)
    at org.openide.WizardDescriptor.updateState(WizardDescriptor.java:820)
    at org.openide.loaders.TemplateWizard.updateState(TemplateWizard.java:754)
    at org.netbeans.modules.project.ui.NewFileWizard.updateState(NewFileWizard.java:124)
    at org.openide.WizardDescriptor._updateState(WizardDescriptor.java:798)
    at org.openide.WizardDescriptor.goToNextStep(WizardDescriptor.java:1070)
    at org.openide.WizardDescriptor.access$1800(WizardDescriptor.java:90)
    at org.openide.WizardDescriptor$Listener$1.run(WizardDescriptor.java:2122)
    at org.openide.WizardDescriptor.lazyValidate(WizardDescriptor.java:1502)
    at org.openide.WizardDescriptor.access$1900(WizardDescriptor.java:90)
    at org.openide.WizardDescriptor$Listener.actionPerformed(WizardDescriptor.java:2141)
    at sun.reflect.GeneratedMethodAccessor231.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at org.openide.util.WeakListenerImpl$ProxyListener.invoke(WeakListenerImpl.java:457)
    at com.sun.proxy.$Proxy43.actionPerformed(Unknown Source)
    at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1995)
    at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2318)
    at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:387)
    at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:242)
    at javax.swing.AbstractButton.doClick(AbstractButton.java:357)
    at javax.swing.AbstractButton.doClick(AbstractButton.java:337)
    at org.openide.WizardDescriptor.doNextClick(WizardDescriptor.java:1618)
    at org.netbeans.modules.project.ui.TemplateChooserPanel$1.actionPerformed(TemplateChooserPanel.java:95)
    at org.netbeans.modules.project.ui.TemplateChooserPanelGUI$FileChooserBuilder.actionPerformed(TemplateChooserPanelGUI.java:418)
    at org.openide.explorer.view.ListView.performObjectAt(ListView.java:684)
    at org.openide.explorer.view.ListView$PopupSupport.mouseClicked(ListView.java:1327)
    at java.awt.AWTEventMulticaster.mouseClicked(AWTEventMulticaster.java:253)
    at java.awt.Component.processMouseEvent(Component.java:6300)
    at javax.swing.JComponent.processMouseEvent(JComponent.java:3275)
    at java.awt.Component.processEvent(Component.java:6062)
    at java.awt.Container.processEvent(Container.java:2039)
    at java.awt.Component.dispatchEventImpl(Component.java:4660)
    at java.awt.Container.dispatchEventImpl(Container.java:2097)
    at java.awt.Component.dispatchEvent(Component.java:4488)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4575)
    at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4245)
    at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4166)
    at java.awt.Container.dispatchEventImpl(Container.java:2083)
    at java.awt.Window.dispatchEventImpl(Window.java:2489)
    at java.awt.Component.dispatchEvent(Component.java:4488)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:674)
    at java.awt.EventQueue.access$400(EventQueue.java:81)
    at java.awt.EventQueue$2.run(EventQueue.java:633)
    at java.awt.EventQueue$2.run(EventQueue.java:631)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:87)
    at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:98)
    at java.awt.EventQueue$3.run(EventQueue.java:647)
    at java.awt.EventQueue$3.run(EventQueue.java:645)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:87)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:644)
    at org.netbeans.core.TimableEventQueue.dispatchEvent(TimableEventQueue.java:159)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:178)
    at java.awt.Dialog$1.run(Dialog.java:1052)
    at java.awt.Dialog$3.run(Dialog.java:1104)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.awt.Dialog.show(Dialog.java:1102)
    at org.netbeans.core.windows.services.NbPresenter.superShow(NbPresenter.java:1076)
    at org.netbeans.core.windows.services.NbPresenter.doShow(NbPresenter.java:1126)
    at org.netbeans.core.windows.services.NbPresenter.run(NbPresenter.java:1098)
    at org.netbeans.core.windows.services.NbPresenter.run(NbPresenter.java:128)
    at org.openide.util.Mutex.doEventAccess(Mutex.java:1363)
    at org.openide.util.Mutex.readAccess(Mutex.java:273)
    at org.netbeans.core.windows.services.NbPresenter.show(NbPresenter.java:1083)
    at java.awt.Component.show(Component.java:1591)
    at java.awt.Component.setVisible(Component.java:1543)
    at java.awt.Window.setVisible(Window.java:843)
    at java.awt.Dialog.setVisible(Dialog.java:987)
    at org.netbeans.core.windows.services.DialogDisplayerImpl$1AWTQuery.showDialog(DialogDisplayerImpl.java:260)
    at org.netbeans.core.windows.services.DialogDisplayerImpl$1AWTQuery.run(DialogDisplayerImpl.java:186)
    at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:209)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:672)
    at java.awt.EventQueue.access$400(EventQueue.java:81)
    at java.awt.EventQueue$2.run(EventQueue.java:633)
    at java.awt.EventQueue$2.run(EventQueue.java:631)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:87)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:642)
    at org.netbeans.core.TimableEventQueue.dispatchEvent(TimableEventQueue.java:159)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)
Caused by: java.lang.ClassCastException: org.hibernate.hql.internal.classic.ClassicQueryTranslatorFactory cannot be cast to org.hibernate.hql.QueryTranslatorFactory
    at org.hibernate.cfg.SettingsFactory.createQueryTranslatorFactory(SettingsFactory.java:388)
    ... 94 more

我的hibernate.cfg文件如下所示:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
  <session-factory>
    <property name="hibernate.connection.driver_class">com.microsoft.sqlserver.jdbc.SQLServerDriver</property>
    <property name="hibernate.connection.url">jdbc:sqlserver://SERVER:PORT;databaseName=NAME</property>
    <property name="hibernate.connection.username">USER</property>
    <property name="hibernate.connection.password">PASS</property>
    <property name="hibernate.show_sql">true</property>
    <property name="hibernate.current_session_context_class">thread</property>
    <property name="hibernate.query.factory_class">org.hibernate.hql.internal.classic.ClassicQueryTranslatorFactory</property>
    <mapping resource="com/bmw/resources/LocationDept.hbm.xml"/>
</session-factory>
</hibernate-configuration>

我正在连接到SQL Server 2008数据库。

我正在使用Hibernate 4.2.0并在我的类路径中包含以下JAR:

antlr-2.7.7.jar
dom4j-1.6.1.jar
hibernate-commons-annotations-4.0.1.Final.jar
hibernate-core-4.2.0.Final.jar
hibernate-jpa-2.0-api-1.0.1.Final.jar
javassist-3.15.0-GA.jar
jboss-logging-3.1.0.GA.jar
jboss-transaction-api_1.1_spec-1.0.0.Final.jar
hibernate-entitymanager-4.2.0.Final.jar
sqljdbc4.jar
log4j-1.2.17.jar

有什么想法吗?

1 个答案:

答案 0 :(得分:1)

这对我有用:

hibernate.query.factory_class更改为org.hibernate.hql.classic.ClassicQueryTranslatorFactory,然后转到Hibernate Mapping Files and POJOs from Database步骤

完成更改后,请将其更改回org.hibernate.hql. 内部 .classic.ClassicQueryTranslatorFactory