39

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> {}
Wojciech Wirzbicki
  • 3,887
  • 6
  • 36
  • 59
KenavR
  • 3,769
  • 9
  • 37
  • 47
  • Check this http://stackoverflow.com/questions/2536829/hibernate-show-real-sql – Sushant Tambare Feb 16 '15 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 Feb 16 '15 at 15:42
  • 2
    Your problem may be related with this one: [spring-boot-jpa-column-name-annotation-ignored](http://stackoverflow.com/questions/25283198/spring-boot-jpa-column-name-annotation-ignored) – David SN Feb 16 '15 at 15:49
  • 1
    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 Feb 16 '15 at 15:57

4 Answers4

55

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
Mohammed Rafeeq
  • 2,586
  • 25
  • 26
  • 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 Oct 12 '20 at 12:48
  • This solved my problem for spring boot security not loging in. Thank you. – Hrvoje T Oct 06 '21 at 20:30
28

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
Community
  • 1
  • 1
David SN
  • 3,389
  • 1
  • 17
  • 21
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

Shuchita Bora
  • 257
  • 2
  • 5
3

use below in application.properties

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

Vivek Goel
  • 762
  • 2
  • 10
  • 21