Problemas com hibernate jpa

3 respostas
E

package escola;

import controllerjpa.AlunoJpaController;

public class Escola {

public static void main(String[] args) {

    Aluno a1 = new Aluno();
    
    a1.setNome("Elias");
    a1.setIdade(27);
    
    AlunoJpaController alunoJpaController = new AlunoJpaController();/*aqui tem um erro mostra a msg abaixo:*/
    alunoJpaController.create(a1);
    
}

}

*MSG:

onstructor AlunoJpaController in class controllerjpa.AlunoJpaController cannot be applied to given types;
required: javax.persistence.EntityManagerFactory
found: no arguments
reason: actual and formal argument lists differ in length

(Alt-Enter mostra dicas)

alguem sabe como resolver?

3 Respostas

E

o site do sourceforge.net/projects/hibernate/files/hibernate3/ é bem confuso não consigo encontrar o EntityManagerFactory

starkiller

Cara posta essa tua classe AlunoJpaController, pelo que parece o EntityManagerFactory não foi instanciado, vc dele instancia-lo afim de persistir seus dados na base!

E

esta aqui:

package controllerjpa;

import controllerjpa.exceptions.NonexistentEntityException;

import escola.Aluno;

import java.io.Serializable;

import java.util.List;

import javax.persistence.EntityManager;

import javax.persistence.EntityManagerFactory;

import javax.persistence.Query;

import javax.persistence.EntityNotFoundException;

import javax.transaction.UserTransaction;

/**
*

  • @author 283
    */
    public class AlunoJpaController implements Serializable {

    public AlunoJpaController(UserTransaction utx, EntityManagerFactory emf) {
    
    this.utx = utx;
    
    this.emf = emf;
    
    }
    
    private UserTransaction utx = null;
    
    private EntityManagerFactory emf = null;
    
    public EntityManager getEntityManager() {
    
    return emf.createEntityManager();
    
    }
    
    public void create(Aluno aluno) {
    
    EntityManager em = null;
    
    try {
    
    em = getEntityManager();
    
    em.getTransaction().begin();
    
    em.persist(aluno);
    
    em.getTransaction().commit();
    
    } finally {
    
    if (em != null) {
    
    em.close();
    
    }
    
    }
    
    }
    
    public void edit(Aluno aluno) throws NonexistentEntityException, Exception {
    
    EntityManager em = null;
    
    try {
    
    em = getEntityManager();
    
    em.getTransaction().begin();
    
    aluno = em.merge(aluno);
    
    em.getTransaction().commit();
    
    } catch (Exception ex) {
    
    String msg = ex.getLocalizedMessage();
    
    if (msg == null || msg.length() == 0) {
    
    Long id = aluno.getId();
    
    if (findAluno(id) == null) {
    
    throw new NonexistentEntityException(The aluno with id " + id + " no longer exists.);
    
    }
    
    }
    
    throw ex;
    
    } finally {
    
    if (em != null) {
    
    em.close();
    
    }
    
    }
    
    }
    
    public void destroy(Long id) throws NonexistentEntityException {
    
    EntityManager em = null;
    
    try {
    
    em = getEntityManager();
    
    em.getTransaction().begin();
    
    Aluno aluno;
    
    try {
    
    aluno = em.getReference(Aluno.class, id);
    
    aluno.getId();
    
    } catch (EntityNotFoundException enfe) {
    
    throw new NonexistentEntityException(The aluno with id " + id + " no longer exists., enfe);
    
    }
    
    em.remove(aluno);
    
    em.getTransaction().commit();
    
    } finally {
    
    if (em != null) {
    
    em.close();
    
    }
    
    }
    
    }
    
    public List findAlunoEntities() {
    
    return findAlunoEntities(true, -1, -1);
    
    }
    
    public List findAlunoEntities(int maxResults, int firstResult) {
    
    return findAlunoEntities(false, maxResults, firstResult);
    
    }
    
    private List findAlunoEntities(boolean all, int maxResults, int firstResult) {
    
    EntityManager em = getEntityManager();
    
    try {
    
    Query q = em.createQuery(select object(o) from Aluno as o);
    
    if (!all) {
    
    q.setMaxResults(maxResults);
    
    q.setFirstResult(firstResult);
    
    }
    
    return q.getResultList();
    
    } finally {
    
    em.close();
    
    }
    
    }
    
    public Aluno findAluno(Long id) {
    
    EntityManager em = getEntityManager();
    
    try {
    
    return em.find(Aluno.class, id);
    
    } finally {
    
    em.close();
    
    }
    
    }
    
    public int getAlunoCount() {
    
    EntityManager em = getEntityManager();
    
    try {
    
    Query q = em.createQuery(select count(o) from Aluno as o);
    
    return ((Long) q.getSingleResult()).intValue();
    
    } finally {
    
    em.close();
    
    }
    
    }
    

}

Criado 28 de junho de 2011
Ultima resposta 28 de jun. de 2011
Respostas 3
Participantes 2