不成功:使用hibernate 2创建表

时间:2012-09-22 19:10:08

标签: mysql hibernate syntax-error keyword create-table

当hinernate尝试将此类映射到MySQL数据库时

/*
*To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package projekat.entities;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import javax.persistence.*;
import org.apache.tapestry5.beaneditor.NonVisual;

/**
 *
 * @author nikola
 */
@Entity
public class Student implements Serializable {

  @Id
  @NonVisual
  @GeneratedValue(strategy = GenerationType.AUTO)
  @Basic(optional = false)
  private Long kljuc;
  @Basic(optional = false)
  private String ime;
  @Basic(optional = false)
  private String prezime;
  @Basic(optional = false)
  private Integer index;
  @ManyToMany
  private List<Grupa> grupaList = new ArrayList<Grupa>();

  public NewClass() {
  }

  public Long getKljuc() {
    return kljuc;
  }

  public void setKljuc(Long kljuc) {
    this.kljuc = kljuc;
  }

  public String getIme() {
    return ime;
  }

  public void setIme(String ime) {
    this.ime = ime;
  }

  public String getPrezime() {
    return prezime;
  }

  public void setPrezime(String prezime) {
    this.prezime = prezime;
  }

  public Integer getIndex() {
    return index;
  }

  public void setIndex(Integer index) {
    this.index = index;
  }

  public List<Grupa> getGrupaList() {
    return grupaList;
  }

  public void setGrupaList(List<Grupa> grupaList) {
    this.grupaList = grupaList;
  }

  @Override
  public int hashCode() {
    int hash = 5;
    hash = 83 * hash + Objects.hashCode(this.kljuc);
    hash = 83 * hash + Objects.hashCode(this.ime);
    hash = 83 * hash + Objects.hashCode(this.prezime);
    hash = 83 * hash + Objects.hashCode(this.index);
    return hash;
  }

  @Override
  public boolean equals(Object obj) {
    if (obj == null) {
      return false;
    }
    if (getClass() != obj.getClass()) {
      return false;
    }
    final Student other = (Student) obj;
    if (!Objects.equals(this.kljuc, other.kljuc)) {
      return false;
    }
    if (!Objects.equals(this.ime, other.ime)) {
      return false;
    }
    if (!Objects.equals(this.prezime, other.prezime)) {
      return false;
    }
    if (!Objects.equals(this.index, other.index)) {
      return false;
    }
    return true;
  }

  @Override
  public String toString() {
    return "Student{" + "ime=" + ime + ", prezime=" + prezime + ", index=" + index + '}';
  }
}

以下输出显示在tomcat log

Unsuccessful: create table Student (kljuc bigint not null auto_increment, ime varchar(255) not null, index integer not null, prezime varchar(255) not null, primary key (kljuc))
20:03:30,540 ERROR SchemaExport:387 - 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 'integer not null, prezime varchar(255) not null, primary key (kljuc))' at line 1

1 个答案:

答案 0 :(得分:7)

结果错误出现在类字段索引中,这是MySQL中的关键字。 我被困在这至少2天了。重新安装MySQL服务器,IDE,搜索hibernate文档等......

  @Basic(optional = false)
  private Integer index;
//to
  @Basic(optional = false)
  private Integer indeks;

而我只是为了帮助我和其他有同样问题的人。