Results 1 to 6 of 6

Thread: Quote problem with Spring+Hibernate

  1. #1
    Join Date
    Aug 2004
    Location
    Liège, Belgique
    Posts
    47

    Default Quote problem with Spring+Hibernate

    I am using Spring and Hibernate to persist my objects and it works fine except when I have single quotes in some fields of some objects.

    I know that single quotes are special caracters for SQL and they must be "quoted". But I thought that Hibernate will be clever enough to do that sort of things.

    It seems it's not the case so what is the best way to do this ?

    I don't want to modify the setters and getters of these fields to do the conversion.

    Thanks in advance.

  2. #2
    Join Date
    Jan 2005
    Location
    Bucharest, Romania
    Posts
    5,403

    Default

    You need to escape the fields, see: http://www.hibernate.org/hib_docs/re...queryinterface
    Costin Leau
    SpringSource - http://www.SpringSource.com- Spring Training, Consulting, and Support - "From the Source"
    http://twitter.com/costinl
    Please use [ c o d e ] [ / c o d e ] tags

  3. #3
    Join Date
    Aug 2004
    Location
    Melbourne, Australia
    Posts
    1,104

    Default

    Hibernate should handle single quotes as long as you use parameters. Take a look at the link posted above and make sure you're not doing something like:
    Code:
    String query = "from Skill where skillName = '"+skillname+"'";
    If there is still a problem can you post the relevant code.

  4. #4
    Join Date
    Jan 2005
    Location
    Bucharest, Romania
    Posts
    5,403

    Default

    One problem with escaping the String yourself is that, it's possible to do SQL injection, meaning craft such a string thay you break the escaping. In Hibernate In Action I think there are some mention about it, but in short, use named parameters as much as possible; it's safer.
    Costin Leau
    SpringSource - http://www.SpringSource.com- Spring Training, Consulting, and Support - "From the Source"
    http://twitter.com/costinl
    Please use [ c o d e ] [ / c o d e ] tags

  5. #5
    Join Date
    Aug 2004
    Location
    Liège, Belgique
    Posts
    47

    Default

    It works fine with parameters.

    Thanks.

  6. #6
    Join Date
    Aug 2004
    Location
    San Mateo, CA
    Posts
    1,265

    Default

    You should never use raw String parameters. Not only are they prone to SQL injection and escaping issues, they mean that Hibernate can't use a PreparedStatement, so the JDBC usage is inefficient (through no fault of Hibernate or Spring).
    Rod Johnson - GM, SpringSource Division, VMware
    http://www.springsource.com
    Spring From the Source

Similar Threads

  1. Replies: 6
    Last Post: Nov 22nd, 2010, 08:32 AM
  2. Replies: 5
    Last Post: Feb 3rd, 2009, 05:19 AM
  3. Replies: 14
    Last Post: Feb 21st, 2005, 05:41 PM
  4. Replies: 3
    Last Post: Nov 19th, 2004, 07:16 PM
  5. Replies: 5
    Last Post: Aug 27th, 2004, 07:13 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •