i am making a sample program in hibernate follow this tutorial:
http://www.myeclipseide.com/documentation/quickstarts/hibernateintroduction/
using reverse engineering, i have created this method.
EventsDAO.java:
public Events findById(com.hibernate.EventsId i) {
log.debug("getting Events instance with id: " + i);
try {
Events instance = (Events) getSession().get(
"com.hibernate.Events", i);
return instance;
} catch (RuntimeException re) {
log.error("get failed", re);
throw re;
}
}
and calling from main class makiing this method:
private static void listEvents()
{
EventsDAO dao= new EventsDAO();
Events events= dao.findById(1);
printEvents("Printing User,", events);
dao.getSession().close();
}
it give me error on : Events events= dao.findById(1);
because here, i am passing int. and in EventsDAO.java, its type is EventsID.
please suggest.
as surlac suggest,
now it is giving exception:
Exception in thread “main” org.hibernate.id.IdentifierGenerationException: ids for this class must be manually assigned before calling save(): com.hibernate.Events
private static void addEvents()
{
Events events= new Events();
events.setUid(1);
events.setName("abc");
events.setDuration(123);
EventsDAO dao= new EventsDAO();
Transaction tx=dao.getSession().beginTransaction();
dao.save(events);
tx.commit();
dao.getSession().close();
}
it is giving exception on this line:
dao.save(events);
Here is Events.hbm.xml file: <?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!--
Mapping file autogenerated by MyEclipse Persistence Tools
-->
<hibernate-mapping>
<class name="com.hibernate.Events" table="EVENTS" schema="APP">
<composite-id name="id" class="com.hibernate.EventsId">
<key-property name="uid" type="java.lang.Long">
<column name="UID" />
</key-property>
<key-property name="name" type="java.lang.String">
<column name="NAME" length="20" />
</key-property>
<key-property name="duration" type="java.lang.Integer">
<column name="DURATION" />
</key-property>
</composite-id>
</class>
</hibernate-mapping>
please help.
Try to use Integer as an ID, unless you use composite keys: