Program 1
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package beans;
/**
*
* @author admin
*/
public class Product
{
private int prodid;
private String prodname;
private int prodqty;
private int prodamount;
private String prodtype;
public Product() {
}
public int getProdid() {
return prodid;
}
public void setProdid(int prodid) {
this.prodid = prodid;
}
public String getProdname() {
return prodname;
}
public void setProdname(String prodname) {
this.prodname = prodname;
}
public int getProdqty() {
return prodqty;
}
public void setProdqty(int prodqty) {
this.prodqty = prodqty;
}
public int getProdamount() {
return prodamount;
}
public void setProdamount(int prodamount) {
this.prodamount = prodamount;
}
public String getProdtype() {
return prodtype;
}
public void setProdtype(String prodtype) {
this.prodtype = prodtype;
}
}Program 2
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="beans.Product" table="product" schema="productdataflair">
<id name="prodid" column="pid"></id>
<property name="prodname" column="pname"></property>
<property name="prodqty" column="qty"></property>
<property name="prodamount" column="amount"></property>
<property name="prodtype" column="type"></property>
</class>
</hibernate-mapping>Program 3
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost/productdataflair</property>
<property name="connection.username">root</property>
<property name="connection.password">root@data</property>
<property name="connection.pool_size">10</property>
<property name="hbm2ddl.auto">update</property>
<property name="show_sql">true</property>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<mapping resource="resources/product.hbm.xml"/>
</session-factory>
</hibernate-configuration>Program 4
package view;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import java.util.*;
public class TestMain
{
public static void main(String[] args)
{
Configuration cfg=new Configuration();
cfg.configure("resources/hibernate.cfg.xml");
SessionFactory sf=cfg.buildSessionFactory();
Session session=sf.openSession();
String hql;
//hql="select max(prodamount) from Product";
//hql="select min(prodamount) from Product";
//hql="select avg(prodamount) from Product";
//hql="select sum(prodamount) from Product";
hql="select count(prodid) from Product";
Query q=session.createQuery(hql);
List<Integer>mylist=q.list();
System.out.println("Result is : " + mylist.get(0));
session.close();
sf.close();
}
}Program 5
package view;
import beans.Product;
import java.util.*;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
public class TestMain1
{
public static void main(String[] args)
{
Configuration cfg=new Configuration();
cfg.configure("resources/hibernate.cfg.xml");
SessionFactory sf=cfg.buildSessionFactory();
Session session=sf.openSession();
String hql,hql1;
hql="select min(prodamount) from Product where prodtype='Liquid'";
//hql="select min(prodamount) from Product";
Query q=session.createQuery(hql);
int amt;
List<Integer> mylist=q.list();
amt=mylist.get(0);
hql1="from Product where prodamount=:pa";
Query q1=session.createQuery(hql1);
q1.setParameter("pa", amt);
List<Product>mylist1=q1.list();
for(Product P:mylist1)
{
System.out.println(" Id: " + P.getProdid());
System.out.println(" Name: "+ P.getProdname());
System.out.println(" Qty: "+ P.getProdqty());
System.out.println(" Amount: "+ P.getProdamount());
System.out.println(" Type: "+ P.getProdtype());
}
//System.out.println(amt);
session.close();
sf.close();
}
}