Hi,
I have a class that that looks like below:
Code:
public class Users {
public long userId;
public String username;
public String password;
public String realname;
public String email;
public String homepage;
public Date lastLogin;
public String country;
//with the normal getter and setter methods
private static Configuration getConfig() throws HibernateException {
Configuration config = new Configuration();
return config.configure();
}
private static Session getSession() throws HibernateException {
Configuration config = getConfig();
SessionFactory sessionFactory = null;
Session session = null;
sessionFactory = config.buildSessionFactory();
session = sessionFactory.openSession();
System.out.println("Session value in getSession()" + session.toString());
return session;
}
}
//and an add method
public void add() {
Session session = null;
try {
session = getSession();
} catch (HibernateException he) {
he.printStackTrace();
}
try {
session.save(this);
session.flush();
session.connection().commit();
} catch (Exception e) {
e.printStackTrace();
try {
session.connection().rollback();
} catch (Exception e1) {
e1.printStackTrace();
}
}
}
public static void main(String [] args ) {
Users u = new Users();
u.setUsername("nilesh");
u.setPassword("parmar");
u.setRealname("Nilesh");
u.setEmail("nilesh@pramati.com");
u.setHomepage("localhost");
u.setCountry("counteryName");
u.setEmail("nilesh@pramati.com");
Date today = new Date(System.currentTimeMillis());
u.setLastLogin(today);
u.add();
}
Here is the hibernate mapping:
Code:
<hibernate-mapping>
<class name="org.weblog.user.Users" table="weblog_users">
<id name = "userId" type ="int" column="userId">
<generator class = "increment" />
</id>
<property name = "username" type ="string" column ="username" length="40"/>
<property name = "password" type ="string" column ="password" length="30"/>
<property name = "realname" type ="string" column ="realname" length="80"/>
<property name = "email" type ="string" column ="email" length="50"/>
<property name = "homepage" type ="string" column ="homePage" length="80"/>
<property name = "lastLogin" type ="date" column ="lasgLogin" />
<property name = "country" type ="string" column ="country" length="80"/>
</class>
</hibernate-mapping>
When i run the above code i get the following exception:
Code:
Hibernate: insert into weblog_users (username, password, realname, email, homePage, lasgLogin, country, userId) values (?, ?, ?, ?, ?, ?, ?, ?)
java.lang.ClassCastException
at net.sf.hibernate.type.IntegerType.set(IntegerType.java:27)
at net.sf.hibernate.type.NullableType.nullSafeSet(NullableType.java:46)
at net.sf.hibernate.type.NullableType.nullSafeSet(NullableType.java:31)
at net.sf.hibernate.persister.EntityPersister.dehydrate(EntityPersister.java:377)
at net.sf.hibernate.persister.EntityPersister.insert(EntityPersister.java:476)
at net.sf.hibernate.persister.EntityPersister.insert(EntityPersister.java:454)
at net.sf.hibernate.impl.ScheduledInsertion.execute(ScheduledInsertion.java:20)
at net.sf.hibernate.impl.SessionImpl.executeAll(SessionImpl.java:2100)
at net.sf.hibernate.impl.SessionImpl.execute(SessionImpl.java:2061)
at net.sf.hibernate.impl.SessionImpl.flush(SessionImpl.java:2005)
at org.weblog.user.Users.add(Users.java:129)
at org.weblog.user.Users.main(Users.java:323)
Can anyone please help me ? I'm not sure if my mappings are correct.