简体   繁体   中英

How to use Space in column name with JPA and Hibernate

I have used in my java DAO class which contains Space in between LANG PREF

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

Once my java class runs, I am getting

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

Can anyone help me regarding this problem?

Manually escaping the reserved keywords

If you're using Hibernate native API, then you can escape them using backticks:

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

If you are using JPA, you can escape with double quotes:

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

Automatically escaping reserved keywords

If you want to automatically scape reserved keywords, you can set to true the Hibernate-specific hibernate.globally_quoted_identifiers configuration property:

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

Change your Column name to LANG_PREF then use

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

You can change your code to

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM