import java.util.Iterator;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
/**
* @author lthomas
*
*/
public class EmployeeM {
public static void main(String[] args) {
Session session = null;
try {
SessionFactory sessionFactory = new Configuration().configure()
.buildSessionFactory();
session = sessionFactory.openSession();
System.out.println("Inserting Record");
Employee[] employee = new Employee[2];
Transaction transaction = session.beginTransaction();
employee[0] = new Employee();
employee[0].setId(1);
employee[0].setFirstName("Ani");
employee[0].setLastName("Jacob");
employee[0].setEmail("ani@yahoo.com");
employee[1] = new Employee();
employee[1].setId(2);
employee[1].setFirstName("Mini");
employee[1].setLastName("Thomas");
employee[1].setEmail("mini@yahoo.com");
session.save(employee[0]);
session.save(employee[1]);
transaction.commit();
System.out.println("Insertion of Records Done");
} catch (Exception e) {
System.out.println(e.getMessage());
} finally {
session.flush();
}
String SQL_QUERY_EMPLOYEE = "Select emp.id,emp.firstName,emp.lastName,emp.email from Employee emp";
Query query2 = session.createQuery(SQL_QUERY_EMPLOYEE);
for(Iterator it = query2.iterate();it.hasNext();){
Object[] row = (Object[]) it.next();
System.out.println("Employee Id: "+row[0]);
System.out.println("First Name: "+row[1]);
System.out.println("Last Name : "+row[2]);
System.out.println("Email : "+row[3]);
}
Integer count = (Integer) session.createQuery("select count(id) from Employee").uniqueResult();
System.out.println("Employee Id's Count: "+count);
session.close();
}
}
but i'm getting the following error
og4j:WARN No appenders could be found for logger (org.hibernate.cfg.Environment).
log4j:WARN Please initialize the log4j system properly.
Inserting Record
Hibernate: insert into Employee (FIRSTNAME, LASTNAME, EMAIL, ID) values (?, ?, ?, ?)
null
Hibernate: insert into Employee (FIRSTNAME, LASTNAME, EMAIL, ID) values (?, ?, ?, ?)
java.lang.ClassCastException
at org.hibernate.type.LongType.set(LongType.java:40)
at org.hibernate.type.NullableType.nullSafeSet(NullableType.java:62)
at org.hibernate.type.NullableType.nullSafeSet(NullableType.java:44)
at org.hibernate.persister.entity.BasicEntityPersister.dehydrate(BasicEntityPersister.java:1603)
at org.hibernate.persister.entity.BasicEntityPersister.dehydrate(BasicEntityPersister.java:1569)
at org.hibernate.persister.entity.BasicEntityPersister.insert(BasicEntityPersister.java:1820)
at org.hibernate.persister.entity.BasicEntityPersister.insert(BasicEntityPersister.java:2171)
at org.hibernate.action.EntityInsertAction.execute(EntityInsertAction.java:46)
at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:239)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:223)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:136)
at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:274)
at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:27)
at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:669)
at com.icope.applications.hibernate.EmployeeM.main(EmployeeM.java:42)
Exception in thread "main"
regards
|