2013-04-29 86 views
5

我正在使用JPA和c3p0並試圖查詢一個表並獲取聲稱該表不存在的堆棧跟蹤。我可以在數據庫中打開一個連接,例如DbVisualizer,然後在那裏查看錶格。事實上,我的應用程序的調試聲明表明它能夠建立連接並測試其可行性。但是,它沒有找到表格。MySQLSyntaxErrorException:表XYZ不存在

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 [[email protected]] on CHECKOUT. 
15:45:53.949 [http-8080-1] DEBUG c.m.v.c.i.C3P0PooledConnectionPool - Test of PooledConnection [[email protected]] on CHECKOUT has SUCCEEDED. 
15:45:53.950 [http-8080-1] DEBUG c.m.v.resourcepool.BasicResourcePool - trace [email protected] [managed: 3, unused: 2, excluded: 0] (e.g. [email protected]) 
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 - [email protected] 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目錄/主/資源/ 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> 

事情我發現可疑是hibernate查詢的一部分,它試圖查詢select ... from reportsDb.alerts alert0_ - 我如何確認「reportsDb」實際上代表我在applicationContext.xml中指定的數據源?

ETA: 實體,警報,看起來是這樣的:

@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; 
    } 
} 
+0

如何實體是什麼樣子?在@Table註釋中,您可以指定表,目錄和模式名稱 – German 2013-04-29 20:57:35

+0

@German - 好點,我忘了 - 編輯添加。 – barclay 2013-04-29 21:51:09

回答

4

從你的實體定義,去掉「目錄=‘reportsDb’」的一部分,因爲它是被用來建立一個像查詢「 「select from'reportsDb.alerts'」 Mysql不使用目錄AFAIK

+0

你達人。這很好。太感謝了! – barclay 2013-04-29 21:59:44

+0

我發現我有另一塊需要指向同一服務器上的不同數據庫,所以我從applicationContext.xml中的數據源規範中刪除了數據庫「worker_events」,並將'catalog ='worker_events''加回到我的實體。那工作。最初,我一直試圖將目錄指向整個數據源名稱,而不是數據庫名稱。我的測試表明,目錄與MySQL一起工作。 – barclay 2013-04-30 21:03:32

6

如果表中確實存在mySQL,並且使用Linux/Unix,並且錯誤顯示錶名在錯誤/大寫的情況下,問題是MySQL中的表名是區分大小寫的,而hibernate是上層的,我使用的是hibernate 4.3 。

我剛纔有這個問題。這裏解釋:lower_case_table_names=1

--edit--回想起來,最好找到並更改任何@Table或hbm.xml引用來匹配數據庫。我運行了一個嚮導,它生成一個帶有大寫名字的hbm.xml - 直到現在才意識到它在我的項目中。我會在這裏留下來讓人們意識到區分大小寫的問題。

的edit--

--end這是我如何固定它:

  1. 刪除數據庫。
  2. 一下添加到/etc/mysql/my.conf:

    set lower_case_table_names=1 #(default value '0'). 
    
  3. 重啓mysqld。
  4. 重新創建數據庫。
  5. (可選?)將註釋/ hbm.xml表引用改爲小寫。
+0

步驟1-4沒有幫助。 – Polyakoff 2016-05-29 04:20:58

+0

我有一個大寫的表名查詢和我總是消息表不存在。感謝您的小寫提示。現在一切順利! :) – PrestigeDev 2016-11-24 17:06:18

1

我們面臨同樣的問題。有一個SQL查詢,沒有一個錯誤傳似

com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException:表 「my_database_name。*」不存在

但查詢本身不含my_database_name引用,甚至*的跡象。

與其他查詢相比,我們發現差異和增加了ORDER BY查詢和錯誤消失。可能是圍繞jdbc或c3p0邏輯的黑客攻擊,但它對我們有效。

0

在我的情況下,原因是因爲左側的外部聯接的子查詢別名,這是工作在SQL編輯器,但不是在JDBCspring,所以我刪除了左側外部聯接的子查詢,並用左側外部沒有子查詢

2

在我的代碼中大大消除了表XYZ的每一次發生後,我發現實際問題:XYZ沒有被JPA引用,而是被一箇舊的無效mysql觸發器引用。也許考慮尋找你的代碼之外的錯誤。

1

我有同樣的問題,我的MySQL數據庫是在Windows上,但我把它移動到Linux導致mysql語法不識別表。 的原因是,在Windows的MySQL不區分大小寫和案例在Linux上 敏感,我能夠在my.cnf解決這個我添加

的lower_case_table_names = 1

還要確保包括

的[mysqld]

在my.cnf文件的開頭,以避免另一個錯誤

「MySQL的my.cnf文件 - 發現選項前沒有組」

-1

在我的代碼上有類似的錯誤,我更改了餘輝文件

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

取代了 「創造降」 與 「更新」

感謝

相關問題