-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: java.lang.ClassCastException
PostPosted: Tue Apr 17, 2007 1:38 am 
Newbie

Joined: Tue Apr 17, 2007 1:30 am
Posts: 3
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


Top
 Profile  
 
 Post subject:
PostPosted: Tue Apr 17, 2007 9:08 am 
Expert
Expert

Joined: Tue Jul 11, 2006 10:21 am
Posts: 457
Location: Columbus, Ohio
The problem probably lies with:

employee[0].setId(1);
employee[1].setId(2);

Try:

employee[0].setId(1L);
employee[1].setId(2L);

I'm assuming that the ids are Long. If this does not solve the problem, please post your mapping files.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.