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