Angga Saputra
Angga Saputra

Reputation: 211

How to Deal JPA / JPQL with SQL Server Keyword

My Entity class

@Entity
class MasterStccycode{
   private static final long serialVersionUID = 1L;
   @Id
   @Basic(optional = false)
   @NotNull
   @Size(min = 1, max = 3)
   @Column(name = "CODE")
   private String code;

   @Size(max = 100)
   @Column(name = "DESC")
   private String desc;
}

my JPA Query SELECT t.code, t.desc FROM MasterStccycode t

then I have this following exception

Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.3.0.v20110604-r9504): org.eclipse.persistence.exceptions.DatabaseException

Internal Exception: com.microsoft.sqlserver.jdbc.SQLServerException: Incorrect syntax near the keyword 'DESC'.

Error Code: 156

Call: SELECT CODE, DESC FROM master_stccycode Query: ReportQuery(referenceClass=MasterStccycode sql="SELECT CODE, DESC FROM master_stccycode")

I know the solution is to wrap the DESC keyword with [] into [DESC] but how can I do this on JPA QL?

Upvotes: 0

Views: 1162

Answers (1)

James
James

Reputation: 18379

DESC is a reserved word on most databases. You should rename the field.

You could also quote the field, but just renaming it would be best.

@Column(name = "\"DESC\"")

Upvotes: 2

Related Questions