18:47:33,179 INFO ReflectHelper:149 - reflection optimizer disabled for: test.h
ibernate.data.OrderItem, NullPointerException: null
hi
i am new to thr hibernate, so i am doing some simple application,
so i have 3 tables.
here are the 3 tables.
CREATE TABLE ORDERS(
ID VARCHAR NOT NULL PRIMARY KEY,
ORDER_DATE TIMESTAMP NOT NULL,
PRICE_TOTAL DOUBLE NOT NULL)
CREATE TABLE PRODUCTS(
ID VARCHAR NOT NULL PRIMARY KEY,
NAME VARCHAR NOT NULL,
PRICE DOUBLE NOT NULL,
AMOUNT INTEGER NOT NULL)
CREATE TABLE ORDER_ITEMS(
ID VARCHAR NOT NULL PRIMARY KEY,
ORDER_ID VARCHAR NOT NULL,
i created asimple web application using hibernate/jsp/java/webworks/tomcat
i created aweb forms for enter each table values.( for order table and products table)
values are insetrd in to the above 2 tables successfully.problem occurs when i am going to display order vales in a another jsp.
then i got following error in the tomcat.
18:47:33,179 INFO ReflectHelper:149 - reflection optimizer disabled for: test.h
ibernate.data.OrderItem, NullPointerException: null
here are my mapping files and other java classes.
order.hbm.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping
PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
<hibernate-mapping>
<class name="test.hibernate.data.Order" table="orders">
<id name="id" type="string" unsaved-value="null" >
<column name="id" sql-type="char(32)"
not-null="true"/>
<generator class="uuid.hex"/>
</id>
<property name="oname">
<column name="oname" sql-type="char(32)" not-null="true"/>
</property>
<property name="date">
<column name="order_date" sql-type="date" not-null="true"/>
</property>
<property name="priceTotal">
<column name="price_total" sql-type="double" not-null="true"/>
</property>
<set name="orderItems" table="items" inverse="true" cascade="all" lazy="false" sort="unsorted" batch-size="1" outer-join="auto">
<key column="order_id" />
<one-to-many class="test.hibernate.data.OrderItem" />
</set>
</class>
</hibernate-mapping>
product.hbm.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping
PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
<hibernate-mapping>
<class name="test.hibernate.data.Product" table="products">
<!-- A 32 hex character is our surrogate key. It's automatically
generated by Hibernate with the UUID pattern. -->
<id name="id" type="string" unsaved-value="null" >
<column name="id" sql-type="char(32)" not-null="true"/>
<generator class="uuid.hex"/>
</id>
<property name="name">
<column name="name" sql-type="char(255)" not-null="true"/>
</property>
<property name="price">
<column name="price" sql-type="double" not-null="true"/>
</property>
<property name="amount">
<column name="amount" sql-type="integer" not-null="true"/>
</property>
</class>
</hibernate-mapping>
orderItem.hbm.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping
PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
<hibernate-mapping>
<class name="test.hibernate.data.OrderItem" table="items">
<!-- A 32 hex character is our surrogate key. It's automatically
generated by Hibernate with the UUID pattern. -->
<id name="id" type="string" unsaved-value="null" >
<column name="id" sql-type="char(32)" not-null="true"/>
<generator class="uuid.hex"/>
</id>
<property name="orderId" insert="false" update="false">
<column name="order_id" sql-type="char(32)" not-null="true"/>
</property>
<property name="productId" insert="false" update="false">
<column name="product_id" sql-type="char(32)" not-null="true"/>
</property>
<property name="amount">
<column name="amount" sql-type="integer" not-null="true"/>
</property>
<property name="price">
<column name="price" sql-type="double" not-null="true"/>
</property>
<many-to-one name="order" class="test.hibernate.data.Order" column="order_id" />
<many-to-one name="product" class="test.hibernate.data.Product" cascade="save-update" column="product_id"/>
</class>
</hibernate-mapping>
here are my java classes
this the class for display order items( not working)
package test.hibernate.actions;
import java.util.List;
import java.util.Date;
import java.text.DateFormat;
import webwork.action.ActionSupport;
import webwork.action.CommandDriven;
import net.sf.hibernate.Session;
import net.sf.hibernate.SessionFactory;
import net.sf.hibernate.cfg.Configuration;
import net.sf.hibernate.HibernateException;
import test.hibernate.util.SessionManager;
import test.hibernate.data.Order;
import test.hibernate.data.OrderItem;
import test.hibernate.data.Product;
public class OrderList extends ActionSupport {
private List orders;
public List getOrders() {
return orders;
}
public String doExecute() {
try {
Configuration cfg=new Configuration().addClass(test.hibernate.data.Order.class).addClass(test.hibernate.data.OrderItem.class).addClass(Product.class);
SessionFactory sf=cfg.buildSessionFactory();
Session sess=sf.openSession();
Order event = new Order();
orders = sess.find("from test.hibernate.data.Order");
// orders = sess.find("select o from o in class test.hibernate.data.Order");
sess.close();
return SUCCESS;
} catch (HibernateException e) {
e.printStackTrace();
System.out.println("errrorrrrrrrrrrrrrrrrrrrrrrrrrrrrr");
return ERROR;
}
}
public String doEnter() {
return INPUT;
}
}
product are displaying successfully
thisn the productlist class
package test.hibernate.actions;
import java.util.List;
import webwork.action.ActionSupport;
import net.sf.hibernate.Session;
import net.sf.hibernate.SessionFactory;
import net.sf.hibernate.cfg.Configuration;
import net.sf.hibernate.HibernateException;
import test.hibernate.util.SessionManager;
import test.hibernate.data.Product;
public class EventList extends ActionSupport {
private List products;
public List getProducts() {
return products;
}
public String doExecute() {
try {
Configuration cfg=new Configuration().addClass(test.hibernate.data.Product.class);
SessionFactory sf=cfg.buildSessionFactory();
Session sess=sf.openSession();
products = sess.find("select p from p in class test.hibernate.data.Product");
sess.close();
//select product from product in class test.hibernate.data.Product
return SUCCESS;
} catch (HibernateException e) {
e.printStackTrace();
return ERROR;
}
}
}
order.java
package test.hibernate.data;
import java.util.Date;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
import java.text.DateFormat;
public class Order {
private String id;
private String oname;
//private Date date;
private String date;
private double priceTotal;
private Set orderItems = new HashSet();
public Order() {
//this.date = new Date();
}
/**
* Add a Product to this Order. Product automatically becomes an OrderItem.
* The priceTotal is automatically updated.
*
* @param p Product to add to this Order
* @param amount amount of products to add
*/
public void addProduct(Product p, int amount) {
OrderItem orderItem = new OrderItem(this, p, amount);
this.priceTotal = this.priceTotal + p.getPrice() * amount;
this.orderItems.add(orderItem);
}
public String getOname(){
return oname;
}
public void setOname(String oname){
this.oname=oname;
}
public String getDate() {
return date;
}
public String getId() {
return id;
}
public Set getOrderItems() {
return orderItems;
}
public double getPriceTotal() {
return priceTotal;
}
public void setDate(String date) {
this.date = date;
}
// /*public void setDate(String odate) {
//
// try{
//
// DateFormat d = DateFormat.getDateInstance();
// this.date = d.parse(odate);
// }catch (Exception p){
//p.printStackTrace();
// System.out.println("errorrrrrrrrrrrrrrrr");
// }
// }*/
public void setId(String string) {
id = string;
}
public void setOrderItems(Set set) {
orderItems = set;
}
public void setPriceTotal(double d) {
priceTotal = d;
}
public String toString() {
String s = "[Order] id=" + id +
"\n priceTotal=" + priceTotal +
"\n date=" + date + "\n";
s = s + "Order items:\n";
Iterator i = orderItems.iterator();
String o = "";
while (i.hasNext()) {
OrderItem item = (OrderItem) i.next();
o = o + " " + item + "\n";
}
return s + o;
}
}
product.java
package test.hibernate.data;
public class Product {
private String id;
private String name;
private double price;
private int amount;
// ===============
public int getAmount() {
return amount;
}
public String getId() {
return id;
}
public String getName() {
return name;
}
public double getPrice() {
return price;
}
public void setAmount(int i) {
amount = i;
}
public void setId(String string) {
id = string;
}
public void setName(String string) {
name = string;
}
public void setPrice(double d) {
price = d;
}
public String toString() {
return "[Product] " + name + "(" + id + ") price=" + price + " amount=" + amount;
}
}
orderitem.java
package test.hibernate.data;
public class OrderItem {
/**
* Empty constructor to conform to JavaBeans convention.
*
*/
public OrderItem() {
// empty default constructor
}
/**
* Creates valid OrderItem.
* @param order to which this OrderItem belongs
* @param p from which this OrderItem is created
* @param amount
*/
public OrderItem(Order order, Product product, int amount) {
this.order = order;
this.product = product;
this.amount = amount;
product.setAmount(product.getAmount() - amount);
this.price = product.getPrice() * amount;
}
private String id;
private Product product;
private Order order;
private String productId;
private String orderId;
private double price;
private int amount;
public int getAmount() {
return amount;
}
public String getId() {
return id;
}
public Order getOrder() {
return order;
}
public String getOrderId() {
return order.getId();
}
public double getPrice() {
return price;
}
public Product getProduct() {
return product;
}
public String getProductId() {
return product.getId();
}
public void setAmount(int i) {
amount = i;
}
public void setId(String string) {
id = string;
}
public void setOrder(Order order) {
this.order = order;
}
public void setOrderId(String string) {
this.orderId = string;
}
public void setPrice(double d) {
price = d;
}
public void setProduct(Product product) {
this.product = product;
}
public void setProductId(String string) {
this.productId = string;
}
public String toString() {
return "[OrderItem] id=" + id +
"\n amount=" + amount +
"\n price=" + price +
"\n (" + product + ")";
}
}
REGARDS'
GIMHAN
PLS HELP ME
|