Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
280 views
in Technique[技术] by (71.8m points)

java - org.hibernate.InstantiationException: No default constructor for entity: : principal.Cliente

I'm having this error:

Exception in thread "main" org.hibernate.InstantiationException: No default constructor for entity:  : principal.Cliente
   at org.hibernate.tuple.PojoInstantiator.instantiate(PojoInstantiator.java:120)
   at org.hibernate.tuple.PojoInstantiator.instantiate(PojoInstantiator.java:136)
   at org.hibernate.tuple.entity.AbstractEntityTuplizer.instantiate(AbstractEntityTuplizer.java:737)
   at org.hibernate.persister.entity.AbstractEntityPersister.instantiate(AbstractEntityPersister.java:4755)
   at org.hibernate.internal.SessionImpl.instantiate(SessionImpl.java:1387)
   at org.hibernate.internal.SessionImpl.instantiate(SessionImpl.java:1375)
   at org.hibernate.loader.plan.exec.process.internal.EntityReferenceInitializerImpl.hydrateEntityState(EntityReferenceInitializerImpl.java:235)
   at org.hibernate.loader.plan.exec.process.internal.AbstractRowReader.readRow(AbstractRowReader.java:107)
   at org.hibernate.loader.plan.exec.internal.EntityLoadQueryDetails$EntityLoaderRowReader.readRow(EntityLoadQueryDetails.java:255)
   at org.hibernate.loader.plan.exec.process.internal.ResultSetProcessorImpl.extractResults(ResultSetProcessorImpl.java:129)
   at org.hibernate.loader.plan.exec.internal.AbstractLoadPlanBasedLoader.executeLoad(AbstractLoadPlanBasedLoader.java:138)
   at org.hibernate.loader.plan.exec.internal.AbstractLoadPlanBasedLoader.executeLoad(AbstractLoadPlanBasedLoader.java:102)
   at org.hibernate.loader.entity.plan.AbstractLoadPlanBasedEntityLoader.load(AbstractLoadPlanBasedEntityLoader.java:186)
   at org.hibernate.persister.entity.AbstractEntityPersister.load(AbstractEntityPersister.java:4120)
   at org.hibernate.event.internal.DefaultLoadEventListener.loadFromDatasource(DefaultLoadEventListener.java:502)
   at org.hibernate.event.internal.DefaultLoadEventListener.doLoad(DefaultLoadEventListener.java:467)
   at org.hibernate.event.internal.DefaultLoadEventListener.load(DefaultLoadEventListener.java:212)
   at org.hibernate.event.internal.DefaultLoadEventListener.proxyOrLoad(DefaultLoadEventListener.java:274)
   at org.hibernate.event.internal.DefaultLoadEventListener.onLoad(DefaultLoadEventListener.java:150)
   at org.hibernate.internal.SessionImpl.fireLoad(SessionImpl.java:1066)
   at org.hibernate.internal.SessionImpl.access$2000(SessionImpl.java:176)
   at org.hibernate.internal.SessionImpl$IdentifierLoadAccessImpl.load(SessionImpl.java:2540)
   at org.hibernate.internal.SessionImpl.get(SessionImpl.java:951)
   at principal.ClienteDAO.obtenContacto(ClienteDAO.java:83)
   at principal.Main.main(Main.java:43)

Have no idea why im getting that exception

im running main

public static void main(String[] args) {

    int idAEliminar = 2;
    ClienteDAO clienteDAO = new ClienteDAO(); 
    GenericDAO genericDAO = new GenericDAO();
    Cliente contactoRecuperado;

    //Creamos tes instancias de Contacto  (String nombre, String direccion, int codigoPostal, String telefono, String cuit)
    Cliente cliente1 = new Cliente("Pedro", "dr clark 385", 3260, "86484","15531"); 
    Cliente cliente2 = new Cliente("Manuel", "dr clark 385", 3260, "86484","15531"); 
    Cliente cliente3 = new Cliente("Martin", "dr clark 385", 3260, "86484","15531");
    Cliente cliente4 = new Cliente("Josecito", "dr clark 385", 3260, "86484","15531");

    //Guardamos las tres instancias, guardamos el id del contacto1 para usarlo posteriormente 
    idAEliminar = clienteDAO.guardaCliente(cliente1); 
    clienteDAO.guardaCliente(cliente2);
    clienteDAO.guardaCliente(cliente3);

    //Modificamos el contacto 2 y lo actualizamos         
    cliente2.setNombre("Nuevo Contcto 2");
    clienteDAO.actualizaCliente(cliente2);
    //genericDAO.save(cliente4);
    //Recuperamos el contacto1 de la base de datos 
    //contactoRecuperado = genericDAO.get(Cliente.class, idAEliminar);
    contactoRecuperado = clienteDAO.obtenContacto(idAEliminar);
    System.out.println("Recuperamos a " + contactoRecuperado.getNombre());

and this it my classDAO

public class ClienteDAO {  
private Session session; 
private Transaction tx;  

public int guardaCliente(Cliente cliente) throws HibernateException 
{ 
    int id = 0;  

    try 
    { 
        iniciaOperacion(); 
        id = (int) session.save(cliente); 
        tx.commit(); 
    } catch (HibernateException he) 
    { 
        manejaExcepcion(he); 
        throw he; 
    } finally 
    { 
        session.close(); 
    }  

    return id; 
}  

public void actualizaCliente(Cliente cliente) throws HibernateException 
{ 
    try 
    { 
        iniciaOperacion(); 
        session.update(cliente); 
        tx.commit(); 
    } catch (HibernateException he) 
    { 
        manejaExcepcion(he); 
        throw he; 
    } finally 
    { 
        session.close(); 
    } 
}  

public void eliminaCliente (Cliente cliente) throws HibernateException 
{ 
    try 
    { 
        iniciaOperacion(); 
        session.delete(cliente); 
        tx.commit(); 
    } catch (HibernateException he) 
    { 
        manejaExcepcion(he); 
        throw he; 
    } finally 
    { 
        session.close(); 
    } 
}  

public Cliente obtenContacto(int codCliente) throws HibernateException 
{ 
   Cliente cliente = null;  
    try 
    { 
        iniciaOperacion(); 
        cliente = (Cliente) session.get(Cliente.class, codCliente); 
    } finally 
    { 
        session.close(); 
    }  

    return cliente; 
}  

public List<Cliente> obtenListaContactos() throws HibernateException 
{ 
    List<Cliente> listaContactos = null;  

    try 
    { 
        iniciaOperacion(); 
        listaContactos = session.createQuery("from Contacto").list(); 
    } finally 
    { 
        session.close(); 
    }  

    return listaContactos; 
}  

private void iniciaOperacion() throws HibernateException 
{ 
    session = HibernateUtil.getSessionFactory().openSession(); 
    tx = session.beginTransaction(); 
}  

private void manejaExcepcion(HibernateException he) throws HibernateException 
{ 
    tx.rollback(); 
    throw new HibernateException("Ocurrió un error en la capa de acceso a datos", he); 
} 
}

When i save, update and eliminate it works perfectly, so i dont know what im doing wrong

EDIT1 here is principal.Cliente

public class Cliente {
private String nombre, direccion, telefono, cuit;
private int codigo, codigoPostal;
private double saldo, deuda;

public Cliente(String nombre, String direccion, int codigoPostal, String telefono, String cuit) {
    this.nombre = nombre;
    this.direccion = direccion;
    this.codigoPostal = codigoPostal;
    this.telefono = telefono;
    this.cuit = cuit;
    this.saldo = 0;
    this.deuda = 0;
}




public String getNombre() {
    return nombre;
}

public void setNombre(String nombre) {
    this.nombre = nombre;
}

public String getDireccion() {
    return direccion;
}

public void setDireccion(String direccion) {
    this.direccion = direccion;
}

public int getCodigoPostal() {
    return codigoPostal;
}

public void setCodigoPostal(int codigoPostal) {
    this.codigoPostal = codigoPostal;
}

public String getTelefono() {
    return telefono;
}

public void setTelefono(String telefono) {
    this.telefono = telefono;
}

public String getCuit() {
    return cuit;
}

public void setCuit(String cuit) {
    this.cuit = cuit;
}

public int getCodigo() {
    return codigo;
}

private void setCodigo(int codigo) {
    this.codigo = codigo;
}

public double getSaldo() {
    return saldo;
}

public void setSaldo(double saldo) {
    this.saldo = saldo;
}

public double getDeuda() {
    return deuda;
}

public void setDeuda(double deuda) {
    this.deuda = deuda;
}



}
question from:https://stackoverflow.com/questions/44088360/org-hibernate-instantiationexception-no-default-constructor-for-entity-princ

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Missing default constructor in principal.Cliente I can say,

public class Cliente{ 
  public Cliente(){}
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...