Search code examples
javahibernategrailsindices

How to use Hibernate @Index annotation properly?


i have a java class used as an entity that has 2 classes that inherit from it. this class has some indices but these indices didn't appear in the database. this is my java super class code

import java.io.Serializable;
import java.util.List;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.UniqueConstraint;
import javax.persistence.Version;
import javax.persistence.OneToMany;
import org.hibernate.annotations.GenericGenerator;

@Entity
@Table(name="service", uniqueConstraints = {@UniqueConstraint(columnNames={"name"})})
@org.hibernate.annotations.Table(appliesTo = "service", 
                                 indexes = { @Index(name = "service_name", columnNames = { "name" }), 
                                 @Index(name = "service_description", columnNames = { "description" }),
                                 @Index(name = "service_accessNumber", columnNames = { "access_number" })   
                     })
public class Service implements Serializable {

@Column(name="access_number",length = 95,nullable=false)
    String accessNumber;

@Column(length=80,nullable=false)
    String name;

@Column(length=140)
    String description;

}

does any one know what is my problem Note: i have this problem in my all java classes but this is one of them. the code in all class is the same of this

Edit: i build an xml file and put it in a grails project, and when i run this project, database created


Solution

  • i have the same problem, but i found it's solution and it works fine with me try it, it may help you

    in your DataSource.groovy file in your grails project make sure that under 
    environment dbCreate is not equal to "update": if it is equal to "update", change 
    it to "create".
    

    This works fine just try it