/*
* HibernateDataPopulate.java
*
* Created on 2006年10月4日, 上午3:27
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/
package org.springframework.samples.jpetstore.hibernate.utils;
import java.math.BigDecimal;
import java.util.Date;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.springframework.samples.jpetstore.domain.Account;
import org.springframework.samples.jpetstore.domain.Address;
import org.springframework.samples.jpetstore.domain.Banner;
import org.springframework.samples.jpetstore.domain.Category;
import org.springframework.samples.jpetstore.domain.Inventory;
import org.springframework.samples.jpetstore.domain.Item;
import org.springframework.samples.jpetstore.domain.Product;
import org.springframework.samples.jpetstore.domain.Supplier;
/**
* don't worked.
*
* @author pprun
*/
public class HibernateDataPopulate {
private static Log log = LogFactory.getLog(HibernateDataPopulate.class);
/** Creates a new instance of HibernateDataPopulate */
public HibernateDataPopulate() {
}
public static void main(String[] args) {
log.info("Start populate hjpetstore database data ...");
Session session = null;
try {
// get session
session = HibernateUtil.getSessionFactory().openSession();
// begin transaction
session.beginTransaction();
// persist entity to db
insertCategories(session);
insertBanners(session);
insertUsers(session);
insertSuppliers(session);
insertProducts(session);
insertItems(session);
insertInventories(session);
// commit and end current transaction
session.getTransaction().commit();
log.info("Successfully populate hjpetstore database data!");
System.out.println("Successfully populate hjpetstore database data!");
} catch (Throwable t) {
if (session.getTransaction() != null) {
session.getTransaction().rollback();
}
log.info("Failly populate hjpetstore database data!");
System.out.println("Failly populate hjpetstore database data!");
} finally {
if (session != null) session.close();
}
}
private static void insertCategories(Session s) {
log.info("populating Category ...");
// 1
Category c = new Category();
c.setCategoryName("Fish");
c.setCategoryDesc("<image src=\"../images/fish_icon.gif\"><font size=\"5\" color=\"blue\"> Fish</font>");
s.persist(c);
//s.flush();
// 2
c = null;
c = new Category();
c.setCategoryName("DogS");
c.setCategoryDesc("<image src=\"../images/dogs_icon.gif\"><font size=\"5\" color=\"blue\"> Dogs</font>");
s.persist(c);
//s.flush();
// 3
c = null;
c = new Category();
c.setCategoryName("REPTILES");
c.setCategoryDesc("<image src=\"../images/reptiles_icon.gif\"><font size=\"5\" color=\"blue\"> Reptiles</font>");
s.persist(c);
//s.flush();
// 4
c = null;
c = new Category();
c.setCategoryName("Cats");
c.setCategoryDesc("<image src=\"../images/cats_icon.gif\"><font size=\"5\" color=\"blue\"> Cats</font>");
s.persist(c);
//s.flush();
// 5
c = null;
c = new Category();
c.setCategoryName("Birds");
c.setCategoryDesc("<image src=\"../images/birds_icon.gif\"><font size=\"5\" color=\"blue\"> Birds</font>");
s.persist(c);
//s.flush();
log.info("populating Category end.");
}
private static void insertBanners(Session s) {
log.info("populating Banner ...");
Category c1 = (Category)s.get(Category.class, Long.valueOf(1));
Category c2 = (Category)s.get(Category.class, Long.valueOf(2));
Category c3 = (Category)s.get(Category.class, Long.valueOf(3));
Category c4 = (Category)s.get(Category.class, Long.valueOf(4));
Category c5 = (Category)s.get(Category.class, Long.valueOf(5));
// 1
Banner b = new Banner();
b.setFavCategory(c1);
b.setBannerName("<image src=\"../images/banner_fish.gif\">");
s.persist(b);
//s.flush();
// 2
b = null;
b = new Banner();
b.setFavCategory(c4);
b.setBannerName("<image src=\"../images/banner_cats.gif\">");
s.persist(b);
//s.flush();
// 3
b = null;
b = new Banner();
b.setFavCategory(c3);
b.setBannerName("<image src=\"../images/banner_dogs.gif\">");
s.persist(b);
//s.flush();
// 4
b = null;
b = new Banner();
b.setFavCategory(c2);
b.setBannerName("<image src=\"../images/banner_reptiles.gif\">");
s.persist(b);
//s.flush();
// 5
b = null;
b = new Banner();
b.setFavCategory(c5);
b.setBannerName("<image src=\"../images/banner_birds.gif\">");
s.persist(b);
//s.flush();
log.info("populating Banner end.");
}
private static void insertUsers(Session s) {
log.info("populating Account ...");
Address ad = new Address();
ad.setAddr1("901 San Antonio Road");
ad.setAddr2("MS UCUP02-206");
ad.setCity("Palo Alto");
ad.setState("CA");
ad.setZipcode("94303");
ad.setCountry("USA");
Category c1 = (Category)s.get(Category.class, Long.valueOf(3));
Category c2 = (Category)s.get(Category.class, Long.valueOf(4));
// 1
Account a = new Account();
a.setUsername("j2ee");
a.setPassword("j2ee");
a.setEmail("admin@pprun.org");
a.setFirstname("ABC");
a.setLastname("XYX");
a.setStatus("OK");
a.setUserAddr(ad);
a.setPhone("555-555-5555");
a.setLangPreference("english");
a.setFavCategory(c1);
a.setDisplayMylist(true);
a.setDisplayBanner(true);
s.persist(a);
s.flush();
// 2
a = null;
a = new Account();
a.setUsername("ACID");
a.setPassword("ACID");
a.setEmail("admin@pprun.org");
a.setFirstname("ABC");
a.setLastname("XYX");
a.setStatus("OK");
a.setUserAddr(ad);
a.setPhone("555-555-5555");
a.setLangPreference("english");
a.setFavCategory(c2);
a.setDisplayMylist(true);
a.setDisplayBanner(true);
s.persist(a);
s.flush();
log.info("populating Account end.");
}
private static void insertSuppliers(Session s) {
log.info("populating Supplier ...");
// 1
Address ad = new Address();
ad.setAddr1("600 Avon Way");
ad.setAddr2("");
ad.setCity("Los Angeles");
ad.setState("CA");
ad.setZipcode("94024");
ad.setCountry("USA");
// 1
Supplier sp = new Supplier();
sp.setSupplierName("XYZ Pets");
sp.setStatus("AC");
sp.setSupplierAddr(ad);
sp.setPhone("212-947-0797");
s.persist(sp);
s.flush();
// 2
ad = null;
ad = new Address();
ad.setAddr1("700 Abalone Way");
ad.setAddr2("");
ad.setCity("San Francisco");
ad.setState("CA");
ad.setZipcode("94024");
ad.setCountry("USA");
sp = null;
sp = new Supplier();
sp.setSupplierName("ABC Pets");
sp.setStatus("AC");
sp.setSupplierAddr(ad);
sp.setPhone("415-947-0797");
s.persist(sp);
s.flush();
log.info("populating Supplier end.");
}
private static void insertProducts(Session s) {
log.info("populating Product ...");
Category c1 = (Category)s.get(Category.class, Long.valueOf(1));
Category c2 = (Category)s.get(Category.class, Long.valueOf(2));
Category c3 = (Category)s.get(Category.class, Long.valueOf(3));
Category c4 = (Category)s.get(Category.class, Long.valueOf(4));