【问题标题】:persistence JPA持久性 JPA
【发布时间】:2013-03-07 23:28:48
【问题描述】:

我有一个实体类

公共候选人(){ }

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)

【问题讨论】:

  • 我认为错误信息很清楚。没有无参数构造函数。
  • 我不想听起来粗鲁,但 JPA 相当复杂。而且,如果您不掌握 Java 的基础知识(如构造函数参数),那么您每次都会碰壁。首先通过简单的程序学习 Java 基础知识。
  • 不能应用于给定类型,需要:EntityManagerFactory 表示您正在将其他内容而不是 EntityManagerFactory 传递给 CandidatosJpaController custructor

标签: java jpa persistence


【解决方案1】:

您还需要解决方案吗?或者其他用户可能需要它。

用这个改变你的构造函数:

CandidatosJpaController control = new CandidatosJpaController(Persistence.createEntityManagerFactory("XXX"));

XXX 值替换为您的持久性单元名称,请参阅您的 persistence.xml 以了解它。

【讨论】:

    猜你喜欢
    • 2018-05-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-17
    • 2017-08-18
    • 1970-01-01
    • 2010-11-25
    • 2014-09-08
    相关资源
    最近更新 更多