1

I use jpa to persist an object to mysql databse.

When I define a column named "index" like below, the table will not be generated in database.

When I delete it or rename it, the jpa works fine.

import java.util.Date;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
import play.db.jpa.GenericModel;

@Entity
@Table(name="QA_ASSETVM")
public class QAAssetVM extends GenericModel{

     private static final long serialVersionUID = 1L;

     @Id 
     @Column(name="vmassetname")
     public String vmassetname;

     @Column(name="assetvm_index")  //this works fine!
     public int index;
     /*
     @Column(name="index")  //When I add this, the table "QA_ASSETVM" will not be generated by jpa in database.
     public int index;
     */
}
Dong
  • 146
  • 1
  • 10

3 Answers3

3

As Hannibal pointed out, index is a reserved keyword in MySQL therefore you cannot use it directly as a column name, but you have to escape it.

  1. If you are using Hibernate 3.5+, try with hibernate.globally_quoted_identifiers=true, which will quote all database indentifiers (source: Automatic reserved word escaping for Hibernate tables and columns)
  2. In JPA 2.0, you can use the annotation: @Column(name="\"index\""), which is the new standardize way to escape a single column
Community
  • 1
  • 1
ThanksForAllTheFish
  • 7,101
  • 5
  • 35
  • 54
2

I haven't tried using mysql, but 'index' seems to be a reserved word.

check this out.

Aaron Digulla
  • 321,842
  • 108
  • 597
  • 820
Hannibal
  • 48
  • 5
1

Because index is the reserved word. In order to use index as a column name you have to use write it within []. like this :

@Column(name="[index]")
Akshay Pethani
  • 2,390
  • 2
  • 29
  • 42