Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / June 2007

Tip: Looking for answers? Try searching our database.

Hibernate: problems with insert and update

Thread view: 
loris_p - 09 Jun 2007 15:28 GMT
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



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.