2

I'm using Spring-boot 1.4.1.RELEASE, with Spring Data and Hibernate to persist some data into a MySQL database.

I have this class, Respondent, annotated with @Entity and one of the fields annotated as below:

@Column(name = "firstName", nullable = false, length = 100)
private String firstName;

When I try to save a Respondent to the DB by calling save() on its CrudRepository<Respondent, Long>, I get this error:

ERRORcom.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Unknown column 'first_name' in 'field list'

This error had started occurring before I had the @Column annotation for the field, so I thought it was some default Hibernate behaviour to map firstName to first_name, but I've added the @Column annotation to it and nothing changed. Is it still wrong? I've already rebuilt the application with mvn clean package.

Here's my Respondent entity:

@Entity
public class Respondent {

    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    private Long id;
    @Column(name = "firstName", nullable = false, length = 100)
    private String firstName;
    private String tussenvoegsel;
    @Column(name = "lastName", nullable = false, length = 100)
    private String lastName;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "Company_id", nullable = false)
    private Company company;
Henrique Ordine
  • 3,337
  • 4
  • 44
  • 70
  • are you sure that the column "firstName" exists? also, usually i neve use any declared filed in the @entity class that is not annotated, like "private String tussenvoegsel;" – RudiDudi Nov 08 '16 at 14:00
  • 100% sure. It's first_name that doesn't exist and to which somehow hibernate is still mapping my field. – Henrique Ordine Nov 08 '16 at 14:14
  • try to change `@Column(name = "firstName"` to lowercase `@Column(name = "firstname"`. – Patrick Nov 08 '16 at 14:15
  • 1
    seems you have a ImproveNamingStrategy set in your configuration http://stackoverflow.com/questions/376093/hibernate-column-name-issues. Could you post your hibernate configuration class? – amicoderozer Nov 08 '16 at 14:20

2 Answers2

7

By default Spring uses jpa.SpringNamingStrategy to generate the table names.

The ImprovedNamingStrategy will convert CamelCase to SNAKE_CASE where as the EJB3NamingStrategy just uses the table name unchanged.

You can try to change the naming_strategy to:

spring.jpa.hibernate.naming_strategy=org.hibernate.cfg.EJB3NamingStrategy

or the @Column name attribute should be in lowercase @Column(name = "firstname")


For Hibernate 5 this should work (I am not quite sure if you also need the above one. But try it with both):

spring.jpa.hibernate.naming.implicit-strategy=org.hibernate.boot.model.naming.ImplicitNamingStrategyLegacyJpaImpl
spring.jpa.hibernate.naming.physical-strategy=org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl
Patrick
  • 12,336
  • 15
  • 73
  • 115
  • Thanks, I tried setting it to my application.properties, but the error persists. It seems there's a problem with that naming strategy in hibernate 5, which is what I'm using: https://github.com/spring-projects/spring-boot/issues/2763 – Henrique Ordine Nov 08 '16 at 15:14
  • @HenriqueOrdine updated the answer for hibernate 5 properties. Try it out – Patrick Nov 08 '16 at 15:23
0

There we need to follow some naming strategy. The column name should be in lowercase or uppercase.

 @Column(name="FIRSTNAME")
    private String firstName;

or

@Column(name="firstname")
private String firstName;

keep in mind that, if you have your column name "first_name" format in the database then you have to follow the following way

@Column(name="firstName")
private String testName;
naib khan
  • 928
  • 9
  • 16