Pessoal,
Estou com problema em um mapeamento JPA de Pedido-Itens, funciona perfeitamente a inclusão e atualização, porem não esta funcionando a exclusão de itens.
Como faço para excluir um item do pedido com JPA ?
Estou fazendo da seguinte forma:
//— Classe Principal
import …
public class Main {
/** Creates a new instance of Main */
public Main() {
EntityManagerFactory factory = Persistence.createEntityManagerFactory("TesteJPAPU");
EntityManager em = factory.createEntityManager();
Query qry1 = em.createNamedQuery("Pedidos.findById");
qry1.setParameter("id",new Integer(1));
Pedidos p = (Pedidos) qry1.getSingleResult();
Itens itemExcluir = p.getItensCollection().iterator().next();
p.getItensCollection().remove(itemExcluir);
em.getTransaction().begin();
em.merge(p);
em.getTransaction().commit();
em.close();
factory.close();
}
public static void main(String[] args) {
// TODO code application logic here
new Main();
}
}
//— Classe Pedidos
import …
@Entity
@Table(name = “pedidos”)
@NamedQueries( {
@NamedQuery(name = “Pedidos.findById”, query = “SELECT p FROM Pedidos p WHERE p.id = :id”),
@NamedQuery(name = “Pedidos.findByCliente”, query = “SELECT p FROM Pedidos p WHERE p.cliente = :cliente”)
})
public class Pedidos implements Serializable {
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(name = "id", nullable = false)
private Integer id;
@Column(name = "cliente", nullable = false)
private String cliente;
@OneToMany(cascade=CascadeType.ALL , mappedBy="idPedido")
private Collection<Itens> itensCollection;
/** Creates a new instance of Pedidos */
public Pedidos() {
}
//... getters, setters, equals, hashCode e toString....
//— Classe Itens
import …
@Entity
@Table(name = “itens”)
@NamedQueries( {
@NamedQuery(name = “Itens.findById”, query = “SELECT i FROM Itens i WHERE i.id = :id”),
@NamedQuery(name = “Itens.findByItem”, query = “SELECT i FROM Itens i WHERE i.item = :item”)
})
public class Itens implements Serializable {
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(name = "id", nullable = false)
private Integer id;
@Column(name = "item", nullable = false)
private String item;
@JoinColumn(name="id_pedido", referencedColumnName="id")
@ManyToOne
private Pedidos idPedido;
/** Creates a new instance of Itens */
public Itens() {
}
//... getters, setters, equals, hashCode e toString....
//… Tabelas Mysql
CREATE TABLE test
.pedidos
(
id
int(10) unsigned NOT NULL auto_increment,
cliente
varchar(45) NOT NULL,
PRIMARY KEY (id
)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
CREATE TABLE test
.itens
(
id
int(10) unsigned NOT NULL auto_increment,
id_pedido
int(10) unsigned NOT NULL,
item
varchar(45) NOT NULL,
PRIMARY KEY (id
),
KEY FK_itens_1
(id_pedido
),
CONSTRAINT FK_itens_1
FOREIGN KEY (id_pedido
) REFERENCES pedidos
(id
) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
//… Persistence XML
<?xml version="1.0" encoding="UTF-8"?> oracle.toplink.essentials.ejb.cmp3.EntityManagerFactoryProvider testejpa.pojos.Pedidos testejpa.pojos.ItensSe alguem puder me ajudar, por favor.