Home Jpa Netbeansjpaexamples Delete All Records

Related Tutorials


 
 

Share on Google+Share on Google+

Delete All Records

Advertisement
In this section, you will learn how to delete all record using jpa.

Delete All Records

     

In this section, you will learn how to delete all record using jpa. You need the following artifacts:

  1. Database table: student
  2. Model Class: Student.java
  3. Main Class: JPADeleteAllRecords.java
  4. And its required jar files.

Table: student

CREATE TABLE `student` ( 
`id` int(11) NOT NULL auto_increment, 
`sname` varchar(40) NOT NULL, 
`sroll` int(11) NOT NULL, 
`scourse` varchar(10) default NULL, 
PRIMARY KEY (`id`) 
)

Model Class: Student.java

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

package jpacrud;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Table;

/**
*
* @author Administrator
*/
@Entity
@Table(name="student")

@NamedQueries({
@NamedQuery(name="readAllRecords",query="SELECT st FROM Student st"),
@NamedQuery(name="updateRecord",query="UPDATE Student st SET st.sname= ?1 WHERE st.sroll= ?2")
})
public class Student implements java.io.Serializable {

@Id
@GeneratedValue
private int id;

public int getId() {
return id;
}

public void setId(int id) {
this.id = id;
}

@Column(name="sname",length=40,nullable=false)
private String sname;

public String getSname() {
return sname;
}

public void setSname(String sname) {
this.sname = sname;
}

@Column(name="sroll",nullable=false)
private int sroll;

public int getSroll() {
return sroll;
}

public void setSroll(int sroll) {
this.sroll = sroll;
}

@Column(name="scourse",length=10,nullable=true)
private String scourse;

public String getScourse() {
return scourse;
}

public void setScourse(String scourse) {
this.scourse = scourse;
}

}

Main Class: JPADeleteAllRecords.java

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

package jpacrud;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.Persistence;
import javax.persistence.Query;

/**
*
* @author Administrator
*/
public class JPADeleteAllRecords {
public static void main(String arg[]){
EntityManagerFactory emf=Persistence.createEntityManagerFactory("netjpa");
EntityManager em=emf.createEntityManager();
try{
EntityTransaction entr=em.getTransaction();
entr.begin();
Query query=em.createQuery("DELETE FROM Student st");
int deleteRecords=query.executeUpdate();
entr.commit();
System.out.println("All records are deleted.");
}
catch(Exception ex){
System.out.println(ex.getMessage());
}
finally{
em.close();
}
}
}

Output:

init:
deps-jar:
compile-single:
run-single:
log4j:WARN No appenders could be found for logger (org.hibernate.cfg.annotations.Version).
log4j:WARN Please initialize the log4j system properly.
Hibernate: delete from student
All records are deleted.
BUILD SUCCESSFUL (total time: 5 seconds)

Download Application

Advertisement

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: June 15, 2009

Related Tutorials

Discuss: Delete All Records   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:1
ali reza
March 29, 2011
question?

i need visual tutorial in netbeans IDE but i can't find anything in netbeans by visual learning i had found visual learning java by netbeans IDE in this site several month ago where can i find visual learning java by netbeans IDE?
DMCA.com