Hi,
I want a simple help.In my prog, I want to delete specified record/records by using id.When i execute the prog. using given coding all records will become deleted.
My Question is that, How can i write the Query to delete specified records?
-Thanks.
Hibernate version:
Hibernate 3.1
Mapping documents:
Event.hbm.xml
-----------------------
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="events.Event" table="EVENTS">
<id name="id" column="EVENT_ID">
<generator class="native"/>
</id>
<property name="date" type="timestamp" column="EVENT_DATE"/>
<property name="title" column="EVENT_TITLE"/>
</class>
</hibernate-mapping>
Code between sessionFactory.openSession() and session.close():
EventManager.java
--------------------------
package events;
import org.hibernate.Session;
import java.util.Date;
import java.util.*;
import util.HibernateUtil;
public class EventManager
{
public static void main(String[] args)
{
EventManager mgr = new EventManager();
if (args[0].equals("delete"))
{
mgr.createAndStoreEvent(1);
}
HibernateUtil.getSessionFactory().close();
}
private void createAndStoreEvent(int id)
{
Session session = HibernateUtil.getSessionFactory().openSession();
session.beginTransaction();
Event theEvent=new Event();
theEvent.setId(id);
List result=session.createQuery("from Event as event where event.EVENT_ID= "+theEvent.getId()).list();
theEvent = (Event)result.get(1);
session.delete(theEvent);
session.getTransaction().commit();
}
}
Full stack trace of any exception that occurs:
Name and version of the database you are using:
mySql
The generated SQL (show_sql=true):
Debug level Hibernate log excerpt:
|