41

I use JPA for database access and annotated every column with the correct name. Now if I execute a query (e.g. findAll()) it returns

Unknown column 'program0_.program_id' in 'field list'

The error message is correct program_id is unknown because the real name is programId.

Models: Program

  @Entity
  @Table(name = "programs")
  @XmlRootElement
  public class Program implements Serializable {
          @Id
          @GeneratedValue(strategy = GenerationType.IDENTITY)
          @Basic(optional = false)
          @Column(name = "programId")
          private Long programId;
          @ManyToMany
          @JoinTable(
                  name = "programlabels",
                  joinColumns = {
                    @JoinColumn(name = "program", referencedColumnName = "programId")},
                  inverseJoinColumns = {
                    @JoinColumn(name = "label", referencedColumnName = "labelId")})
          private Collection<Label> labels;
        }

Label

@Entity
@Table(name = "labels")
@XmlRootElement
public class Label implements Serializable {
  @Id
  @Basic(optional = false)
  @NotNull
  @Size(min = 1, max = 100)
  @Column(name = "labelId")
  private String labelId;  
}

Query

select program0_.program_id as program_1_5_, ...

Is there a reason why JPA changes "programId" to "program_id" or am I missing any configuration?

thanks

Edit: Oh sorry forgot to add query code/information.

I use the Spring Data's JpaRepository interface and tried the findAll() query.

@Repository
public interface ProgramRepository extends JpaRepository<Program, Long> {}
4
  • Check this stackoverflow.com/questions/2536829/hibernate-show-real-sql Commented Feb 16, 2015 at 15:35
  • I may be wrong, but I think that doesn't help me, because the problem is that JPA uses the wrong column name, regardless of what I see in the console. What I have seen the column names are the same in both outputs.
    – KenavR
    Commented Feb 16, 2015 at 15:42
  • 3
    Your problem may be related with this one: spring-boot-jpa-column-name-annotation-ignored
    – David SN
    Commented Feb 16, 2015 at 15:49
  • 2
    Thanks that is the same issue. I added a naming strategy to the configuration and now it works. If you want to add it as answer you get some useless internet points.
    – KenavR
    Commented Feb 16, 2015 at 15:57

4 Answers 4

58

http://docs.spring.io/spring-boot/docs/current/reference/html/common-application-properties.html

spring.jpa.hibernate.naming.strategy is not a supported property for Spring JPA implementation using Hibernate 5.

Use the below property in application.properties

spring.jpa.hibernate.naming.physical-strategy=org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl
2
  • For me, my cameCase properties in my entity class were automatically getting generated to snake case columns of defined properties in DB table. I wanted it camelCase mapping in DB column as well. This solution worked for me perfectly without much changes anywhere.
    – vinsinraw
    Commented Oct 12, 2020 at 12:48
  • This solved my problem for spring boot security not loging in. Thank you.
    – Hrvoje T
    Commented Oct 6, 2021 at 20:30
31

As described in spring-boot-jpa-column-name-annotation-ignored, your column name is being converted to snake case.

Possible solutions:

  • Setup a Naming Strategy
  • Use lowercase column names in your annotations
0
10

Were able to map

@Column(name = "PersonFullName")

private String PersonFullName;


to the database table column name "PersonFullName" without the underscore.

The below worked for me. Add this in the application settings and then use @Column to specify the physical database column name for the model's property.

@Column(name = "PersonFullName")

In Application.properties

spring.jpa.hibernate.naming.implicit-strategy=org.hibernate.boot.model.naming.ImplicitNamingStrategyLegacyJpaImpl spring.jpa.hibernate.naming.physical-strategy=org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl

4

use below in application.properties

spring.jpa.hibernate.naming.physical-strategy=org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.