-->
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: SQLState: 42P01 ERROR: relation "XXXX" does not e
PostPosted: Sat Feb 21, 2009 12:56 am 
Newbie

Joined: Sat Feb 21, 2009 12:30 am
Posts: 7
I am trying to get a very basic example from the book "Professional Hibernate" working. When I solve one problem, another pops up. But here is one I cannot figure out. I have seen several similar posts, but no real solutions or even ideas that point one in the correct direction.

I am using Hibernate 3 with NetBeans and a PostgreSQL 8.3 database on a Mac OS X platform.

I have ONE class called CD. I used NetBeans to generate the code, from the "Entity Class From a Database" wizard. It generated the requisite POJO fields and annotations for those fields. However, I don't seem to get the annotations to work (which is another problem entirely).

When I attempt to add an instance of CD to the database, I receive the following exception and debug log (this includes the generated SQL to attempt to insert the CD instance).

Code:
java.sql.BatchUpdateException: Batch entry 0 insert into CD (title, artist, purchasedate, cost, id) values (test, test, 2009-02-20 -05:00:00, 1.0, 5) was aborted.  Call getNextException to see the cause.

......(there's a bunch of stack trace info here)........

Caused by: java.sql.BatchUpdateException: Batch entry 0 insert into CD (title, artist, purchasedate, cost, id) values (test, test, 2009-02-20 -05:00:00, 1.0, 5) was aborted.  Call getNextException to see the cause.
12168 [AWT-EventQueue-0] WARN  org.hibernate.util.JDBCExceptionReporter  - SQL Error: 0, SQLState: null
12168 [AWT-EventQueue-0] ERROR org.hibernate.util.JDBCExceptionReporter  - Batch entry 0 insert into CD (title, artist, purchasedate, cost, id) values (test, test, 2009-02-20 -05:00:00, 1.0, 5) was aborted.  Call getNextException to see the cause.
        at org.postgresql.jdbc2.AbstractJdbc2Statement$BatchResultHandler.handleError(AbstractJdbc2Statement.java:2537)
12168 [AWT-EventQueue-0] WARN  org.hibernate.util.JDBCExceptionReporter  - SQL Error: 0, SQLState: 42P01
        at org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:1328)
12168 [AWT-EventQueue-0] ERROR org.hibernate.util.JDBCExceptionReporter  - ERROR: relation "cd" does not exist
12168 [AWT-EventQueue-0] ERROR org.hibernate.event.def.AbstractFlushingEventListener  - Could not synchronize database state with session
        at org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:351)
org.hibernate.exception.SQLGrammarException: Could not execute JDBC batch update



I created the following Mapping Documents for the CD class:
CD.hbm.xml
Code:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
  <class name="org.scawa.prohibernate.pojo.CD" table="CD">
      <id name="id"
          type="int">
          <column name="id"
                  sql-type="integer"
                  not-null="true"/>
          <generator class="sequence"/>
      </id>
      <property name="title" type="text" />
      <property name="artist" type="text" />
      <property name="purchasedate" type="date" />
      <property name="cost" type="double" />
  </class>
</hibernate-mapping>

Here is the SQL generated to create the Table CD:
Code:
CREATE TABLE "CD"
(
  id integer NOT NULL,
  title text,
  artist text,
  purchasedate date,
  "cost" double precision,
  CONSTRAINT "CD_pkey" PRIMARY KEY (id)
)
WITH (OIDS=FALSE);



The code that is used in the save is as follows:
Code:
                CD cd = new CD();

                cd.setTitle(titleField.getText());
                cd.setArtist(titleField.getText());
                cd.setCost(new Double(costField.getText()));
                cd.setPurchasedate(new Date());

                listModel.addCD(cd);
                Session session = null;
                try{
                    session = sessionFactory.openSession();
                    session.save(cd);
                }catch(Exception e){
                    System.out.println("Exception attemtping to Add CD: " + e.getMessage());

                }finally{
                    if(session != null && session.isOpen()){
                        session.flush();
                        session.close();
                    }
                }

                IDLabel.setText("");
                titleField.setText("");
                artistField.setText("");
                costField.setText("");



I know that the configuration file is correct and the mapping parses correctly and maps because those are problems I have worked through before. I, also know, that the hibernate_sequence is working because it increments each time I run the add, and the DEBUG log says it works. This means I am hitting the correct Schema and database (the postgres database and public schema).

Any possible help here?

Thanks in advance.

Stephen McConnell

"Boredom is a personal defect."
-- Lamar Stephens


Top
 Profile  
 
 Post subject:
PostPosted: Sat Feb 21, 2009 12:23 pm 
Newbie

Joined: Sat Feb 21, 2009 12:30 am
Posts: 7
I always believe that if one finds the solution to one's own problem, they should post it!!!! Well, here it is.

When I defined the table, I defined it as "CD" instead of "cd".

For some reason, Hibernate took the relationships that I had set up in my mappings...... YOU CAN SEE I MAPPED the class "CD" to the table "CD" (in upper case!) and put the table name to lower case.

I dropped the table, recreated it in lower case and I no longer received that exception!!!!!!!

HOW DUMB CAN THAT BE!!!! WHEN I DEFINE A TABLE IN UPPER CASE AND DEFINE THE RELATIONSHIP IN UPPER CASE, HIBERNATE SHOULD KEEP IT IN UPPER CASE!!!!!!!!!!!!!!!!!!!!!!

1) WHY DOES IT CHANGE THE CASE?
2) IS THIS HANDLED THE SAME ON ALL PLATFORMS (Windoz, Linux, Mac, AS/400, etc).
3) What happens if the table is defined in MIXED CASE?

This is the most inflexible behavior I have seen in a major framework.

Stephen McConnell


"Either you are part of the problem, or a part of the solution.... or maybe a percipient or a colloidal suspension."
-- Lamar Stephens


Top
 Profile  
 
 Post subject:
PostPosted: Sat Feb 21, 2009 12:42 pm 
Newbie

Joined: Sat Feb 21, 2009 12:30 am
Posts: 7
Now... I have another problem!!!

Although the DEBUG statements say the Batch is executed, no data is being saved to the database!!!!!!!!

Here is a copy of the debug log:

Code:
9854 [AWT-EventQueue-0] DEBUG org.hibernate.impl.SessionImpl  - opened session at timestamp: 12352339470
9856 [AWT-EventQueue-0] DEBUG org.hibernate.jdbc.AbstractBatcher  - about to open PreparedStatement (open PreparedStatements: 0, globally: 0)
9856 [AWT-EventQueue-0] DEBUG org.hibernate.jdbc.ConnectionManager  - opening JDBC connection
9856 [AWT-EventQueue-0] DEBUG org.hibernate.SQL  - select nextval ('hibernate_sequence')
9859 [AWT-EventQueue-0] DEBUG org.hibernate.id.SequenceGenerator  - Sequence identifier generated: 10
9859 [AWT-EventQueue-0] DEBUG org.hibernate.jdbc.AbstractBatcher  - about to close PreparedStatement (open PreparedStatements: 1, globally: 1)
9860 [AWT-EventQueue-0] DEBUG org.hibernate.event.def.AbstractSaveEventListener  - generated identifier: 10, using strategy: org.hibernate.id.SequenceGenerator
9868 [AWT-EventQueue-0] DEBUG org.hibernate.event.def.AbstractFlushingEventListener  - processing flush-time cascades
9868 [AWT-EventQueue-0] DEBUG org.hibernate.event.def.AbstractFlushingEventListener  - dirty checking collections
9869 [AWT-EventQueue-0] DEBUG org.hibernate.event.def.AbstractFlushingEventListener  - Flushed: 1 insertions, 0 updates, 0 deletions to 1 objects
9869 [AWT-EventQueue-0] DEBUG org.hibernate.event.def.AbstractFlushingEventListener  - Flushed: 0 (re)creations, 0 updates, 0 removals to 0 collections
9870 [AWT-EventQueue-0] DEBUG org.hibernate.pretty.Printer  - listing entities:
9871 [AWT-EventQueue-0] DEBUG org.hibernate.pretty.Printer  - org.scawa.prohibernate.pojo.CD{id=10, title=test, purchasedate=21 February 2009, artist=test, cost=1.0}
9884 [AWT-EventQueue-0] DEBUG org.hibernate.jdbc.AbstractBatcher  - about to open PreparedStatement (open PreparedStatements: 0, globally: 0)
9884 [AWT-EventQueue-0] DEBUG org.hibernate.SQL  - insert into cd (title, artist, purchasedate, cost, id) values (?, ?, ?, ?, ?)
9885 [AWT-EventQueue-0] DEBUG org.hibernate.jdbc.AbstractBatcher  - Executing batch size: 1
9887 [AWT-EventQueue-0] DEBUG org.hibernate.jdbc.AbstractBatcher  - about to close PreparedStatement (open PreparedStatements: 1, globally: 1)
9887 [AWT-EventQueue-0] DEBUG org.hibernate.jdbc.ConnectionManager  - releasing JDBC connection [ (open PreparedStatements: 0, globally: 0) (open ResultSets: 0, globally: 0)]
9888 [AWT-EventQueue-0] DEBUG org.hibernate.jdbc.ConnectionManager  - aggressively releasing JDBC connection


All the information from the above post is the same except that the table is now in lower case and I changed the CD.hbm.xml table="CD" to table="cd"

I'm searching the forum for other posts similar right about now. This is nuts.

Stephen McConnell

"Boredom is a personal defect."
-- Lamar Stephens


Top
 Profile  
 
 Post subject:
PostPosted: Sat Feb 21, 2009 12:57 pm 
Newbie

Joined: Sat Feb 21, 2009 12:30 am
Posts: 7
Ok.

Found solution to the ABOVE problem in this post Cannot Insert in Postgres.

It seems that one has to use Transaction code in Postgres, where as one does not have to use it with some other, less strick Databases (ie MySQL).

The new code is:

Code:
                CD cd = new CD();

                cd.setTitle(titleField.getText());
                cd.setArtist(titleField.getText());
                cd.setCost(new Double(costField.getText()));
                cd.setPurchasedate(new Date());

                listModel.addCD(cd);
                Session session = null;
                Transaction transaction = null;
                try{
                    session = sessionFactory.openSession();
                    transaction = session.beginTransaction();
                    session.save(cd);
                }catch(Exception e){
                    System.out.println("Exception attemtping to Add CD: " + e.getMessage());

                }finally{
                    if(session != null && session.isOpen()){
                        if(transaction != null)
                            transaction.commit();
                        session.flush();
                        session.close();
                    }
                }


Note the addition of "transaction" code. I have yet to try setting "autoCommit(false)", but will work with that.

I do understand the reason for the strict use of "transaction" code to persist into a DB... I'm slowly getting there in my understanding of Hibernate...

Stephen McConnell

"We are all born ignorant. You have to work hard to remain stupid."
-- Benjamin Franklin


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.