Java Tutorial/JPA/Delete
Версия от 17:44, 31 мая 2010; (обсуждение)
Содержание
EJB QL Delete Statement
File: Main.java
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
public class Main {
static EntityManagerFactory emf = Persistence.createEntityManagerFactory("JPAService");
static EntityManager em = emf.createEntityManager();
public static void main(String[] a) throws Exception {
em.getTransaction().begin();
em.createQuery("DELETE FROM Department d WHERE d.name IN ("CA13", "CA19", "NY30")")
.executeUpdate();
em.getTransaction().rumit();
em.close();
emf.close();
Helper.checkData();
}
}
EJB QL Delete With Condition
File: Main.java
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.Query;
public class Main {
static EntityManagerFactory emf = Persistence.createEntityManagerFactory("JPAService");
static EntityManager em = emf.createEntityManager();
public static void main(String[] a) throws Exception {
em.getTransaction().begin();
Query query = em.createQuery("DELETE FROM Student e WHERE e.department IS NULL");
query.executeUpdate();
em.getTransaction().rumit();
em.close();
emf.close();
Helper.checkData();
}
}
Remove a Found Entity
File: Student.java
import java.util.Date;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.Id;
@Entity
public class Student {
@Id
private long id = 0;
private String name;
private Date dateOfBirth = new Date();
@Enumerated(EnumType.ORDINAL)
private Gender gender;
public Date getDateOfBirth() {
return dateOfBirth;
}
public void setDateOfBirth(Date dateOfBirth) {
this.dateOfBirth = dateOfBirth;
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Gender getGender() {
return gender;
}
public void setGender(Gender gender) {
this.gender = gender;
}
public String toString() {
return "\n\nID:" + id + "\nName:" + name + "\nBirthday:" + dateOfBirth + "\nGender:"
+ gender+"\n\n";
}
}
Remove a Just Added Entity
File: Student.java
import java.util.Date;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.Id;
@Entity
public class Student {
@Id
private long id = 0;
private String name;
private Date dateOfBirth = new Date();
@Enumerated(EnumType.ORDINAL)
private Gender gender;
public Date getDateOfBirth() {
return dateOfBirth;
}
public void setDateOfBirth(Date dateOfBirth) {
this.dateOfBirth = dateOfBirth;
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Gender getGender() {
return gender;
}
public void setGender(Gender gender) {
this.gender = gender;
}
public String toString() {
return "\n\nID:" + id + "\nName:" + name + "\nBirthday:" + dateOfBirth + "\nGender:"
+ gender+"\n\n";
}
}