Hibernate Count Number of Products
Database
CREATE TABLE `product` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(45) DEFAULT NULL,
`price` decimal(10,0) DEFAULT NULL,
`quantity` int(45) DEFAULT NULL,
`description` varchar(450) DEFAULT NULL,
`photo` varchar(45) DEFAULT NULL,
`active` tinyint(1) DEFAULT NULL,
`createiondate` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`id`)
)
hibernate.cfg.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory >
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.password">ravi</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost/hibernate</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.current_session_context_class">thread</property>
<mapping class="java9r.entites.com.Product"/>
</session-factory>
</hibernate-configuration>
hibernate.reveng.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-reverse-engineering PUBLIC "-//Hibernate/Hibernate Reverse Engineering DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-reverse-engineering-3.0.dtd" >
<hibernate-reverse-engineering>
</hibernate-reverse-engineering>
HibernateUtil.java
package java9r.util.com;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;
@SuppressWarnings("deprecation")
public class HibernateUtil {
private static SessionFactory sessionFactory;
static{
try{
sessionFactory=new AnnotationConfiguration().configure().buildSessionFactory();
}catch (Throwable ex) {
System.err.println("Initial sessionFactory creation failed."+ex);
throw new ExceptionInInitializerError(ex);
}
}
public static SessionFactory getSessionFactory() {
return sessionFactory;
}
}
Product.java
package java9r.entites.com;
import java.util.Date;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import static javax.persistence.GenerationType.IDENTITY;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
@Entity
@Table(name = "product", catalog = "hibernate")
public class Product implements java.io.Serializable {
private Integer id;
private String name;
private Long price;
private Integer quantity;
private String description;
private String photo;
private Boolean active;
private Date createiondate;
public Product() {
}
public Product(Date createiondate) {
this.createiondate = createiondate;
}
public Product(String name, Long price, Integer quantity,
String description, String photo, Boolean active, Date createiondate) {
this.name = name;
this.price = price;
this.quantity = quantity;
this.description = description;
this.photo = photo;
this.active = active;
this.createiondate = createiondate;
}
@Id
@GeneratedValue(strategy = IDENTITY)
@Column(name = "id", unique = true, nullable = false)
public Integer getId() {
return this.id;
}
public void setId(Integer id) {
this.id = id;
}
@Column(name = "name", length = 45)
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
@Column(name = "price", precision = 10, scale = 0)
public Long getPrice() {
return this.price;
}
public void setPrice(Long price) {
this.price = price;
}
@Column(name = "quantity")
public Integer getQuantity() {
return this.quantity;
}
public void setQuantity(Integer quantity) {
this.quantity = quantity;
}
@Column(name = "description", length = 450)
public String getDescription() {
return this.description;
}
public void setDescription(String description) {
this.description = description;
}
@Column(name = "photo", length = 45)
public String getPhoto() {
return this.photo;
}
public void setPhoto(String photo) {
this.photo = photo;
}
@Column(name = "active")
public Boolean getActive() {
return this.active;
}
public void setActive(Boolean active) {
this.active = active;
}
@Temporal(TemporalType.TIMESTAMP)
@Column(name = "createiondate", nullable = false, length = 19)
public Date getCreateiondate() {
return this.createiondate;
}
public void setCreateiondate(Date createiondate) {
this.createiondate = createiondate;
}
}
package java9r.dao.com;
import java9r.entites.com.*;
import java9r.util.com.*;
import java.util.*;
import org.hibernate.*;
public class ProductDAO {
private SessionFactory sessionFactory=HibernateUtil.getSessionFactory();
@SuppressWarnings("unchecked")
public List<Product> countProducts(){
try {
if(!sessionFactory.getCurrentSession().getTransaction().isActive())
sessionFactory.getCurrentSession().getTransaction().begin();
return sessionFactory.getCurrentSession().createCriteria(Product.class)
.list();
} catch (Exception e) {
return null;
}
}
}
ProductMain.java
package java9r.main.com;
import java.util.List;
import java9r.dao.com.*;
import java9r.entites.com.*;
public class ProductMain {
public static void main(String[] args) {
ProductDAO product=new ProductDAO();
List<Product> liProducts=product.countProducts();
System.out.println("Number of Products - " + liProducts.size());
}
}
Output
0 comments:
Post a Comment