I have the following simple design
Quote:
Library has an array of books - Book[]
Book is abstract
SoftwareBook is a derivedclass of Book
DB has Library Table, SoftwareBook table.
Since Book is defined abstract, there is no Book table
Hibernate is trying to update Book table and since there is no Book table,
am getting "table does not exist error"
Hibernate version: 3.2.1
Mapping documents:
Library.hbm.xml:
-------------------
Code:
<hibernate-mapping package="sample">
<class name="Library" table="Library">
<id name="id" column="INSTANCEID" type="long">
<generator class="native"/>
</id>
<property name="Name" column="Name"/>
<property name="Location" column="Location"/>
<array name="Books" table="Books" cascade="all">
<key column="libraryId" />
<index column="sortOrder" />
<one-to-many class="Book"/>
</array>
</class>
</hibernate-mapping>
Book.hbm.xml:
-----------------
Please note the use of union-subclass and abstract=true for Book and abstract=false for SoftwareBook
Code:
<hibernate-mapping package="sample">
<class name="Book" table="Book" abstract="true">
<id name="id" column="INSTANCEID" type="long">
<generator class="native"/>
</id>
<property name="author" column="author"/>
<union-subclass name="SoftwareBook" table="SoftwareBook" abstract="false">
<property name="Category" column="Category"/>
</union-subclass>
</class>
</hibernate-mapping>
Code between sessionFactory.openSession() and session.close():Code:
1. Transaction save = session.beginTransaction();
2. Library library = new Library("Santa-Clara-Library", "Santa Clara");
3. library.setId(1);
4. SoftwareBook book1 = new SoftwareBook("K&R","C++");
5. SoftwareBook book2 = new SoftwareBook("Routing", "Networking");
6. Book[] books = {book1, book2};
7. library.setBooks(books);
8. session.save(library);
Full stack trace of any exception that occurs:The problem is happening because it is trying to update Book table.
If I comment out line number 7 above, it works fine.
If I change from array to set, it works fine too.
Code:
Hibernate: insert into Library (Name, Location, INSTANCEID) values (?, ?, ?)
....
Hibernate: insert into SoftwareBook (author, Category, INSTANCEID) values (?, ?, ?)
....
Hibernate: insert into SoftwareBook (author, Category, INSTANCEID) values (?, ?, ?)
....
2007-02-23 00:33:37,807 DEBUG SQL:393 - update Book set libraryId=?, sortOrder=? where INSTANCEID=?
Hibernate: update Book set libraryId=?, sortOrder=? where INSTANCEID=?
....
2007-02-23 00:33:37,885 DEBUG JDBCExceptionReporter:69 - Could not execute JDBC batch update [update Book set libraryId=?, sortOrder=? where INSTANCEID=?]
java.sql.BatchUpdateException: ORA-00942: table or view does not exist
at oracle.jdbc.driver.DatabaseError.throwBatchUpdateException(DatabaseError.java:343)
at oracle.jdbc.driver.OraclePreparedStatement.executeBatch(OraclePreparedStatement.java:10656)
at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:48)
at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:242)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:235)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:143)
at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:298)
at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:27)
at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1000)
at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:338)
at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:106)
at sample.Main.sampleWrite1(Main.java:58)
at sample.Main.main(Main.java:17)
Name and version of the database you are using:
Oracle
The generated SQL (show_sql=true):
Debug level Hibernate log excerpt: