[英]Java persistence api not deleting / removing record oracle database 11g XE
我已使用Java Persistence API成功创建,合并和提取了记录,但无法删除记录。 我使用了em.createNativeQuery(“ Delete ...”),它运行良好,问题是我想使用实体管理器来做到这一点。 我认为问题可能出在oracle数据库上,因为我在Derby上尝试了上述代码,并且运行良好。
public void destroy(String id) throws NonexistentEntityException, RollbackFailureException, Exception {
EntityManager em = null;
Context initCtx = new InitialContext();
utx = (UserTransaction) initCtx.lookup("java:comp/env/UserTransaction");
try {
utx.begin();
em = getEntityManager();
//Query query = em.createNativeQuery("DELETE FROM SERVICIO WHERE SERVICIODESCRIPCION = '"+id+"'");
//query.executeUpdate();
Servicio servicio;
try {
servicio = em.getReference(Servicio.class, id);
servicio.getServiciodescripcion();
} catch (EntityNotFoundException enfe) {
throw new NonexistentEntityException("The servicio with id " + id + " no longer exists.", enfe);
}
em.remove(servicio);
utx.commit();
} catch (Exception ex) {
try {
utx.rollback();
} catch (Exception re) {
throw new RollbackFailureException("An error occurred attempting to roll back the transaction.", re);
}
throw ex;
} finally {
if (em != null) {
em.flush();
em.close();
}
}
}
实际上,问题在于我的主键的数据类型设置为char(10),我解决了将其更改为VARCHAR(10)的问题,因此,总之,在使用Oracle数据库,JPA和Oracle时,主键设置为文本,应该使用的数据类型是VARCHAR而不是CHAR,因此实体管理器可以正常工作。 谢谢。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.