Hibernate: problems with insert and update

L

loris_p

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.
 
L

loris_p

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>
 
M

Marc E

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
 
L

loris_p

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.
 
L

Lew

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,053
Latest member
BrodieSola

Latest Threads

Top