5

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

2 Answers2

2

Would a single @Table annotation work? I haven't tried it, I guess the Hibernate @Table might be overridden by JPA @Table.

You may also try @Index annotation on the column fields:

public class Service implements Serializable {
    @Index(name="service_accessnumber")
    @Column(name="access_number",length = 95,nullable=false)
    String accessNumber;

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

    @Index(name="service_description")
    @Column(length=140)
    String description;
}
Lenik
  • 13,946
  • 17
  • 75
  • 103
  • Did hibernate try to recreate the tables? I.e., have you set hibernate.hbm2ddl.auto = create-drop? – Lenik Jul 14 '11 at 08:29
  • i remove the database and apply create-drop, but the problem didn't solve. there is no indexes in the database appears –  Jul 14 '11 at 08:56
  • I'm using PostgreSQL, I'm sure this work for me. As Service is a superclass in your case, what's @Inheritance strategy do you use? A mapped-superclass won't work because only one index for each index-name will be created. See also [How to auto generate names for @Index annotation](http://stackoverflow.com/questions/6290495). – Lenik Jul 14 '11 at 09:06
  • And, have you tried to remove all the `@Table` annotations? Maybe hibernate ignored all @Index-es when @Table exists? The unique constraint may be specified by annotate @NaturalId on the name column. – Lenik Jul 14 '11 at 09:08
  • i try to remove @Table annotations but the problem didn't solve –  Jul 15 '11 at 06:34
  • Can you post the hibernate output logs? Or the related source code in github? – Lenik Jul 15 '11 at 23:03
  • i build an xml file and put it in a grails project, and when i run this project, database created –  Jul 18 '11 at 07:44
  • I couldn't see what's wrong without further information. But the annotations you used should be correct. – Lenik Jul 18 '11 at 08:04
  • i know that, and all references i refer to, used the same annotations, but i don't know what is the problem. i have also a spring project used this database. i think that the problem is with grails project, i try to run the spring project to create the database, do you know the method i can use it (or script) to auto create the database when running spring project –  Jul 18 '11 at 08:09
  • btw, regardless of the question, It still works for me to put the 2 @Table annotation (i wanted unique constraints and indices on multiple columns). – Eyad Ebrahim May 15 '14 at 18:20
1

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

Tomislav Nakic-Alfirevic
  • 10,017
  • 5
  • 38
  • 51
Georgian Citizen
  • 3,727
  • 6
  • 38
  • 46