2

I'm working on a Spring Boot application using Hibernate and Lombok. I have an entity class Product mapped to a product table in my database. Here's the relevant part of the code:

@Entity
@Table(name = "product")
@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class Product {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "ProductId")
    private int id;
    // Other fields...
}

When I run my application, I get the following error:

ERROR o.h.engine.jdbc.spi.SqlExceptionHelper : Unknown column 'p1_0.product_id' in 'field list'

It seems like Hibernate is converting the camel-case field name 'ProductId' in my entity class to the underscore-separated column name 'product_id' in the database, causing a mismatch.

I tried changing the @Column annotation to match the actual column name in the database, but it didn't solve the issue. I also checked my database schema and it does have a 'ProductId' column.

Is there a way to change the Hibernate naming strategy so that it doesn't automatically convert the field names? I want the names in my entity class to directly map to the column names in the database without any modifications.

0

2 Answers 2

2

Hibernate Naming Strategy: Hibernate by default converts the camel-case field names in the entity class to underscore-separated column names in the database (java.sql.SQLSyntaxErrorException: Unknown column in 'field list'). This means that 'ProductId' in the entity class would be mapped to 'product_id' in the database, which might be causing the issue. You can change Hibernate's naming strategy to avoid this automatic conversion.

spring.jpa.hibernate.naming.physical-strategy=org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl This should be added to your application.properties file to change the naming strategy (What am I mapping incorrectly? Unknown column in the 'field list').

Sign up to request clarification or add additional context in comments.

Comments

0

Mysql is case insensitive. If you use case sensitive column name then it will convert the camel case to the snake case.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.