[英]persistence JPA
我有一个实体课
公共Candidatos(){}
public Candidatos(Integer identificacion) {
this.identificacion = identificacion;
}
public Integer getIdentificacion() {
return identificacion;
}
public void setIdentificacion(Integer identificacion) {
this.identificacion = identificacion;
}
public String getNombre() {
return nombre;
}
public void setNombre(String nombre) {
this.nombre = nombre;
}
public String getApellido() {
return apellido;
}
public void setApellido(String apellido) {
this.apellido = apellido;
}
public String getCurso() {
return curso;
}
public void setCurso(String curso) {
this.curso = curso;
}
public Integer getVotos() {
return votos;
}
public void setVotos(Integer votos) {
this.votos = votos;
}
@Override
public int hashCode() {
int hash = 0;
hash += (identificacion != null ? identificacion.hashCode() : 0);
return hash;
}
@Override
public boolean equals(Object object) {
// TODO: Warning - this method won't work in the case the id fields are not set
if (!(object instanceof Candidatos)) {
return false;
}
Candidatos other = (Candidatos) object;
if ((this.identificacion == null && other.identificacion != null) || (this.identificacion != null && !this.identificacion.equals(other.identificacion))) {
return false;
}
return true;
}
@Override
public String toString() {
return "entidades.Candidatos[ identificacion=" + identificacion + " ]";
}
}
和我的JPA控制器
公共类CandidatosJpaController实现了Serializable {
public CandidatosJpaController(EntityManagerFactory emf) {
this.emf = emf;
}
private EntityManagerFactory emf = null;
public EntityManager getEntityManager() {
return emf.createEntityManager();
}
public void create(Candidatos candidatos) throws PreexistingEntityException, Exception {
EntityManager em = null;
try {
em = getEntityManager();
em.getTransaction().begin();
em.persist(candidatos);
em.getTransaction().commit();
} catch (Exception ex) {
if (findCandidatos(candidatos.getIdentificacion()) != null) {
throw new PreexistingEntityException("Candidatos " + candidatos + " already exists.", ex);
}
throw ex;
} finally {
if (em != null) {
em.close();
}
}
}
public void edit(Candidatos candidatos) throws NonexistentEntityException, Exception {
EntityManager em = null;
try {
em = getEntityManager();
em.getTransaction().begin();
candidatos = em.merge(candidatos);
em.getTransaction().commit();
} catch (Exception ex) {
String msg = ex.getLocalizedMessage();
if (msg == null || msg.length() == 0) {
Integer id = candidatos.getIdentificacion();
if (findCandidatos(id) == null) {
throw new NonexistentEntityException("The candidatos with id " + id + " no longer exists.");
}
}
throw ex;
} finally {
if (em != null) {
em.close();
}
}
}
public void destroy(Integer id) throws NonexistentEntityException {
EntityManager em = null;
try {
em = getEntityManager();
em.getTransaction().begin();
Candidatos candidatos;
try {
candidatos = em.getReference(Candidatos.class, id);
candidatos.getIdentificacion();
} catch (EntityNotFoundException enfe) {
throw new NonexistentEntityException("The candidatos with id " + id + " no longer exists.", enfe);
}
em.remove(candidatos);
em.getTransaction().commit();
} finally {
if (em != null) {
em.close();
}
}
}
public List<Candidatos> findCandidatosEntities() {
return findCandidatosEntities(true, -1, -1);
}
public List<Candidatos> findCandidatosEntities(int maxResults, int firstResult) {
return findCandidatosEntities(false, maxResults, firstResult);
}
private List<Candidatos> findCandidatosEntities(boolean all, int maxResults, int firstResult) {
EntityManager em = getEntityManager();
try {
CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
cq.select(cq.from(Candidatos.class));
Query q = em.createQuery(cq);
if (!all) {
q.setMaxResults(maxResults);
q.setFirstResult(firstResult);
}
return q.getResultList();
} finally {
em.close();
}
}
public Candidatos findCandidatos(Integer id) {
EntityManager em = getEntityManager();
try {
return em.find(Candidatos.class, id);
} finally {
em.close();
}
}
public int getCandidatosCount() {
EntityManager em = getEntityManager();
try {
CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
Root<Candidatos> rt = cq.from(Candidatos.class);
cq.select(em.getCriteriaBuilder().count(rt));
Query q = em.createQuery(cq);
return ((Long) q.getSingleResult()).intValue();
} finally {
em.close();
}
}
}
但是当我实例课时
public static void main(String[] args) {
Candidatos candi = new Candidatos();
candi.setIdentificacion(1);
candi.setNombre("juan");
candi.setApellido("ovalle");
candi.setCurso("1001");
candi.setVotos(1);
CandidatosJpaController control = new CandidatosJpaController();
说我,类CandidatosJpaController中的构造函数CandidatosJpaController无法应用于给定类型,要求:EntityManagerFactory。
发生什么事? 因为如果我创建一个空的构造函数,请对我说:线程“主”中的异常java.lang.UnsupportedOperationException:不支持。 在controladores.CandidatosJpaController。(CandidatosJpaController.java:31)在votaciones.Votaciones.main(Votaciones.java:26)
您还需要解决方案吗? 或者,也许其他用户需要它。
更改您的构造函数:
CandidatosJpaController control = new CandidatosJpaController(Persistence.createEntityManagerFactory("XXX"));
将XXX
值替换为您的持久性单元名称,请参阅persistence.xml
以了解它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.