【问题标题】:MySQLSyntaxErrorException: Table XYZ doesn't existMySQLSyntaxErrorException:表 XYZ 不存在
【发布时间】:2013-04-23 15:51:35
【问题描述】:

我正在使用 JPA 和 c3p0 并尝试查询表并取回声称该表不存在的堆栈跟踪。例如,我可以在 DbVisualizer 中打开与数据库的连接,然后查看那里的表格。事实上,我的应用程序中的调试语句表明它能够建立连接并测试其可行性。但后来它没有找到桌子。

15:45:53.940 [http-8080-1] DEBUG o.h.e.j.i.LogicalConnectionImpl - Obtaining JDBC connection
15:45:53.940 [http-8080-1] DEBUG c.m.v.c.i.C3P0PooledConnectionPool - Testing PooledConnection [com.mchange.v2.c3p0.impl.NewPooledConnection@4d687dcd] on CHECKOUT.
15:45:53.949 [http-8080-1] DEBUG c.m.v.c.i.C3P0PooledConnectionPool - Test of PooledConnection [com.mchange.v2.c3p0.impl.NewPooledConnection@4d687dcd] on CHECKOUT has SUCCEEDED.
15:45:53.950 [http-8080-1] DEBUG c.m.v.resourcepool.BasicResourcePool - trace com.mchange.v2.resourcepool.BasicResourcePool@7930ebb [managed: 3, unused: 2, excluded: 0] (e.g. com.mchange.v2.c3p0.impl.NewPooledConnection@3e30e173)
15:45:53.950 [http-8080-1] DEBUG o.h.e.j.i.LogicalConnectionImpl - Obtained JDBC connection
15:45:53.966 [http-8080-1] DEBUG org.hibernate.SQL - select alert0_.rrdb_key as rrdb1_0_, alert0_.date as date0_, alert0_.hostname as hostname0_, alert0_.message as message0_, alert0_.program as program0_ from reportsDb.alerts alert0_ where (alert0_.message not like '%Anomolous%') and (alert0_.message not like '%Requeue%')
Hibernate: select alert0_.rrdb_key as rrdb1_0_, alert0_.date as date0_, alert0_.hostname as hostname0_, alert0_.message as message0_, alert0_.program as program0_ from reportsDb.alerts alert0_ where (alert0_.message not like '%Anomolous%') and (alert0_.message not like '%Requeue%')
15:45:54.013 [http-8080-1] DEBUG c.m.v2.c3p0.impl.NewPooledConnection - com.mchange.v2.c3p0.impl.NewPooledConnection@4d687dcd handling a throwable.
com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Table 'reportsDb.alerts' doesn't exist
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) ~[na:1.6.0_45]
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39) ~[na:1.6.0_45]
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27) ~[na:1.6.0_45]
    at java.lang.reflect.Constructor.newInstance(Constructor.java:513) ~[na:1.6.0_45]
    at com.mysql.jdbc.Util.handleNewInstance(Util.java:406) ~[mysql-connector-java-5.1.6.jar:na]
...

这里是 persistence.xml(在 /src/main/resources/META-INF 中):

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="1.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd">
    <persistence-unit name="reportsDb" transaction-type="RESOURCE_LOCAL">
        <description>Hibernate</description>
        <class>com.pronto.mexp.common.entity.Alert</class>
    </persistence-unit>
</persistence>

applicationContext.xml 的一个小节:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context-3.1.xsd">

    <bean id="jpaDialect" class="org.springframework.orm.jpa.vendor.HibernateJpaDialect"/>

    <bean id="reportsDbEntityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
        <property name="dataSource" ref="reportsDbDataSource" />
        <property name="jpaVendorAdapter">
            <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
                <property name="showSql" value="true"/>
                <property name="generateDdl" value="false" />
                <property name="databasePlatform" value="org.hibernate.dialect.MySQL5InnoDBDialect" />
            </bean>
        </property>
        <property name="persistenceUnitName" value="reportsDb" />
        <property name="jpaDialect" ref="jpaDialect"/>
    </bean>

    <bean id="reportsDbDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
        <property name="driverClass" value="com.mysql.jdbc.Driver"/>
        <!--<property name="jdbcUrl" value="jdbc:mysql://devdbrw01:3306/mexp"/>-->
        <property name="jdbcUrl" value="jdbc:mysql://report101:3306/worker_events"/>
        <property name="user" value="********"/>
        <property name="password" value="********"/>
        <property name="acquireRetryDelay" value="1000"/>
        <property name="acquireRetryAttempts" value="4"/>
        <property name="breakAfterAcquireFailure" value="false"/>
        <property name="testConnectionOnCheckout" value="true"/>
        <property name="maxConnectionAge" value="14400"/>
        <property name="maxIdleTimeExcessConnections" value="1800"/>
    </bean>

    <!-- DAOs -->
    <bean id="genericReportsDbDAO" class="com.pronto.mexp.common.dal.GenericReportsDbJPADAOImpl"/>

    <bean id="alertJPADAO" class="com.pronto.mexp.dal.AlertJPADAOImpl" parent="genericReportsDbDAO"/>
</beans>

我发现可疑的是休眠查询的一部分,它试图查询select ... from reportsDb.alerts alert0_ - 我如何确认“reportsDb”实际上代表我在 applicationContext.xml 中指定的数据源?

预计到达时间: 实体 Alert 如下所示:

@Entity
@Table(name = "alerts", catalog = "reportsDb")
public class Alert {

    int rrdbKey;
    String hostname = "";
    String message = "";
    String program = "";
    Date date = new Date();

    @javax.persistence.Column(name = "rrdb_key", nullable = false, insertable = false, updatable = false, length = 10, precision = 0)
    @Id
    public int getRrdbKey() {
        return rrdbKey;
    }

    public void setRrdbKey(int rrdbKey) {
        this.rrdbKey = rrdbKey;
    }

    @javax.persistence.Column(name = "hostname", nullable = false, insertable = false, updatable = false, length = 32, precision = 0)
    @Basic
    public String getHostname() {
        return hostname;
    }

    public void setHostname(String hostname) {
        this.hostname = hostname;
    }

    @javax.persistence.Column(name = "message", nullable = false, insertable = false, updatable = false, length = 128, precision = 0)
    @Basic
    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    @javax.persistence.Column(name = "program", nullable = true, insertable = false, updatable = false, length = 40, precision = 0)
    @Basic
    public String getProgram() {
        return program;
    }

    public void setProgram(String program) {
        this.program = program;
    }

    @javax.persistence.Column(name = "date", nullable = false, insertable = false, updatable = false, length = 19, precision = 0)
    @Basic
    public Date getDate() {
        return date;
    }

    public void setDate(Date date) {
        this.date = date;
    }
}

【问题讨论】:

  • 实体长什么样?在@Table 注释中,您可以指定表、目录和模式名称
  • @German - 好点,我忘了 - 编辑添加。

标签: mysql hibernate jpa c3p0


【解决方案1】:

如果表确实存在于 mySQL 中,并且您使用的是 Linux/Unix,并且错误以错误/大写形式显示表名,那么问题是 MySQL 中的表名区分大小写且处于休眠状态是他们的大写字母。我正在使用休眠 4.3。

我刚遇到这个问题。此处解释:lower_case_table_names=1

--edit-- 回想起来,最好找到并更改任何 @Table 或 hbm.xml 引用以匹配数据库。我运行了一个生成带有大写名称的 hbm.xml 的向导——直到现在才意识到它在我的项目中。我将把它留在这里,让人们意识到区分大小写。

--编辑结束--

这是我修复它的方法:

  1. 删除数据库。
  2. 将此添加到 /etc/mysql/my.conf:

    set lower_case_table_names=1 #(default value '0'). 
    
  3. 重启mysqld。
  4. 重新创建数据库。
  5. (可选?)将 annotation/hbm.xml 表引用更改为小写。

【讨论】:

  • 步骤 1-4 没有帮助。
  • 我有一个大写表名的查询,我总是得到消息表不存在。感谢您的小写提示。现在一切顺利! :)
【解决方案2】:

从您的实体定义中删除 catalog = 'reportsDb' 部分,因为它用于构建查询,例如 select from 'reportsDb.alerts'。 Mysql 不使用目录,AFAIK。

【讨论】:

  • 我发现我有另一个片段需要指向同一服务器上的不同数据库,所以我从 applicationContext.xml 中的数据源规范中删除了数据库“worker_events”,并添加回 @ 987654323@ 给我的实体。那行得通。最初,我一直试图将目录指向整个数据源名称,而不是数据库名称。我的测试表明目录确实适用于 MySQL。
【解决方案3】:

就我而言,这是 Hibernate 将我的表格转换为小写的问题。

我的错误是:

com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Table 'Pluto.c_story' doesn't exist

Pluto 是我的数据库,C_Story 是我的表(注意:不是 c_story - 小写)。

我所要做的就是:

spring.jpa.hibernate.naming.physical-strategy=org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl

好吧,我希望这对某人有所帮助。

【讨论】:

    【解决方案4】:

    在我的代码中彻底消除表XYZ 的每一次出现后,我发现了实际问题:XYZ 没有被 JPA 引用,而是被一个旧的、无效的 mysql 触发器引用。也许考虑在代码之外寻找错误。

    【讨论】:

      【解决方案5】:

      我遇到了同样的问题,我的 mysql 数据库在 windows 上,但我将它移到了 linux,导致 mysql 语法无法识别表。 原因是windows上的mysql不区分大小写,linux上区分大小写 我能够通过添加来解决这个问题:

      lower_case_table_names=1

      在 my.cnf 中。

      还要确保包含

      [mysqld]

      在 my.cnf 的开头以避免另一个错误

      "MySQL my.cnf file - Found option without preceding group"

      【讨论】:

        【解决方案6】:

        我们遇到了同样的问题。有一个 SQL 查询未通过,并出现类似

        的错误

        com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: 表 'my_database_name.*' 不存在

        但查询本身不包含 my_database_name 引用,甚至没有 * 符号。

        与其他查询相比,我们发现了差异,并在查询中添加了 ORDER BY,错误消失了。可能是对 jdbc 或 c3p0 逻辑的破解,但它对我们有用。

        【讨论】:

          【解决方案7】:

          在我的情况下,原因是因为带有别名的子查询上的左外连接,它在 sql 编辑器上工作,但在 JDBCspring 上没有,所以我删除了子查询的左外连接,并将其替换为没有子查询的左外连接

          【讨论】:

            【解决方案8】:

            我的代码有类似的错误,我更改了持久性文件

               <properties>
                 <!-- Properties for Hibernate -->
                 <property name="hibernate.hbm2ddl.auto" value="create-drop" />
                 <property name="hibernate.show_sql" value="false" />
               </properties>
            

              <properties>
                 <!-- Properties for Hibernate -->
                 <property name="hibernate.hbm2ddl.auto" value="update" />
                 <property name="hibernate.show_sql" value="false" />
              </properties>
            

            将“create-drop”替换为“update”

            谢谢

            【讨论】:

              【解决方案9】:

              我也遇到了这个错误。就我而言,我在 jdbc 数据源类中写了错误的数据库名称。

              我写了cms

              jdbc:mysql://localhost:3306/cms

               <bean id="jdbcDataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
                  <property name="driverClassName" value="com.mysql.jdbc.Driver" />
                  <property name="url" value="jdbc:mysql://localhost:3306/cms" />
                  <property name="username" value="root" />   
                  <property name="password" value="" />
              </bean>
              

              其实我的数据库名是hit_kcsl

              jdbc:mysql://localhost:3306/hit_kcsl

               <bean id="jdbcDataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
                  <property name="driverClassName" value="com.mysql.jdbc.Driver" />
                  <property name="url" value="jdbc:mysql://localhost:3306/hit_kcsl" />
                  <property name="username" value="root" />   
                  <property name="password" value="" />
              </bean>
              

              【讨论】:

                【解决方案10】:

                在我的情况下,我在 cfg.xml 中映射了错误的方言,我使用的是 MySQL57 db,但使用的是 MySQL 方言。当我将正确的方言替换为 MySQL57Dialect 时,它起作用了。

                【讨论】:

                  【解决方案11】:

                  只需在 hibernate.cfg.xml 文件中添加属性,然后更新 项目然后运行主要方法,异常会去..谢谢 你..

                  <session-factory><property name="hibernate.hbm2ddl.auto">create</property> </session-factory>
                  

                  【讨论】:

                    猜你喜欢
                    • 2016-04-16
                    • 2016-08-23
                    • 2020-05-15
                    • 2017-09-10
                    • 2015-03-29
                    • 1970-01-01
                    • 1970-01-01
                    • 2016-05-19
                    • 2015-07-13
                    相关资源
                    最近更新 更多