Henrique Ordine Henrique Ordine - 24 days ago 21
MySQL Question

Spring Data doesn't seem to understand @Column name

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;

Answer

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
Comments