-->
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.  [ 12 posts ] 
Author Message
 Post subject: Help in deletion
PostPosted: Mon Apr 24, 2006 2:47 am 
Newbie

Joined: Tue Apr 11, 2006 9:06 am
Posts: 10
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:


Top
 Profile  
 
 Post subject:
PostPosted: Mon Apr 24, 2006 9:33 am 
Beginner
Beginner

Joined: Mon Mar 27, 2006 5:25 am
Posts: 39
Location: India Hyderabad
private void createAndStoreEvent(int id)
{

Session session = HibernateUtil.getSessionFactory().openSession();

session.beginTransaction();

Event theEvent=new Event();
theEvent.setId(id);
List result=session.createQuery("from events.Event as event where [b]event.EVENT_ID= "+theEvent.getId()).list();
if(result!=null){
theEvent = (Event)result.get(0);
}[/b]


session.delete(theEvent);

session.getTransaction().commit();
}


Top
 Profile  
 
 Post subject:
PostPosted: Tue Apr 25, 2006 2:03 am 
Newbie

Joined: Tue Apr 11, 2006 9:06 am
Posts: 10
Hi vinaykumar,

Thanks for ur response.I want to know the need of putting these ( ,[i] ). When use this, it produce an error like this!

ERROR is :
---------
Illigal start of Expression!


-Thanks
-Cicilin.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Apr 25, 2006 2:07 am 
Newbie

Joined: Tue Apr 11, 2006 9:06 am
Posts: 10
Hi vinaykumar,

Thanks for ur response.I want to know the need of putting these ( ,[i] ). When use this, it produce an error like this!

ERROR is :
---------
Illigal start of Expression!


-Thanks
-Cicilin.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Apr 25, 2006 4:54 am 
Beginner
Beginner

Joined: Mon Mar 27, 2006 5:25 am
Posts: 39
Location: India Hyderabad
could u explain me little bit clear wen u r getting that error


Top
 Profile  
 
 Post subject:
PostPosted: Tue Apr 25, 2006 5:47 am 
Newbie

Joined: Tue Apr 11, 2006 9:06 am
Posts: 10
Hi,

The Error is:

compile:
[javac] Compiling 3 source files to C:\hiberdel\bin
[javac] C:\hiberdel\src\events\EventManager.java:70: illegal start of expression
[javac] [/b]
[javac] ^
[javac] 1 error

Can u give the reason.

-Thanks.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Apr 25, 2006 5:49 am 
Newbie

Joined: Tue Apr 11, 2006 9:06 am
Posts: 10
Hi,

The Error is:

compile:
[javac] Compiling 3 source files to C:\hiberdel\bin
[javac] C:\hiberdel\src\events\EventManager.java:70: illegal start of expression
[javac] [/b]
[javac] ^
[javac] 1 error

Can u give the reason.

-Thanks.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Apr 25, 2006 5:59 am 
Beginner
Beginner

Joined: Mon Mar 27, 2006 5:25 am
Posts: 39
Location: India Hyderabad
Heay Friend,

Its a problem with hibernate.try this code.


private void createAndStoreEvent(int id)
{

Session session = HibernateUtil.getSessionFactory().openSession();

session.beginTransaction();

Event theEvent=new Event();
theEvent.setId(id);
List result=session.createQuery("from events.Event as event where event.EVENT_ID= "+theEvent.getId()).list();
if(result!=null){
theEvent = (Event)result.get(0);
}

session.delete(theEvent);

session.getTransaction().commit();
}


Top
 Profile  
 
 Post subject:
PostPosted: Tue Apr 25, 2006 6:28 am 
Newbie

Joined: Tue Apr 11, 2006 9:06 am
Posts: 10
Hi,

The prog become error free. But the all records in the entity deleted. The specified id has no hand on deletion.I want to delete only one record which i specify the id .Any further modification?

-Thanks


Top
 Profile  
 
 Post subject:
PostPosted: Tue Apr 25, 2006 6:30 am 
Newbie

Joined: Tue Apr 11, 2006 9:06 am
Posts: 10
Hi,

The prog become error free. But the all records in the entity deleted. The specified id has no hand on deletion.I want to delete only one record which i specify the id .Any further modification?

-Thanks


Top
 Profile  
 
 Post subject:
PostPosted: Tue Apr 25, 2006 7:30 am 
Beginner
Beginner

Joined: Mon Mar 27, 2006 5:25 am
Posts: 39
Location: India Hyderabad
private void createAndStoreEvent(int id)
{

Session session = HibernateUtil.getSessionFactory().openSession();

session.beginTransaction();


Event theEvent=session.get(Event.class,new Integer("0"));
session.delete(theEvent);


session.getTransaction().commit();
}


Top
 Profile  
 
 Post subject:
PostPosted: Tue Apr 25, 2006 8:26 am 
Newbie

Joined: Tue Apr 11, 2006 9:06 am
Posts: 10
Hi,

I tried many different types of coding combinations.I don't know what is the reason for all records deletion.
This code has also produce the same result .(All record's deletion)

private void createAndStoreEvent(int id)
{

Session session = HibernateUtil.getSessionFactory().openSession();

session.beginTransaction();


Event theEvent=session.get(Event.class,new Integer("0"));
session.delete(theEvent);


session.getTransaction().commit();
}

The issue is by any other reasons?


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 12 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.