hi, can u please tell me SQL DDL queries to create Product and Dealer table... and also tell me java code for Product and Dealer bean...
Exmple of Hibernate Associations and Joins using Javadb / derby database
HibernateUtil.java file
import org.hibernate.HibernateException; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.cfg.Configuration; public class HibernateUtil { public static final SessionFactory sessionFactory; static { try { // Create the SessionFactory from hibernate.cfg.xml sessionFactory = new Configuration().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 final ThreadLocal session = new ThreadLocal(); public static Session currentSession() throws HibernateException { Session s = (Session) session.get(); // Open a new Session, if this thread has none yet if (s == null) { s = sessionFactory.openSession(); // Store it in the ThreadLocal variable session.set(s); } return s; } public static void closeSession() throws HibernateException { Session s = (Session) session.get(); if (s != null) s.close(); session.set(null); } }
Main.java File
import java.util.*; import org.hibernate.*; public class Main { public static void main(String[] args) { Session session = HibernateUtil.currentSession(); String hql = "from Product p inner join p.supplier as s"; Query query = session.createQuery(hql); List results = query.list(); displayObjectsList(results); } static public void displayObjectsList(List list) { Iterator iter = list.iterator(); if (!iter.hasNext()) { System.out.println("No objects to display."); return; } while (iter.hasNext()) { System.out.println("---------New object-------------"); Object[] obj = (Object[]) iter.next(); Product p = (Product)obj[0]; Supplier s = (Supplier)obj[1]; System.out.println("Product ID :"+p.getId()); System.out.println("Product Name :"+p.getName()); System.out.println("Product Price :"+p.getPrice()); System.out.println("Supplier ID:"+s.getId()); System.out.println("Supplier Name :"+s.getName()); } } }
Product.java
public class Product { private int id; private Supplier supplier; private String name; private double price; public Product() { super(); } public Product(String name, double price) { super(); this.name = name; this.price = price; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Supplier getSupplier() { return supplier; } public void setSupplier(Supplier supplier) { this.supplier = supplier; } public double getPrice() { return price; } public void setPrice(double price) { this.price = price; } }
Supplier.java
import java.util.ArrayList; import java.util.List; public class Supplier { private int id; private String name; private List products = new ArrayList(); public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public List getProducts() { return products; } public void setProducts(List products) { this.products = products; } }
Hibernate.cfg.xml
<?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> <!-- Database connection settings --> <property name="hibernate.connection.driver_class">org.apache.derby.jdbc.EmbeddedDriver</property> <property name="hibernate.connection.url">jdbc:derby://localhost:1527/sample;create=true;autocommit=true</property> <property name="hibernate.connection.username">NarendraMah</property> <property name="hibernate.connection.schema">APP</property> <property name="hibernate.connection.password">NarendraMah</property> <property name="hibernate.connection.pool_size">10</property> <property name="hibernate.connection.autommit">true</property> <property name="show_sql">true</property> <property name="dialect">org.hibernate.dialect.DerbyDialect</property> <property name="hibernate.hbm2ddl.auto">update</property> <!-- Mapping files --> <mapping resource="Product.hbm.xml"/> <mapping resource="Supplier.hbm.xml"/> </session-factory> </hibernate-configuration>
Product.xml
<?xml version='1.0' encoding='utf-8'?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <hibernate-mapping> <class name="Product" table="APP.PRODUCT"> <id name="id" type="int" column="ID"> <generator class="increment"/> </id> <property name="name" type="string" column="NAME"/> <property name="price" type="double" column="PRICE"/> <many-to-one name="supplier" class="Supplier" column="SUPPLIERID"/> </class> <query name="HQLpricing"><![CDATA[ select product.price from Product product where product.price > 25.0]]> </query> <sql-query name="SQLpricing"> <return-scalar column="price" type="double"/> <![CDATA[ select product.price from Product as product where product.price > 25.0]]> </sql-query> </hibernate-mapping>
Supplier.hbm.xml
<?xml version='1.0' encoding='utf-8'?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <hibernate-mapping> <class name="Supplier" table="APP.SUPPLIER"> <id name="id" type="int" column="ID"> <generator class="increment"/> </id> <property name="name" type="string" column="NAME"/> <bag name="products" inverse="true" cascade="all,delete-orphan"> <key column="supplierId"/> <one-to-many class="Product"/> </bag> </class> </hibernate-mapping>
SQL Queries
create table APP.SUPPLIER ( ID INTEGER NOT NULL PRIMARY KEY, NAME VARCHAR(20) NOT NULL ); create table APP.PRODUCT ( ID INTEGER NOT NULL PRIMARY KEY, NAME VARCHAR(20) NOT NULL , PRICE DOUBLE NOT NULL, SUPPLIERID integer not null ); insert into APP.SUPPLIER values(1,'One Services'); insert into APP.SUPPLIER values(2,'XLS Services'); insert into APP.SUPPLIER values(3,'Infocom Services'); insert into APP.PRODUCT values(1,'Hard Disk',2000.00,1); insert into APP.PRODUCT values(2,'CD',100.00,2); insert into APP.PRODUCT values(3,'Processor',10000.00,3); insert into APP.PRODUCT values(4,'Cable',150.00,1); insert into APP.PRODUCT values(5,'USB Drive',800.00,2); insert into APP.PRODUCT values(6,'Mouse',200.00,3);
Exmple of Associations and Joins using Javadb / derby database
HibernateUtil.java file
import org.hibernate.HibernateException; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.cfg.Configuration; public class HibernateUtil { public static final SessionFactory sessionFactory; static { try { // Create the SessionFactory from hibernate.cfg.xml sessionFactory = new Configuration().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 final ThreadLocal session = new ThreadLocal(); public static Session currentSession() throws HibernateException { Session s = (Session) session.get(); // Open a new Session, if this thread has none yet if (s == null) { s = sessionFactory.openSession(); // Store it in the ThreadLocal variable session.set(s); } return s; } public static void closeSession() throws HibernateException { Session s = (Session) session.get(); if (s != null) s.close(); session.set(null); } }
Main.java File
import java.util.*; import org.hibernate.*; public class Main { public static void main(String[] args) { Session session = HibernateUtil.currentSession(); String hql = "from Product p inner join p.supplier as s"; Query query = session.createQuery(hql); List results = query.list(); displayObjectsList(results); } static public void displayObjectsList(List list) { Iterator iter = list.iterator(); if (!iter.hasNext()) { System.out.println("No objects to display."); return; } while (iter.hasNext()) { System.out.println("---------New object-------------"); Object[] obj = (Object[]) iter.next(); Product p = (Product)obj[0]; Supplier s = (Supplier)obj[1]; System.out.println("Product ID :"+p.getId()); System.out.println("Product Name :"+p.getName()); System.out.println("Product Price :"+p.getPrice()); System.out.println("Supplier ID:"+s.getId()); System.out.println("Supplier Name :"+s.getName()); } } }
Product.java
public class Product { private int id; private Supplier supplier; private String name; private double price; public Product() { super(); } public Product(String name, double price) { super(); this.name = name; this.price = price; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Supplier getSupplier() { return supplier; } public void setSupplier(Supplier supplier) { this.supplier = supplier; } public double getPrice() { return price; } public void setPrice(double price) { this.price = price; } }
Supplier.java
import java.util.ArrayList; import java.util.List; public class Supplier { private int id; private String name; private List products = new ArrayList(); public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public List getProducts() { return products; } public void setProducts(List products) { this.products = products; } }
Product.xml
<?xml version='1.0' encoding='utf-8'?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <hibernate-mapping> <class name="Product" table="APP.PRODUCT"> <id name="id" type="int" column="ID"> <generator class="increment"/> </id> <property name="name" type="string" column="NAME"/> <property name="price" type="double" column="PRICE"/> <many-to-one name="supplier" class="Supplier" column="SUPPLIERID"/> </class> <query name="HQLpricing"><![CDATA[ select product.price from Product product where product.price > 25.0]]> </query> <sql-query name="SQLpricing"> <return-scalar column="price" type="double"/> <![CDATA[ select product.price from Product as product where product.price > 25.0]]> </sql-query> </hibernate-mapping>
Supplier.hbm.xml
<?xml version='1.0' encoding='utf-8'?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <hibernate-mapping> <class name="Supplier" table="APP.SUPPLIER"> <id name="id" type="int" column="ID"> <generator class="increment"/> </id> <property name="name" type="string" column="NAME"/> <bag name="products" inverse="true" cascade="all,delete-orphan"> <key column="supplierId"/> <one-to-many class="Product"/> </bag> </class> </hibernate-mapping>
SQL Queries
create table APP.SUPPLIER ( ID INTEGER NOT NULL PRIMARY KEY, NAME VARCHAR(20) NOT NULL ); create table APP.PRODUCT ( ID INTEGER NOT NULL PRIMARY KEY, NAME VARCHAR(20) NOT NULL , PRICE DOUBLE NOT NULL, SUPPLIERID integer not null ); insert into APP.SUPPLIER values(1,'One Services'); insert into APP.SUPPLIER values(2,'XLS Services'); insert into APP.SUPPLIER values(3,'Infocom Services'); insert into APP.PRODUCT values(1,'Hard Disk',2000.00,1); insert into APP.PRODUCT values(2,'CD',100.00,2); insert into APP.PRODUCT values(3,'Processor',10000.00,3); insert into APP.PRODUCT values(4,'Cable',150.00,1); insert into APP.PRODUCT values(5,'USB Drive',800.00,2); insert into APP.PRODUCT values(6,'Mouse',200.00,3);
Ads