How to use Space in column name with JPA and Hibernate

I used in my Java DAO class which contains a space between LANG PREF

 @Column(name = "LANG PREF")
  private String langprefid;

      

As soon as my java class starts, I get

org.hibernate.exception.SQLGrammarException: Incorrect syntax near the keyword 'as'.

Can anyone help me with this problem?

+4


source to share


3 answers


Escaping manually reserved keywords

If you are using the Hibernate native API, you can avoid them with back checkboxes:

@Column(name = "'LANG PREF'")

      

If you are using JPA you can avoid double quotes:

@Column(name = "\"LANG PREF\"")

      



Automatically escaping reserved keywords

If you want to automatically trim reserved keywords, you can set a value true

for the configuration property hibernate.globally_quoted_identifiers

specific to hibernate.globally_quoted_identifiers

:

<property
    name="hibernate.globally_quoted_identifiers"
    value=true"
/>

      

For more details, check out this article .

+8


source


Change the column name to LANG_PREF

then use



@Column(name = "LANG_PREF")
  private String langprefid;

      

0


source


You can change your code to

import javax.persistence.Column;
@Column(name = "LANG_PREF")
private String langprefid;

      

0


source







All Articles