Searching...
Sunday, December 27, 2015

Hibernate Delete Record

December 27, 2015


Hibernate Delete Record

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;
}

}


ProductDAO.java

package java9r.dao.com;

import java9r.entites.com.*;
import java9r.util.com.*;
import org.hibernate.*;
import org.hibernate.criterion.Restrictions;

public class ProductDAO {
private SessionFactory sessionFactory=HibernateUtil.getSessionFactory();
 

public Product FindParticularProduct(Integer id){
try {
if(!sessionFactory.getCurrentSession().getTransaction().isActive())
sessionFactory.getCurrentSession().getTransaction().begin();
return (Product)sessionFactory.getCurrentSession().createCriteria(Product.class)
.add(Restrictions.eq("id",id)).uniqueResult();


} catch (Exception e) {
return null;
}
}

public void deleterecord(Product p){
try {
if(!sessionFactory.getCurrentSession().getTransaction().isActive())
sessionFactory.getCurrentSession().getTransaction().begin();
sessionFactory.getCurrentSession().delete(p);
sessionFactory.getCurrentSession().getTransaction().commit();

} catch (Exception e) {
sessionFactory.getCurrentSession().getTransaction().rollback();
throw e;
}
}


}



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) {
Product p=product.FindParticularProduct(7);
product.deleterecord(p);
System.out.println("Successfully Deleted Product 7");


}
}





Output


1 comments:

  1. Great Article. Thank you for sharing! Really an awesome post for every one.

    IEEE Final Year projects Project Centers in Chennai are consistently sought after. Final Year Students Projects take a shot at them to improve their aptitudes, while specialists like the enjoyment in interfering with innovation. For experts, it's an alternate ball game through and through. Smaller than expected IEEE Final Year project centers ground for all fragments of CSE & IT engineers hoping to assemble. Final Year Project Domains for IT It gives you tips and rules that is progressively critical to consider while choosing any final year project point.

    Spring Framework has already made serious inroads as an integrated technology stack for building user-facing applications. Spring Framework Corporate TRaining the authors explore the idea of using Java in Big Data platforms.
    Specifically, Spring Framework provides various tasks are geared around preparing data for further analysis and visualization. Spring Training in Chennai

    ReplyDelete

ads2