-->
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.  [ 4 posts ] 
Author Message
 Post subject: Persisted Strings in Hibernate
PostPosted: Sun Apr 30, 2006 1:07 pm 
Newbie

Joined: Wed Apr 19, 2006 7:54 am
Posts: 6
This relates to my previous post.

Are the strings automatically escaped when persisted? I'm using Postgres dialect and my strings aren't being escaped and are throwing an illegal SQL exception when characters like single quotes are used. The literals themselves aren't being surrounded by quotes - is this normal in hibernate or the postgres dialect?

Is there a setting I'm missing? Do I need to escape the strings myself?

Any help would really be appreciated :)

Cheers,

Dan.


Top
 Profile  
 
 Post subject:
PostPosted: Sun Apr 30, 2006 8:31 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 7:19 pm
Posts: 2364
Location: Brisbane, Australia
eh? The SQL generated uses prepared statements meaning you will see a ? as the augument so no string quotes necessary. I have a postgres database running for a large project I am on and we have no issues - maybe you had better supply more information, eg, simple sample code and mappings etc.


Top
 Profile  
 
 Post subject:
PostPosted: Mon May 01, 2006 5:21 am 
Newbie

Joined: Wed Apr 19, 2006 7:54 am
Posts: 6
As far as I can tell strings containing SQL characters that don't need to be escaped are fine. As soon as a single quote character is introduced the tests fall over - hence the question regarding whether hibernate automatically escapes strings or not.

Here's the details from my previous post:

Hibernate version:

3.1

Spring Framework version:

1.2.7

Full stack trace of any exception that occurs:


Code:
[junit] Testcase: testComments(com.mediagraft.podsplice.dao.hibernate.Hibern
ateCommentaryDAOTest): Caused an ERROR
[junit] Hibernate operation: Could not execute JDBC batch update; bad SQL gr
ammar [insert into comments (username, date, rating, subject, text, comment_id)
values (?, ?, ?, ?, ?, ?)]; nested exception is java.sql.BatchUpdateException: B
atch entry 0 insert into comments (username, date, rating, subject, text, commen
t_id) values (derek_sykes564, 2006-04-28 11:48:47.876000+0100, 3, Average., Not
really the worst thing I've seen but hardly the best. I was really expecting som
ething amazing but in the end was left feeling a little deflated. The chicken in
particular is particularly bad., 1) was aborted. Call getNextException to see
the cause.
[junit] org.springframework.jdbc.BadSqlGrammarException: Hibernate operation
: Could not execute JDBC batch update; bad SQL grammar [insert into comments (us
ername, date, rating, subject, text, comment_id) values (?, ?, ?, ?, ?, ?)]; nes
ted exception is java.sql.BatchUpdateException: Batch entry 0 insert into commen
ts (username, date, rating, subject, text, comment_id) values (derek_sykes564, 2
006-04-28 11:48:47.876000+0100, 3, Average., Not really the worst thing I've see
n but hardly the best. I was really expecting something amazing but in the end w
as left feeling a little deflated. The chicken in particular is particularly bad
., 1) was aborted. Call getNextException to see the cause.
[junit] java.sql.BatchUpdateException: Batch entry 0 insert into comments (u
sername, date, rating, subject, text, comment_id) values (derek_sykes564, 2006-0
4-28 11:48:47.876000+0100, 3, Average., Not really the worst thing I've seen but
hardly the best. I was really expecting something amazing but in the end was le
ft feeling a little deflated. The chicken in particular is particularly bad., 1)
was aborted. Call getNextException to see the cause.
[junit] at org.postgresql.jdbc2.AbstractJdbc2Statement$BatchResultHandle
r.handleError(AbstractJdbc2Statement.java:2396)
[junit] at org.postgresql.core.v3.QueryExecutorImpl.processResults(Query
ExecutorImpl.java:1257)
[junit] at org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecuto
rImpl.java:334)
[junit] at org.postgresql.jdbc2.AbstractJdbc2Statement.executeBatch(Abst
ractJdbc2Statement.java:2455)
[junit] at org.apache.commons.dbcp.DelegatingStatement.executeBatch(Dele
gatingStatement.java:294)
[junit] at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBat
cher.java:58)
[junit] at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatch
er.java:195)
[junit] at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.j
ava:235)
[junit] at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.j
ava:139)
[junit] at org.hibernate.event.def.AbstractFlushingEventListener.perform
Executions(AbstractFlushingEventListener.java:297)
[junit] at org.hibernate.event.def.DefaultFlushEventListener.onFlush(Def
aultFlushEventListener.java:27)
[junit] at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:985)
[junit] at org.springframework.orm.hibernate3.HibernateTemplate$29.doInH
ibernate(HibernateTemplate.java:815)
[junit] at org.springframework.orm.hibernate3.HibernateTemplate.execute(
HibernateTemplate.java:366)
[junit] at org.springframework.orm.hibernate3.HibernateTemplate.flush(Hi
bernateTemplate.java:813)
[junit] at com.mediagraft.podsplice.dao.hibernate.HibernateCommentaryDAO
Test.beforeAssertions(HibernateCommentaryDAOTest.java:63)
[junit] at com.mediagraft.podsplice.dao.hibernate.HibernateCommentaryDAO
Test.testComments(HibernateCommentaryDAOTest.java:1090)
[junit] at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
[junit] at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAcces
sorImpl.java:39)
[junit] at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMet
hodAccessorImpl.java:25)


[junit] Test com.mediagraft.podsplice.dao.hibernate.HibernateCommentaryDAOTe
st FAILED


Name and version of the database you are using:

PostgreSQL 8.1

The generated SQL (show_sql=true):

Code:
[junit] Hibernate: select nextval ('comment_id')
[junit] Hibernate: select nextval ('comment_id')
[junit] Hibernate: select nextval ('comment_id')
[junit] Hibernate: insert into comments (username, date, rating, subject, te
xt, comment_id) values (?, ?, ?, ?, ?, ?)
[junit] Hibernate: insert into comments (username, date, rating, subject, te
xt, comment_id) values (?, ?, ?, ?, ?, ?)
[junit] Hibernate: insert into comments (username, date, rating, subject, te
xt, comment_id) values (?, ?, ?, ?, ?, ?)


Top
 Profile  
 
 Post subject:
PostPosted: Mon May 01, 2006 11:37 am 
Newbie

Joined: Wed Apr 19, 2006 7:54 am
Posts: 6
sigh..

I was being a muppet with my schema - BIGINT instead of VARCHAR for the username. *groans*..

Thanks anyway - will leave positive feedback :)

[/img]


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 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.