2013-04-29 34 views
5

Tôi đang sử dụng JPA và c3p0 và cố gắng truy vấn một bảng và lấy lại một dấu vết ngăn xếp cho rằng bảng không tồn tại. Tôi có thể mở một kết nối đến db trong, ví dụ, DbVisualizer, và xem bảng ở đó. Trong thực tế, các báo cáo gỡ rối từ ứng dụng của tôi cho thấy nó có thể tạo kết nối và kiểm tra tính khả thi của nó. Nhưng sau đó nó không phải là tìm bàn.MySQLSyntaxErrorException: Bảng XYZ không tồn tại

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

Đây là persistence.xml (trong/src/main/nguồn/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> 

Một tiểu mục của 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> 

Điều tôi tìm thấy nghi ngờ là một phần của truy vấn ngủ đông, nơi nó cố gắng để truy vấn select ... from reportsDb.alerts alert0_ - làm thế nào để tôi xác nhận rằng "reportsDb" thực sự là viết tắt của nguồn dữ liệu của tôi mà tôi spec'd trong applicationContext.xml?

ETA: Các tổ chức nào, Alert, trông như thế này:

@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

như thế nào Entity như thế nào? trong chú thích @Table, bạn có thể chỉ định bảng, danh mục và tên lược đồ – German

+0

@German - điểm tốt, tôi quên rằng - đã chỉnh sửa để thêm. – barclay

Trả lời

4

Từ định nghĩa thực thể của bạn, loại bỏ các "Danh mục = 'reportsDb'" một phần, vì nó đang được sử dụng để xây dựng các truy vấn như " Mysql không sử dụng danh mục, AFAIK

+0

bạn da man. điều đó rất hiệu quả. cảm ơn rất nhiều! – barclay

+0

tôi phát hiện ra rằng tôi có một mảnh khác cần trỏ đến một cơ sở dữ liệu khác trên cùng một máy chủ, vì vậy tôi đã xóa cơ sở dữ liệu "worker_events" khỏi spec dữ liệu nguồn của tôi trong applicationContext.xml và thêm lại 'catalog = 'worker_events'' vào thực thể của tôi. đã hoạt động. ban đầu, tôi đã cố gắng điểm danh mục tại tên nguồn dữ liệu tổng thể, chứ không phải ở tên cơ sở dữ liệu. thử nghiệm của tôi cho thấy rằng danh mục sản phẩm làm việc với MySQL. – barclay

6

Nếu bảng thực sự, thực sự, tồn tại trong mySQL, và sử dụng Linux/Unix của bạn, và lỗi hiển thị tên bảng trong trường hợp sai/trên, thì vấn đề là tên bảng trong MySQL là trường hợp nhạy cảm và hibernate là trên vỏ chúng.Tôi đang sử dụng hibernate 4.3 .

Tôi vừa gặp sự cố này. Giải thích tại đây: lower_case_table_names=1

--edit-- Nhìn lại, có thể tìm và thay đổi bất kỳ tham chiếu @Table hoặc hbm.xml nào phù hợp với cơ sở dữ liệu. Tôi đã chạy một thuật sĩ đã tạo ra một hbm.xml với các tên viết hoa - đã không nhận ra rằng nó nằm trong dự án của tôi cho đến bây giờ. Tôi sẽ để lại điều này ở đây để làm cho mọi người nhận thức được trường hợp nhạy cảm.

--end của edit--

Dưới đây là cách tôi cố định nó:

  1. Drop cơ sở dữ liệu.
  2. Thêm phần này vào /etc/mysql/my.conf:

    set lower_case_table_names=1 #(default value '0'). 
    
  3. Khởi động lại mysqld.
  4. Tạo lại cơ sở dữ liệu.
  5. (tùy chọn?) Thay đổi tham chiếu bảng chú thích/hbm.xml thành chữ thường.
+0

Các bước 1-4 không giúp được gì. – Polyakoff

+0

Tôi đã có truy vấn với tablenames chữ hoa và tôi luôn nhận được bảng thông báo không tồn tại. Cảm ơn bạn đã gợi ý chữ thường của bạn. Tất cả mọi thứ hoạt động trơn tru ngay bây giờ! :) – PrestigeDev

1

Chúng tôi cũng gặp phải vấn đề tương tự. Có một truy vấn SQL mà không vượt qua với một lỗi như

com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Bảng 'my_database_name.* 'không tồn tại

Nhưng bản thân truy vấn không chứa tham chiếu my_database_name hoặc thậm chí * dấu.

So sánh với các truy vấn khác, chúng tôi nhận thấy sự khác biệt và được thêm vào ORDER BY để truy vấn và lỗi biến mất. Có thể là một hack xung quanh jdbc hoặc c3p0 logic, nhưng nó đã làm việc cho chúng tôi.

0

Trong trường hợp của tôi, lý do là vì bên ngoài bên trái tham gia vào truy vấn con với bí danh, đang làm việc trên trình soạn thảo sql chứ không phải trên JDBCspring, vì vậy tôi đã loại bỏ phần bên ngoài bên trái của truy vấn phụ và thay thế nó bằng bên trái bên trái mà không có truy vấn phụ

2

Sau khi loại trừ từng lần xuất hiện bảng XYZ trong mã của tôi, tôi đã tìm thấy vấn đề thực tế: XYZ không được tham chiếu bởi JPA, mà bởi trình kích hoạt mysql cũ, không hợp lệ. Có thể xem xét tìm lỗi bên ngoài mã của bạn.

1

Tôi đã có cùng một vấn đề, mysql db của tôi đã được trên cửa sổ nhưng tôi đã chuyển nó sang linux dẫn đến cú pháp mysql không nhận ra bảng. Nguyên nhân là mysql trên cửa sổ là trường hợp nhạy cảm và trường hợp nhạy cảm trên linux tôi đã có thể giải quyết này thêm tôi

lower_case_table_names = 1

trong my.cnf.

Ngoài ra hãy chắc chắn bao gồm

[mysqld]

vào đầu my.cnf để tránh lỗi khác

"file my.cnf MySQL - Tìm thấy tùy chọn mà không cần nhóm trước"

-1

có lỗi tương tự trên mã của tôi và tôi đã thay đổi tệp kiên trì

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

thay thế "tạo-thả" với "cập nhật"

nhờ

Các vấn đề liên quan