0

I am trying to persist my java object to Mysql database. But i am facing the following error.

ClientDemo.java
package com.infybank;
import java.util.Scanner;
import org.hibernate.HibernateException;
public class ClientDemo {
     public static void main(String args[])
     {
         CustomerDAO custdao=new CustomerDAO();
         try
           {
             System.out.println("CREATE");
             System.out.println("Enter the customer details");
             Scanner sc=new Scanner(System.in);
             System.out.println("Enter the customer number");
             int d=sc.nextInt();
             System.out.println("Enter the customer name");
             String name=sc.next();
             Customer cust=new Customer(d,name);
             custdao.addCustomer(cust);
             System.out.println("One recort inserted");     
    }
    catch(HibernateException ex)
    {

        System.out.println("Exception "+ex);
    }
}}`

HibernateUtil.java package com.infybank;

 import org.hibernate.SessionFactory;
 import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
 import org.hibernate.cfg.Configuration;
 import org.hibernate.service.ServiceRegistry;

 public class HibernateUtil {

private static final SessionFactory sessionFactory;
static
{
    try
    {
        Configuration configuration =new Configuration().configure("/config/hibernate.cfg.xml");
        ServiceRegistry serviceRegistry=new StandardServiceRegistryBuilder()
                .applySettings(configuration.getProperties()).build();
        sessionFactory=configuration.buildSessionFactory(serviceRegistry);

    }
    catch(Throwable ex)
    {
        System.err.println("Initial sessionFactory creation failed"+ex);
        throw new ExceptionInInitializerError(ex);

    }
}
public static SessionFactory getSessionFactory()
{
    return sessionFactory;
}}

This is hibernate.cfg.xml file

 <?xml version="1.0" encoding="UTF-8"?>
 <!DOCTYPE hibernate-configuration PUBLIC
    "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
    "http://hibernate.org/dtd/hibernate-configuration-3.0.dtd">
 <hibernate-configuration>
 <session-factory>
    <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
    <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/hibernatedb</property>
    <property name="hibernate.connection.username">root</property>
    <property name="hibernate.connection.password">Anak@1604</property>
    <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
    <mapping class="com.infybank.Customer"/>
 </session-factory>
 </hibernate-configuration>

Customer.java

package com.infybank;

import javax.persistence.Column;    
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name="customer")
public class Customer {
@Id
@Column(name="CUSTOMERID")
private int customerId;
@Column(name="CUSTOMERNAME")
private String customerName;


public Customer(int id,String name)
{
    this.customerId=id;
    this.customerName=name;
}


public int getCustomerId() {
    return customerId;
}


public void setCustomerId(int customerId) {
    this.customerId = customerId;
}


public String getCustomerName() {
    return customerName;
}


public void setCustomerName(String customerName) {
    this.customerName = customerName;
}
}

CustomerDAO.java

package com.infybank;

import org.hibernate.Session;
import org.hibernate.Transaction;

public class CustomerDAO implements ICustomer{

@Override
public void addCustomer(Customer cust) {
    // TODO Auto-generated method stub
    Session session=HibernateUtil.getSessionFactory().openSession();
    Transaction tx=session.beginTransaction();
    session.save(cust);
    tx.commit();
    session.close();

}
}

Error Exception org.hibernate.MappingException: Unknown entity: com.infybank.Customer

Facing the above error when tried to run the code

Aarthi Ananth
  • 460
  • 1
  • 4
  • 16

2 Answers2

0

At least Hibernate entities should not have this kind of constructor with arguments:

public Customer(int id,String name)
{
    this.customerId=id;
    this.customerName=name;
}

See this answer for more information.

haba713
  • 2,465
  • 1
  • 24
  • 45
0

By default all entity classes should have a default constructor. This is required by Hibernate due to the fact that they are instantiated via reflection.

I believe you just need to add an empty constructor to your Customer class and it should be fine

public Customer(){}
Brenin
  • 195
  • 1
  • 2
  • 16