Hibernate查询生成器抛出MySQLSyntaxErrorException

时间:2015-03-27 01:31:24

标签: java mysql hibernate

我正在使用 Hibernate ,并且查询生成器似乎在MySQL中抛出语法错误(exceptions.jdbc4.MySQLSyntaxErrorException)。

我已经包含了相关文件:

hibernate1.cfg.xml

<?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">org.gjt.mm.mysql.Driver</property>
<property name="hibernate.connection.password">65065827</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost/test</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.default_schema">vincent</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</property>
<property name="hibernate.current_session_context_class">thread</property>
<property name="hibernate.show_sql">true</property>
<property name="hibernate.jdbc.use_streams_for_binary">false</property>


<mapping class="pa.Tesdb" />

</session-factory>

hibernateUtil.java (由JBOSS TOOLS生成)

package pa;

import org.hibernate.*;
import org.hibernate.cfg.*;

public class HibernateUtil {
  private static final SessionFactory sessionFactory = new AnnotationConfiguration()
      .configure("/pa/hibernate1.cfg.xml").buildSessionFactory();

  public static SessionFactory getSessionFactory() {
    return sessionFactory;
  }
}

**tesdb.java** ( tesdb.java was generated by JBOSS TOOLS )

package pa;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;

/**
* Tesdb generated by hbm2java
*/
@Entity
@Table(name = "tesdb", catalog = "test")
public class Tesdb implements java.io.Serializable {

  private int idtesdb;
  private String tesdbcol;

  public Tesdb() {
  }

  public Tesdb(int idtesdb) {
    this.idtesdb = idtesdb;
  }

  public Tesdb(int idtesdb, String tesdbcol) {
    this.idtesdb = idtesdb;
    this.tesdbcol = tesdbcol;
  }

  @Id
  @Column(name = "idtesdb", unique = true, nullable = false)
  public int getIdtesdb() {
    return this.idtesdb;
  }

  public void setIdtesdb(int idtesdb) {
    this.idtesdb = idtesdb;
  }

  @Column(name = "tesdbcol", length = 45)
  public String getTesdbcol() {
    return this.tesdbcol;
  }

  public void setTesdbcol(String tesdbcol) {
    this.tesdbcol = tesdbcol;
  }

}

Testingg.java

package pa;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.persistence.Query;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.transaction.UserTransaction;

import org.hibernate.Session;
import org.hibernate.Transaction;

import pa.HibernateUtil;
import pa.Tesdb;

/**
* Servlet implementation class Testing
*/
@WebServlet("/Testing")
public class Testingg extends HttpServlet {
  private static final long serialVersionUID = 1L;

/**
* @see HttpServlet#HttpServlet()
*/
public Testingg() {
super();
// TODO Auto-generated constructor stub
}

  /**
   * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
   */
  protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    // TODO Auto-generated method stub
    response.setContentType("text/html");
   Session session = HibernateUtil.getSessionFactory().getCurrentSession();
Transaction Tx = session.beginTransaction();
    List intro = session.createQuery("FROM Tesdb").list();
   PrintWriter out = response.getWriter();
   out.println( "Run End"  );
   out.println( intro.size() );
Tx.commit();
  }

  /**
   * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
   */
  protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    // TODO Auto-generated method stub
  }

}

数据库

Table: tesdb
Columns:
idtesdb int(11) PK
tesdbcol varchar(45)

通过hibernate生成查询

Hibernate: 
    select 
        tesdb0_.idtesdb as idtesdb1_0_, 
        tesdb0_.tesdbcol as tesdbcol2_0_ 
    from test.vincent.tesdb tesdb0_

错误消息

com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '.tesdb tesdb0_' at line 1
  sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
  sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
  sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
..... 
你可以帮助我吗?

1 个答案:

答案 0 :(得分:0)

你的类的JBOSS TOOLS生成器在这里是MySQL does not support catalog

修改您的hibernateUtil类并删除catalog属性,此shoudl会删除生成的查询中表名前面的额外test.