Scenario
My program is a Simple Swing program which asks the user to input a ProductID and parts for it. This program demonstrates Hibernate Set Mapping.
Hibernate version: 3.2.4
Mapping documents
Product.hbm.xml
Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="hibernate.Product" table="products">
<id name="productId" column="productId" >
<generator class="assigned"/>
</id>
<set name="parts">
<key column = "productId" not-null="true" />
<one-to-many class = "hibernate.Parts" />
</set>
</class>
</hibernate-mapping>
Parts.hbm.xmlCode:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="hibernate.Parts" table="parts">
<id name="partId" column="partId" >
<generator class="assigned"/>
</id>
<property name="partName" column="partName" />
</class>
</hibernate-mapping>
CodeCode:
package hibernate;
import java.util.HashSet;
import java.util.Set;
import javax.swing.JOptionPane;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
public class ProductAppln {
public static void main(String[] args) {
Session session = null;
Transaction tx = null;
Product product = null;
String choice = null;
int count = 0;
try{
SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
session =sessionFactory.openSession();
do{
choice = JOptionPane.showInputDialog(null, "Do you want to Insert Data for Products (Y/N)");
if (choice.equalsIgnoreCase("Y")){
String productId = JOptionPane.showInputDialog(null, "Enter the Product ID (Number)");
int prodId = Integer.parseInt(productId);
String noOfParts = JOptionPane.showInputDialog(null, "Enter the number of parts for " +
"product id : "+productId);
int no_parts = Integer.parseInt(noOfParts);
Set parts_set = new HashSet();
for(int i = 0; i< no_parts ; i++){
String partId = JOptionPane.showInputDialog(null, "Enter part id (Number)");
int pId = Integer.parseInt(partId);
String partName = JOptionPane.showInputDialog(null, "Enter part Name");
Parts parts = new Parts();
parts.setPartId(pId);
parts.setPartName(partName);
parts_set.add(parts);
session.save(parts);
// session = sessionFactory.openSession();
}
tx = session.beginTransaction();
product = new Product();
product.setProductId(prodId);
product.setParts(parts_set);
session.save(product);
// session =sessionFactory.openSession();
tx.commit();
count++;
}else{
return;
}
}while (!choice.equalsIgnoreCase("N"));
System.out.println(count+ " Row(s) inserted into the Message Table");
}
catch(NumberFormatException e){
e.printStackTrace();
}catch(HibernateException e){
System.out.println("Entered Duplicate Product id : "+product.getProductId());
JOptionPane.showMessageDialog(null, "Entered Duplicate Product id : "+product.getProductId());
e.printStackTrace();
}
catch(Exception e){
e.printStackTrace();
}finally{
session.flush();
session.close();
System.exit(0);
}
}
}
Full stack trace of any exception that occurs:log4j:WARN No appenders could be found for logger (org.hibernate.cfg.Environment).
log4j:WARN Please initialize the log4j system properly.
Hibernate: update parts set partName=? where partId=?
Exception in thread "main" org.hibernate.exception.SQLGrammarException: Could not execute JDBC batch update
at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:67)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:253)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:237)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:142)
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 hibernate.ProductAppln.main(ProductAppln.java:71)
Caused by: java.sql.BatchUpdateException: ORA-00942: table or view does not exist
at oracle.jdbc.dbaccess.DBError.throwBatchUpdateException(DBError.java:441)
at oracle.jdbc.driver.OraclePreparedStatement.executeBatch(OraclePreparedStatement.java:3377)
at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:48)
at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:246)
... 6 more
Name and version of the database you are using:Oracle 9i
Configuration FileCode:
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.url">jdbc:oracle:thin:@localhost:1521:bob</property>
<property name="hibernate.connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
<property name="hibernate.connection.username">scott</property>
<property name="hibernate.connection.password">tiger</property>
<property name="dialect">org.hibernate.dialect.Oracle9Dialect</property>
<property name="show_sql">true</property>
<property name="hibernate.hbm2ddl.auto">update</property>
<mapping resource="Message.hbm.xml" />
<mapping resource="Parts.hbm.xml" />
<mapping resource="Product.hbm.xml" />
</session-factory>
</hibernate-configuration>
[/b]