Hi.
When I store object with
session.save(people); It cause an exception
java.lang.ClassCastException
at net.sf.hibernate.type.CharacterType.set(CharacterType.java:32)
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:371)
at net.sf.hibernate.persister.EntityPersister.insert(EntityPersister.java:503)
at net.sf.hibernate.persister.EntityPersister.insert(EntityPersister.java:444)
at net.sf.hibernate.impl.SessionImpl.doSave(SessionImpl.java:717)
at net.sf.hibernate.impl.SessionImpl.save(SessionImpl.java:605)
at persistance.UseHibernate.main(UseHibernate.java:42)
I use hibernate provided connection on mysql and my code an xml are
Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 2.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
<hibernate-mapping>
<class name="persistance.People" table="people">
<id name="id" type="integer" column="peopleid">
<generator class="native"/>
</id>
<property name="name" column="name" type="character" not-null="true"/>
<property name="birthDate" column="birthdate" type="date" not-null="true"/>
</class>
</hibernate-mapping>
Code:
public class UseHibernate {
public static void main(String[] args) throws HibernateException {
Configuration cfg = null;
try {
cfg = new Configuration().addFile("D:/IdeaProjects" +
"/Projects/restaurant/Prototype/hibernate/People.hbm.xml");
} catch (MappingException e) { }
SessionFactory sessionFactory = null;
sessionFactory = cfg.buildSessionFactory();
Session session = null;
session = sessionFactory.openSession();
People people = new People();
people.setName("Paan");
people.setBirthDate(Calendar.getInstance().getTime());
session.save(people);
session.close();
}
}
Code:
public class People {
private int id;
private String name;
private Date birthDate;
private int getId() {
return id;
}
private void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Date getBirthDate() {
return birthDate;
}
public void setBirthDate(Date birthDate) {
this.birthDate = birthDate;
}
}
Please tell me what I done wrong.
Best Regard