EntityManager ?
O banco de dados ta assim no wokbanch: (nomes ficticios)
tentei remover o id "1" e o id "2" , mas quando listei nao tinha removido , alguem sabe pq ?
id email nome
1 saraivaarrobagmail.com saraiva
2 editoraxxarrobagmail.com editoraxx
3 editorazzzarrobahotmail.com editorazz
Code:
package test;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import model.Editora;
public class RemoverObjeto {
public static void main(String[] args) {
EntityManagerFactory factory = Persistence.createEntityManagerFactory("livraria");
EntityManager manager = factory.createEntityManager();
Editora editora1 = manager.find (Editora.class , 1L); // no ID 1 sera removido a editora no caso "saraiva" 1L
manager.remove(editora1);
Editora editora2 = manager.find(Editora.class, 2L); // no 2 3 sera removido a editora no caso " editoraxx" 2L
manager.remove(editora2);
System.out.println("removido: "+ editora2.getNome());
System.out.println("removido: "+ editora1.getNome());
}
}
Code:
package test;
import java.util.List;
import javax.persistence.Entity;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.Query;
import model.Editora;
public class ListaEditoraComJPA {
public static void main(String[] args) {
EntityManagerFactory factory = Persistence.createEntityManagerFactory("livraria");
EntityManager manager = factory.createEntityManager();
Query query = manager.createQuery("SELECT e FROM Editora e");
List<Editora>editoras = query.getResultList();
for(Editora e: editoras){
System.out.println("Editora: "+e.getNome()+" - "+e.getId());
}
}
}
Depois da remocao listando o banco de dados, e ainda continuam os mesmos objetos: pq ?
id email nome
1 saraivaarrobagmail.com saraiva
2 editoraxxarrobagmail.com editoraxx
3 editorazzzarrobahotmail.com editorazz