Hi, I'm having some problems inserting and updating records on my
application.
The class is Answer. It has a n-1 relation with Question.
When I try to save a new Answer or a modified one, I have this
exception:
org.hibernate.exception.SQLGrammarException: Could not execute JDBC
batch update
org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:
67)
org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:
43)
org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:
253)
org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:235)
org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:139)
org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:
298)
org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:
27)
org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1000)
org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:338)
org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:
106)
reservoirdogs.informaticTrivia.hibernate.facade.AnswerFacade.saveAnswer(AnswerFacade.java:
19)
I'm using a PostgreSQL server, Answer's primary key type is mapped as
"native".
This is the code I use for Answer saving:
public void saveAnswer(Answer answer) {
Session session =
reservoirdogs.informaticTrivia.hibernate.util.HibernateUtil.currentSession();
Transaction tx = session.beginTransaction();
//session.saveOrUpdate(answer);
session.merge(answer);
tx.commit();
reservoirdogs.informaticTrivia.hibernate.util.HibernateUtil.closeSession();
}
What's my mistake? Thanks.
Marc E - 09 Jun 2007 17:11 GMT
can you post the mapping file(s)?
> Hi, I'm having some problems inserting and updating records on my
> application.
[quoted text clipped - 40 lines]
>
> What's my mistake? Thanks.
loris_p - 09 Jun 2007 17:50 GMT
This is mapping file for Answer bean:
<hibernate-mapping>
<class
name="reservoirdogs.informaticTrivia.hibernate.bean.Answer"
table="answer"
>
<id
name="id"
column="id"
type="int"
>
<generator class="native">
</generator>
</id>
<property
name="text"
type="java.lang.String"
update="true"
insert="true"
column="text"
/>
<property
name="right"
type="boolean"
update="true"
insert="true"
column="right"
/>
<many-to-one
name="question"
class="reservoirdogs.informaticTrivia.hibernate.bean.Question"
cascade="all"
outer-join="auto"
update="true"
insert="true"
column="question_fk"
not-null="true"
/>
</class>
</hibernate-mapping>
And this is Question mapping file:
<hibernate-mapping>
<class
name="reservoirdogs.informaticTrivia.hibernate.bean.Question"
table="QUESTION"
>
<id
name="id"
column="id"
type="int"
>
<generator class="native">
</generator>
</id>
<property
name="difficulty"
type="int"
update="true"
insert="true"
column="difficulty"
/>
<property
name="text"
type="java.lang.String"
update="true"
insert="true"
column="text"
/>
<set
name="answers"
lazy="false"
cascade="none"
sort="unsorted"
>
<key
column="question_fk"
>
</key>
<one-to-many
class="reservoirdogs.informaticTrivia.hibernate.bean.Answer"
/>
</set>
<many-to-one
name="category"
class="reservoirdogs.informaticTrivia.hibernate.bean.Category"
cascade="none"
outer-join="auto"
update="true"
insert="true"
column="category_fk"
not-null="true"
/>
</class>
</hibernate-mapping>
Marc E - 10 Jun 2007 19:29 GMT
two things to help you down the right track:
1) confirm in your hibernate config file that you have the correct dialect
set for your database
2) turn show_sql on in your hibernate config file and run your code, look at
the sql it's trying to run and see what's screwy about it. that usually
gives me a good idea where i've gone wrong. like, i'll see single quotes
around an int field or something dumb
> This is mapping file for Answer bean:
>
[quoted text clipped - 108 lines]
> </class>
> </hibernate-mapping>
loris_p - 10 Jun 2007 23:28 GMT
Thank you :)
The error was on a field named "right" that PostgreSQL didn't like
very much.
I renamed it into "is_right" and now all works fine.
Thanks.
Lew - 10 Jun 2007 23:46 GMT
> Thank you :)
> The error was on a field named "right" that PostgreSQL didn't like
> very much.
> I renamed it into "is_right" and now all works fine.
Given that RIGHT is a Standard SQL keyword, that is hardly surprising.
<http://www.postgresql.org/docs/8.2/interactive/sql-keywords-appendix.html>
You can use keywords as identifiers if you enclose them in double-quotes.
<http://www.postgresql.org/docs/8.2/interactive/sql-syntax-lexical.html#SQL-SYNTA
X-IDENTIFIERS>

Signature
Lew