2

I am trying to use spring-data-rest with MySql with hibernate as the JPA provider.

I want the table names used in the query to match the simple name of the class; Nothing more. How do I achieve that?

I have tried all of the naming strategies and it seems to have no effect. In addition, I have added

@Table("Foo")

And it gets lowercased as well

Here is the error:

com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Table 'TEST.worker' doesn't exist

Here is the entity:

@Entity
public class Worker {
    private Long id;
    private String givenName;
    private String familyName;
    private LocalDate dob;
    private String nationalId;
    private byte[] photo;

    public Worker() {
        this.id = Math.abs(new Random().nextLong());
    }

    @Id
    @Column(name = "ID")
    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    @Basic
    @Column(name = "GivenName")
    public String getGivenName() {
        return givenName;
    }

    public void setGivenName(String givenName) {
        this.givenName = givenName;
    }

    @Basic
    @Column(name = "FamilyName")
    public String getFamilyName() {
        return familyName;
    }

    public void setFamilyName(String familyName) {
        this.familyName = familyName;
    }

    @Basic
    @Column(name = "DOB")
    public LocalDate getDob() {
        return dob;
    }

    public void setDob(LocalDate dob) {
        this.dob = dob;
    }

    @Basic
    @Column(name = "NationalID")
    public String getNationalId() {
        return nationalId;
    }

    public void setNationalId(String nationalId) {
        this.nationalId = nationalId;
    }

    @Basic
    @Column(name = "photo")
    public byte[] getPhoto() {
        return photo;
    }

    public void setPhoto(byte[] photo) {
        this.photo = photo;
    }
}

My relevant config:

spring.jpa.database-platform=org.hibernate.dialect.MySQL5Dialect
spring.jpa.show-sql=true
spring.jpa.generate-ddl = false
spring.jpa.hibernate.ddl-auto = none
spring.jpa.hibernate.naming-strategy=org.springframework.boot.orm.jpa.hibernate.SpringNamingStrategy

Transitive dependencies from spring data:

[INFO] |  +- org.hibernate:hibernate-entitymanager:jar:4.3.11.Final:compile
[INFO] |  |  +- org.hibernate:hibernate-core:jar:4.3.11.Final:compile
[INFO] |  |  +- org.hibernate.common:hibernate-commons-annotations:jar:4.0.5.Final:compile
[INFO] |  |  \- org.hibernate.javax.persistence:hibernate-jpa-2.1-api:jar:1.0.0.Final:compile

This link doesn't apply;

Community
  • 1
  • 1
Christian Bongiorno
  • 5,150
  • 3
  • 38
  • 76
  • Do either of the solutions provided in [this question](http://stackoverflow.com/questions/28571848/spring-boot-jpa-insert-in-table-with-uppercase-name) work for you? – Chatoyancy Mar 31 '16 at 00:40
  • Which Hibernate version do you use? – v.ladynev Mar 31 '16 at 06:44
  • @Chatoyancy Yes, actually. I inadvertently stumbled on it myself but your link(s) work. Provided it as an answer if you want. I basically backtracked my way up the stack to figure out how it was coming to that table name and used some auto complete from intellij to get there. I didn't find it sooner because it seems I was asking the wrong question and it was leading me to answers related to the OS. – Christian Bongiorno Apr 01 '16 at 17:45
  • Check [this](http://stackoverflow.com/questions/6134006/are-table-names-in-mysql-case-sensitive) post. It could be helpful. – K.Nicholas Mar 31 '16 at 00:32

1 Answers1

2

Since you are using SpringNamingStrategy it uses the same values as the ImprovedNamingStrategy from Hibernate (verify it here).

If you check the link for ImprovedNamingStrategy you will see that the default behavior for tableName is to lowercase any mixed case names, which seems to be your case.

dambros
  • 4,252
  • 1
  • 23
  • 39
  • Yeah, I discovered that and Changed it to the EJB strategy. Thanks! – Christian Bongiorno Apr 01 '16 at 19:08
  • You can also extends the strategy and simply change this behavior only, if EJB strategy doesn't fit your needs. – dambros Apr 01 '16 at 19:10
  • I had the same problem. Using spring.jpa.hibernate.naming.physical-strategy=org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl as described in http://stackoverflow.com/questions/28571848/spring-boot-jpa-insert-in-table-with-uppercase-name-with-hibernate solved the problem – 5im May 20 '17 at 02:41