此代码在以下部分中无法正常运行。我该如何解决这个问题?
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!-- <property name="hibernate.connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
<property name="hibernate.connection.url">jdbc:oracle:thin:@10.241.47.160:1521:orcl</property>
<property name="hibernate.connection.username">training</property> <property
name="hibernate.connection.password">training</property> <property name="hibernate.connection.pool_size">150</property>
<property name="show_sql">true</property> <property name="dialect">org.hibernate.dialect.OracleDialect</property>
<property name="hibernate.hbm2ddl.auto">update</property> -->
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/pizzadelivery</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">root</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="show_sql">true</property>
<property name="hibernate.cache.use_second_level_cache">true</property>
<property name="hibernate.cache.region.factory_class">org.hibernate.cache.ehcache.EhCacheRegionFactory</property>
<!-- Mapping files -->
<mapping class="com.cts.pizza.pojo.Customer" />
<mapping class="com.cts.pizza.pojo.MemberShipCard" />
<mapping class="com.cts.pizza.pojo.Pizza" />
<mapping class="com.cts.pizza.pojo.PizzaOrder" />
<mapping class="com.cts.pizza.pojo.PizzaOrderDetail" />
<mapping class="com.cts.pizza.pojo.SalesBoy" />
</session-factory>
</hibernate-configuration>
@Entity
@Table(name="SALESBOYS")
public class SalesBoy {
private Integer salesBoyId;
private String salesBoyName;
private String address;
private String contactNo;
private String emailId;
private Set<PizzaOrder> pizzaOrders;
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
public Integer getSalesBoyId() {
return salesBoyId;
}
public void setSalesBoyId(Integer salesBoyId) {
this.salesBoyId = salesBoyId;
}
@Column(name="SALESBOYNAME")
public String getSalesBoyName() {
return salesBoyName;
}
public void setSalesBoyName(String salesBoyName) {
this.salesBoyName = salesBoyName;
}
@Column(name="ADDRESS")
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
@Column(name="CONTACTNO")
public String getContactNo() {
return contactNo;
}
public void setContactNo(String contactNo) {
this.contactNo = contactNo;
}
@Column(name="EMAILID")
public String getEmailId() {
return emailId;
}
public void setEmailId(String emailId) {
this.emailId = emailId;
}
@OneToMany
@JoinColumn(name = "SALESBOY_ID")
public Set<PizzaOrder> getPizzaOrders() {
return pizzaOrders;
}
public void setPizzaOrders(Set<PizzaOrder> pizzaOrders) {
this.pizzaOrders = pizzaOrders;
}
}
package com.cts.pizza.pojo;
import java.io.Serializable;
import javax.persistence.Embeddable;
@Embeddable
public class PizzaOrderDetailId implements Serializable{
private static final long serialVersionUID = 1L;
private int orderId;
private int pizzaId;
public int getOrderId() {
return orderId;
}
public void setOrderId(int orderId) {
this.orderId = orderId;
}
public int getPizzaId() {
return pizzaId;
}
public void setPizzaId(int pizzaId) {
this.pizzaId = pizzaId;
}
}
package com.cts.pizza.pojo;
import javax.persistence.AttributeOverride;
import javax.persistence.AttributeOverrides;
import javax.persistence.Column;
import javax.persistence.EmbeddedId;
import javax.persistence.Entity;
import javax.persistence.Table;
@Entity
@Table(name="PIZZAORDERDETAIL")
public class PizzaOrderDetail {
@AttributeOverrides({
@AttributeOverride(name="orderId",column=@Column(name="ORDER_ID")),
@AttributeOverride(name="pizzaId",column=@Column(name="PIZZA_ID"))
})
@EmbeddedId
private PizzaOrderDetailId pizzaOrderDetailId;
private int quantity;
private double price;
public PizzaOrderDetailId getPizzaOrderDetailId() {
return pizzaOrderDetailId;
}
public void setPizzaOrderDetailId(PizzaOrderDetailId pizzaOrderDetailId) {
this.pizzaOrderDetailId = pizzaOrderDetailId;
}
@Column(name="QUANTITY")
public int getQuantity() {
return quantity;
}
public void setQuantity(int quantity) {
this.quantity = quantity;
}
@Column(name="PRICE")
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
}
package com.cts.pizza.pojo;
import java.util.Date;
import java.util.Set;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToMany;
import javax.persistence.Table;
@Entity
@Table(name="PIZZAORDER")
public class PizzaOrder {
private int orderId;
private Date orderDate;
private double totalAmount;
private int customerNo;
private Integer salesBoyId;
private Set<PizzaOrderDetail> pizzaOrderDetail;
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
@Column(name="ORDERID")
public int getOrderId() {
return orderId;
}
public void setOrderId(int orderId) {
this.orderId = orderId;
}
@Column(name="ORDERDATE")
public Date getOrderDate() {
return orderDate;
}
public void setOrderDate(Date orderDate) {
this.orderDate = orderDate;
}
@Column(name="TOTALAMOUNT")
public double getTotalAmount() {
return totalAmount;
}
public void setTotalAmount(double totalAmount) {
this.totalAmount = totalAmount;
}
@Column(name="CUSTOMER_NO")
public int getCustomerNo() {
return customerNo;
}
public void setCustomerNo(int customerNo) {
this.customerNo = customerNo;
}
@Column(name="SALESBOY_ID")
public Integer getSalesBoyId() {
return salesBoyId;
}
public void setSalesBoyId(Integer salesBoyId) {
this.salesBoyId = salesBoyId;
}
@OneToMany(fetch=FetchType.LAZY)
@JoinColumn(name="ORDER_ID")
public Set<PizzaOrderDetail> getPizzaOrderDetail() {
return pizzaOrderDetail;
}
public void setPizzaOrderDetail(Set<PizzaOrderDetail> pizzaOrderDetail) {
this.pizzaOrderDetail = pizzaOrderDetail;
}
}
package com.cts.pizza.pojo;
import java.util.Set;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToMany;
import javax.persistence.Table;
@Entity
@Table(name="PIZZA")
public class Pizza {
private int pizzaId;
private String pizzaName;
private Set<PizzaOrderDetail> pizzaOrderDetail;
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
@Column(name="PIZZAID")
public int getPizzaId() {
return pizzaId;
}
public void setPizzaId(int pizzaId) {
this.pizzaId = pizzaId;
}
@Column(name="PIZZANAME")
public String getPizzaName() {
return pizzaName;
}
public void setPizzaName(String pizzaName) {
this.pizzaName = pizzaName;
}
@OneToMany(fetch=FetchType.LAZY)
@JoinColumn(name="PIZZA_ID")
public Set<PizzaOrderDetail> getPizzaOrderDetail() {
return pizzaOrderDetail;
}
public void setPizzaOrderDetail(Set<PizzaOrderDetail> pizzaOrderDetail) {
this.pizzaOrderDetail = pizzaOrderDetail;
}
}
package com.cts.pizza.pojo;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name = "MEMBERSHIPCARD")
public class MemberShipCard {
private int cardNo;
private int pointsEarned;
private int customerNo;
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
@Column(name="CARDNO")
public int getCardNo() {
return cardNo;
}
public void setCardNo(int cardNo) {
this.cardNo = cardNo;
}
@Column(name="POINTSEARNED")
public int getPointsEarned() {
return pointsEarned;
}
public void setPointsEarned(int pointsEarned) {
this.pointsEarned = pointsEarned;
}
@Column(name="CUSTOMER_NO")
public int getCustomerNo() {
return customerNo;
}
public void setCustomerNo(int customerNo) {
this.customerNo = customerNo;
}
}
package com.cts.pizza.pojo;
import java.util.Set;
import javax.persistence.Column;
import javax.persistence.Embedded;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
import javax.persistence.Table;
@Entity
@Table(name="CUSTOMER")
public class Customer {
private int customerNo;
private String custName;
private String contactNo;
private String emailId;
private Address address;
private Set<PizzaOrder> pizzaOrders;
private MemberShipCard memberShipCard;
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
@Column(name="CUSTOMERNO")
public int getCustomerNo() {
return customerNo;
}
public void setcustomerNo(int customerNo) {
this.customerNo = customerNo;
}
@Column(name="CUSTOMERNAME")
public String getCustName() {
return custName;
}
public void setCustName(String custName) {
this.custName = custName;
}
@Column(name="CONTACTNO")
public String getContactNo() {
return contactNo;
}
public void setContactNo(String contactNo) {
this.contactNo = contactNo;
}
@Column(name="EMAILID")
public String getEmailId() {
return emailId;
}
public void setEmailId(String emailId) {
this.emailId = emailId;
}
@Embedded
public Address getAddress() {
return address;
}
public void setAddress(Address address) {
this.address = address;
}
@OneToMany
@JoinColumn(name="CUSTOMER_NO")
public Set<PizzaOrder> getPizzaOrders() {
return pizzaOrders;
}
public void setPizzaOrders(Set<PizzaOrder> pizzaOrders) {
this.pizzaOrders = pizzaOrders;
}
@OneToOne
@JoinColumn(name="CUSTOMER_NO")
public MemberShipCard getMemberShipCard() {
return memberShipCard;
}
public void setMemberShipCard(MemberShipCard memberShipCard) {
this.memberShipCard = memberShipCard;
}
}
package com.cts.pizza.pojo;
import javax.persistence.Column;
public class Address {
private String doorNo;
private String street;
private String city;
private String state;
private int pinCode;
@Column(name="DOORNO")
public String getDoorNo() {
return doorNo;
}
public void setDoorNo(String doorNo) {
this.doorNo = doorNo;
}
@Column(name="STREET")
public String getStreet() {
return street;
}
public void setStreet(String street) {
this.street = street;
}
@Column(name="CITY")
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
@Column(name="STATE")
public String getState() {
return state;
}
public void setState(String state) {
this.state = state;
}
@Column(name="PINCODE")
public int getPinCode() {
return pinCode;
}
public void setPinCode(int pinCode) {
this.pinCode = pinCode;
}
}
package com.cts.pizza.main;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;
public class HibernateUtil {
private static final SessionFactory sessionFactory = buildSessionFactory();
private static SessionFactory buildSessionFactory() {
try {
// Create the SessionFactory from hibernate.cfg.xml
return new AnnotationConfiguration().configure().buildSessionFactory();
} catch (Throwable ex) {
// Make sure you log the exception, as it might be swallowed
System.err.println("Initial SessionFactory creation failed." + ex);
throw new ExceptionInInitializerError(ex);
}
}
public static SessionFactory getSessionFactory() {
return sessionFactory;
}
public static void shutdown() {
// Close caches and connection pools
getSessionFactory().close();
}
}
package com.cts.pizza.main;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;
import com.cts.pizza.dao.CustomerDAO;
import com.cts.pizza.dao.OrderDAO;
import com.cts.pizza.dao.PizzaDAO;
import com.cts.pizza.dao.PizzaStoreDAO;
import com.cts.pizza.dao.SalesBoyDAO;
import com.cts.pizza.pojo.Address;
import com.cts.pizza.pojo.Customer;
import com.cts.pizza.pojo.MemberShipCard;
import com.cts.pizza.pojo.Pizza;
import com.cts.pizza.pojo.PizzaOrder;
public class PizzaStoreController {
public static void main(String[] args) {
CustomerDAO customer = new CustomerDAO();
// Store Customer Detials starts here
Customer cust = new Customer();
cust.setContactNo("9562055522");
cust.setCustName("Mani");
cust.setEmailId("mani@gmail.com");
Address address = new Address();
address.setCity("Kochi");
address.setDoorNo("3g");
address.setPinCode(682030);
address.setState("Kerala");
address.setStreet("edachira");
customer.storeCustomerDetails(cust, address);
// Store Customer Detials ends here
// Enroll for MemberShipCard starts here
MemberShipCard card = new MemberShipCard();
card.setPointsEarned(100);
customer.enrollForMemberShipCard(cust.getCustomerNo(), card);
// Enroll for MemberShipCard ends here
// Create Pizzas starts here
PizzaDAO pizza = new PizzaDAO();
Set<Pizza> pizzas = new HashSet<Pizza>();
Pizza pizza1 = new Pizza();
pizza1.setPizzaName("Chicken pizza");
pizzas.add(pizza1);
Pizza pizza2 = new Pizza();
pizza2.setPizzaName("Veg pizza");
pizzas.add(pizza2);
pizza.createPizzas(pizzas);
// Create Pizzas ends here
// CreateOrder starts here
OrderDAO order = new OrderDAO();
Set<PizzaOrder> orderObjs = new HashSet<PizzaOrder>();
PizzaOrder pizzaOrder1 = new PizzaOrder();
pizzaOrder1.setOrderDate(new Date());
pizzaOrder1.setTotalAmount(204.3);
pizzaOrder1.setSalesBoyId(null);
orderObjs.add(pizzaOrder1);
PizzaOrder pizzaOrder2 = new PizzaOrder();
pizzaOrder2.setOrderDate(new Date());
pizzaOrder2.setTotalAmount(303.3);
pizzaOrder2.setSalesBoyId(null);
orderObjs.add(pizzaOrder2);
order.createOrder(cust.getCustomerNo(), orderObjs);
// CreateOrder ends here
// Create Oder Detail starts here
order.createOrderDetail(pizzaOrder1.getOrderId(), pizza1.getPizzaId(), 5, 450);
order.createOrderDetail(pizzaOrder1.getOrderId(), pizza2.getPizzaId(), 3, 300);
// Create Oder Detail ends here
// Assign order to salesboy starts here
SalesBoyDAO salesBoyDAO = new SalesBoyDAO();
salesBoyDAO.assignOrderToSalesBoy(pizzaOrder1.getOrderId(), 1);
salesBoyDAO.assignOrderToSalesBoy(pizzaOrder2.getOrderId(), 1);
// Assign order to salesboy ends here
/* PizzaStoreDAO pi = new PizzaStoreDAO();
pi.findTotalDeliveryMadeByEachSalesBoy();
pi.findCustomerWhoPlacedLargestNoOfOrders();*/
//
}
/* public static void main(String[] args) {
PizzaStoreDAO pi = new PizzaStoreDAO();
pi.findTotalDeliveryMadeByEachSalesBoy();
}*/
}
public class CustomerDAO {
public void storeCustomerDetails(Customer customer, Address address) {
Session session = null;
try {
session = HibernateUtil.getSessionFactory().openSession();
Transaction transaction = session.beginTransaction();
customer.setAddress(address);
session.save(customer);
transaction.commit();
} catch (HibernateException e) {
System.out.println("Inside hibernate exception" + e.getMessage());
} finally {
session.close();
}
}
public void enrollForMemberShipCard(int customerId, MemberShipCard card) {
Session session = null;
try {
session = HibernateUtil.getSessionFactory().openSession();
Transaction transaction = session.beginTransaction();
card.setCustomerNo(customerId);
session.save(card);
transaction.commit();
} catch (HibernateException e) {
System.out.println("Inside hibernate exception" + e.getMessage());
} finally {
session.close();
}
}
}
public class OrderDAO {
public void createOrder(int customerId, Set<PizzaOrder> orderObjs) {
Session session = null;
try {
session = HibernateUtil.getSessionFactory().openSession();
for (PizzaOrder pizzaOrder : orderObjs) {
Transaction transaction = session.beginTransaction();
pizzaOrder.setCustomerNo(customerId);
session.save(pizzaOrder);
transaction.commit();
}
} catch (HibernateException e) {
System.out.println("Inside hibernate exception" + e.getMessage());
} finally {
session.close();
}
}
public void createOrderDetail(int orderId, int pizzaId, int quantity, double price){
Session session = null;
try{
session = HibernateUtil.getSessionFactory().openSession();
Transaction transaction = session.beginTransaction();
PizzaOrderDetailId pizzaOrderDetailId = new PizzaOrderDetailId();
pizzaOrderDetailId.setOrderId(orderId);
pizzaOrderDetailId.setPizzaId(pizzaId);
PizzaOrderDetail pizzaOrderDetail = new PizzaOrderDetail();
pizzaOrderDetail.setPizzaOrderDetailId(pizzaOrderDetailId);
pizzaOrderDetail.setPrice(price);
pizzaOrderDetail.setQuantity(quantity);
session.save(pizzaOrderDetail);
transaction.commit();
} catch(HibernateException e){
System.out.println("Inside hibernate exception" + e.getMessage());
} finally{
session.close();
}
}
}
public class PizzaDAO {
public void createPizzas(Set<Pizza> pizzas){
Session session = null;
try {
session = HibernateUtil.getSessionFactory().openSession();
for(Pizza pizza : pizzas){
if(null != pizza){
Transaction transaction = session.beginTransaction();
session.save(pizza);
transaction.commit();
}
}
} catch (HibernateException e){
System.out.println("Inside hibernate exception" + e.getMessage());
} finally {
session.close();
}
}
}
public class PizzaStoreDAO {
public void findTotalDeliveryMadeByEachSalesBoy(){
Session session = null;
List<SalesBoy> salesBoys = new ArrayList<SalesBoy>();
try{
session = HibernateUtil.getSessionFactory().openSession();
Query query = (Query) session.createQuery("from SalesBoy");
salesBoys = query.list();
for(SalesBoy sb : salesBoys){
System.out.println("SalesBoy Id :" + sb.getSalesBoyId() + " made -" + sb.getPizzaOrders().size() + " orders");
}
} catch(HibernateException e){
System.out.println("Inside hibernate exception" +e.getMessage());
} finally{
session.close();
}
}
public void findCustomerWhoPlacedLargestNoOfOrders(){
Session session = null;
try{
session = HibernateUtil.getSessionFactory().openSession();
/* List list = session.createCriteria(Customer.class)
.add(Restrictions.eq("customerNo", customerNo)).list();*/
Criteria cr1 = session.createCriteria(Customer.class);
Criteria cr2 = cr1.createCriteria("pizzaOrders");
cr2.setProjection(Projections.rowCount()).uniqueResult();
List<Customer> list = cr2.list();
System.out.println("hi");
} catch (HibernateException e){
System.out.println("Inside hibernate exception" + e.getMessage());
} finally{
session.close();
}
}
public void getAvailablePizzas(){
}
}
public class SalesBoyDAO {
public void assignOrderToSalesBoy(int orderId, int salesBoyId){
Session session = null;
try{
session = HibernateUtil.getSessionFactory().openSession();
PizzaOrder pizzaOrder = (PizzaOrder) session.get(PizzaOrder.class, orderId);
pizzaOrder.setSalesBoyId(salesBoyId);
Transaction transaction = session.beginTransaction();
session.update(pizzaOrder);
transaction.commit();
} catch (HibernateException e){
System.out.println("Inside hibernate Exception" + e.getMessage());
} finally{
session.close();
}
}
}