1

对于我的工作,我必须在项目中第一次使用Hibernate 。实际上,一切都是由其他人设置的,现在我必须稍微扩展“查询”。我们有一个运行在Eclipse中的Tomcat v7.0服务器,Quartz框架用于每 5 分钟安排一次查询。然后这个查询由 Hibernate 执行/处理。我想我理解了大部分内容,但正如我所说:我是这里的新手,所以这个问题可能真的很愚蠢,但我无法使用 Google/StackOverflow 找到答案,所以就这样吧。

由于我在hibernate的查询配置中修改了一些东西,所以我一直得到null结果。经过一番挣扎,我已经登录并运行。这是我在启动 Tomcat 服务器时得到的日志的(我认为)相关部分:

30-apr-2012 11:02:32 org.hibernate.cfg.SettingsFactory buildSettings
WARNING: Could not obtain connection metadata
java.sql.SQLException: An SQLException was provoked by the following failure: java.lang.UnsupportedOperationException
    at com.mchange.v2.sql.SqlUtils.toSQLException(SqlUtils.java:106)
    at com.mchange.v2.sql.SqlUtils.toSQLException(SqlUtils.java:65)
    at com.mchange.v2.sql.SqlUtils.toSQLException(SqlUtils.java:62)
    at com.mchange.v2.c3p0.impl.NewPooledConnection.handleThrowable(NewPooledConnection.java:369)
    at com.mchange.v2.c3p0.impl.NewProxyDatabaseMetaData.getDatabaseMajorVersion(NewProxyDatabaseMetaData.java:3692)
    at org.hibernate.cfg.SettingsFactory.buildSettings(SettingsFactory.java:126)
    at org.hibernate.cfg.Configuration.buildSettingsInternal(Configuration.java:2863)
    at org.hibernate.cfg.Configuration.buildSettings(Configuration.java:2859)
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1870)
    at org.springframework.orm.hibernate3.LocalSessionFactoryBean.newSessionFactory(LocalSessionFactoryBean.java:753)
    at org.springframework.orm.hibernate3.LocalSessionFactoryBean.buildSessionFactory(LocalSessionFactoryBean.java:691)
    at org.springframework.orm.hibernate3.AbstractSessionFactoryBean.afterPropertiesSet(AbstractSessionFactoryBean.java:211)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1390)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1359)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:540)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory$1.run(AbstractAutowireCapableBeanFactory.java:485)
    at java.security.AccessController.doPrivileged(Native Method)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:455)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:251)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:169)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:248)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:170)
    at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:268)
    at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveValueIfNecessary(BeanDefinitionValueResolver.java:104)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1274)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1042)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:539)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory$1.run(AbstractAutowireCapableBeanFactory.java:485)
    at java.security.AccessController.doPrivileged(Native Method)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:455)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:251)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:169)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:248)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:170)
    at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:268)
    at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveValueIfNecessary(BeanDefinitionValueResolver.java:104)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1274)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1042)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:539)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory$1.run(AbstractAutowireCapableBeanFactory.java:485)
    at java.security.AccessController.doPrivileged(Native Method)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:455)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:251)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:169)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:248)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:170)
    at com.mh2o.dbase.manager.HibernateDaoManager.getInstance(HibernateDaoManager.java:17)
    at com.mh2o.dbase.wbuserlist.WelcomeBoardMgr.<init>(WelcomeBoardMgr.java:11)
    at com.mh2o.application.cache.welcomeboard.WelcomeCache.synchronize(WelcomeCache.java:44)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.springframework.util.MethodInvoker.invoke(MethodInvoker.java:283)
    at org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean$MethodInvokingJob.executeInternal(MethodInvokingJobDetailFactoryBean.java:272)
    at org.springframework.scheduling.quartz.QuartzJobBean.execute(QuartzJobBean.java:86)
    at org.quartz.core.JobRunShell.run(JobRunShell.java:216)
    at org.quartz.simpl.SimpleThreadPool$WorkerThread.run(SimpleThreadPool.java:549)
Caused by: java.lang.UnsupportedOperationException
    at sun.jdbc.odbc.JdbcOdbcDatabaseMetaData.getDatabaseMajorVersion(Unknown Source)
    at com.mchange.v2.c3p0.impl.NewProxyDatabaseMetaData.getDatabaseMajorVersion(NewProxyDatabaseMetaData.java:3678)
    ... 53 more
30-apr-2012 11:02:32 org.hibernate.transaction.TransactionFactoryFactory buildTransactionFactory

因此,SQL 中似乎存在错误。现在,我添加了一些文件,所以我猜这就是错误出现的地方。数据库包含一个可以连接到已经查询过的表的表,这就是我所做的:

employeeavailablehours.hbm.xml(描述数据库中的表):

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN"  "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.mh2o.application.vo.planningview" schema="N_C">
 <class name="EmployeeAvailableHours" table="WerknemersBeschikbareUr">
    <id column="ID" name="id" type="string">
        <generator class="native"/>
    </id>
    <property column="InitialenWerknemer"  name="initial" generated="never" lazy="false" not-null="true" type="string"/>
    <property column="Vanaf"  name="from" generated="never" lazy="false" not-null="true" type="string"/>
    <property column="Tot"  name="to" generated="never" lazy="false" not-null="true" type="string"/>
    <property column="Volgnummer"  name="indexnumber" generated="never" lazy="false" not-null="true" type="string"/>
  </class>
</hibernate-mapping>

employeeinfo.hbm.xml(已经由其他人创建,由我修改以包含对上表的引用):

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN"  "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.mh2o.application.vo.planningview" schema="N_C">
 <class name="EmployeeInfo" table="WerknemersInfo">
    <id column="ID" name="id" type="string">
        <generator class="native"/>
    </id>
    <property column="Naam"  name="name" generated="never" lazy="false" not-null="true" type="string"/>
    <property column="InitialenWerknemer"  name="initial" generated="never" lazy="false" not-null="true" type="string"/>
    <set name="employeeavailablehours" table="WerknemersBeschikbareUr" inverse="false" lazy="false" fetch="select">
        <key>
            <column name="InitialenWerknemer" not-null="true" />
        </key>
        <one-to-many class="EmployeeAvailableHours"  />
    </set>
  </class>


</hibernate-mapping>

当然,我已经context.xml在 xml 文件中添加了新表 employeeavailablehours,并创建了类来表示新表中的数据。此外,我还编辑了代表现有表的类,以保存对我添加的表的引用。我认为这不是错误出现的地方,查看异常,这就是为什么我在这里不包括这些类的 Java 源代码。如果需要,我当然会。

如果你们需要更多信息,请询问,我会编辑我的问题。提前致谢!

4

1 回答 1

1

事实证明我做错了什么:我用作外键的键根本不能用作外键。更改将一张表与另一张表连接的位置的完整设置解决了该问题。

于 2012-05-10T15:48:44.830 回答