-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 7 posts ] 
Author Message
 Post subject: How to escape keywords with JPA
PostPosted: Thu Mar 16, 2017 11:54 pm 
Newbie

Joined: Thu Mar 16, 2017 11:46 pm
Posts: 4
im use hibernate and MySQL,when i use 'describe' as column name.my code return:

org.hibernate.exception.SQLGrammarException: could not execute statement

java.sql.SQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'describe='for test', islocked=0, permission_code='add,edit', role_name='test_rol' at line 1


Top
 Profile  
 
 Post subject: Re: how to avoid some word can lead Exception
PostPosted: Fri Mar 17, 2017 2:15 am 
Hibernate Team
Hibernate Team

Joined: Thu Sep 11, 2014 2:50 am
Posts: 1628
Location: Romania
You need to escape your column name as follows:

Code:
@Column(name ="\"describe"\")
private String describe;


For more details, check out this article.


Top
 Profile  
 
 Post subject: Re: how to avoid some word can lead Exception
PostPosted: Fri Mar 17, 2017 2:23 am 
Newbie

Joined: Thu Mar 16, 2017 11:46 pm
Posts: 4
vlad wrote:
You need to escape your column name as follows:

Code:
@Column(name ="\"describe"\")
private String describe;



thanks


Top
 Profile  
 
 Post subject: Re: how to avoid some word can lead Exception
PostPosted: Tue Aug 22, 2017 5:00 am 
Newbie

Joined: Thu Mar 16, 2017 11:46 pm
Posts: 4
vlad wrote:
You need to escape your column name as follows:

Code:
@Column(name ="\"describe"\")
private String describe;


sorry,i did not test this method just remember it.But i encountered the same problem.then i try add \" like this name ="\"describe\"".it is invalid.

@Basic
@Column(name = "\"describe\"")
public String getDescribe() {
return describe;
}

public void setDescribe(String describe) {
this.describe = describe;
}

if you find the cause for my code. tell me please!thanks a lot!


Top
 Profile  
 
 Post subject: Re: How to escape keywords with JPA
PostPosted: Wed Aug 23, 2017 5:24 am 
Hibernate Team
Hibernate Team

Joined: Thu Sep 11, 2014 2:50 am
Posts: 1628
Location: Romania
Not only that it works, but it works like charm. Check out this test case on GitHub and run it yourself if you don't believe me.

Code:
@Entity(name = "Table")
@javax.persistence.Table(name = "\"table\"")
public static class Table {

   @Id
   private Long id;

   @Column(name = "\"number\"")
   private Integer number;

   @Column(name = "\"from\"")
   private String from;

   @Column(name = "\"select\"")
   private String select;
}


And why wouldn't it work? Its' standard JPA:

Quote:
Using annotations, a name is specified as a delimited identifier by enclosing the name
within double quotes, whereby the inner quotes are escaped, e.g.,
@Table(name="\"customer\"").


Top
 Profile  
 
 Post subject: Re: How to escape keywords with JPA
PostPosted: Wed Aug 23, 2017 5:45 am 
Newbie

Joined: Thu Mar 16, 2017 11:46 pm
Posts: 4
vlad wrote:
Not only that it works, but it works like charm. Check out this test case on GitHub and run it yourself if you don't believe me.

Code:
@Entity(name = "Table")
@javax.persistence.Table(name = "\"table\"")
public static class Table {

   @Id
   private Long id;

   @Column(name = "\"number\"")
   private Integer number;

   @Column(name = "\"from\"")
   private String from;

   @Column(name = "\"select\"")
   private String select;
}


And why wouldn't it work? Its' standard JPA:

Quote:
Using annotations, a name is specified as a delimited identifier by enclosing the name
within double quotes, whereby the inner quotes are escaped, e.g.,
@Table(name="\"customer\"").


i use intellij idea persistence generate persistence mapping.
Code:
@Basic
    @Column(name = "process_describe")
    public String getProcessDescribe() {
        return processDescribe;
    }

    public void setProcessDescribe(String processDescribe) {
        this.processDescribe = processDescribe;
    }

if i use 'describe' named my column.it will error.And i can't resolve it used \". but i just rename 'process_describe'.it will success without '\"'. i think trouble is in 'describe'. Probably the problem is beyond my understanding of hibernate.I will record this problem until I can solve him.Later I will go to reading your code on github.Thank you for your reply


Top
 Profile  
 
 Post subject: Re: How to escape keywords with JPA
PostPosted: Wed Aug 23, 2017 5:56 am 
Hibernate Team
Hibernate Team

Joined: Thu Sep 11, 2014 2:50 am
Posts: 1628
Location: Romania
I tested it using Hibernate 5.2.10. Maybe you are running an older version which didn't support the JPA escape syntax, in which case you can use the Hibernate one using the ` character:

@Column(name ="`describe`")
private String describe;

or you can set the global escape behavior:

Code:
<property name="hibernate.globally_quoted_identifiers">true</property>


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 7 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.