-->
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.  [ 7 posts ] 
Author Message
 Post subject: ..I cannot Delete from MySQL using Hibernate :(
PostPosted: Wed Sep 14, 2005 1:16 am 
Beginner
Beginner

Joined: Fri Jun 10, 2005 11:51 pm
Posts: 45
..i cannot execute multiple delete using hibernate

Hibernate version: 3
here is my codes: from JSP page to get the check values:

String deleteArray[] = request.getParameterValues("chkor");
String selected="";
if(deleteArray.length == 1) {
// Only one item is selected
selected = deleteArray[0];

} else {

// multiple items selected
for (int i = 0; i < deleteArray.length; i++) {

selected += deleteArray[i];

if( i < deleteArray.length -1) {
selected += ",";
}
}

}
System.out.println("DELETE FROM itemOreder WHERE itemId IN ("+selected+")");
sp.deleteR("ItemOrder",selected);
..and here my method to execute the query..

public void deleteR(String table,String selected)
{

Session session = null;
Transaction tx = null;
Logger log = Logger.getLogger("Delete Items");
log.info("deleteR()");

String qry = "DELETE FROM "+table+" WHERE itemId IN ("+selected+")";
System.out.println(qry);
try {

// get the session from the factory
session = HibernateSessionFactory.currentSession();

// always start a transaction before doing something
// (even reading) from the database
tx = session.beginTransaction();
session.createQuery(qry).executeUpate();


// commit your transaction or nothing is wrote to the db
tx.commit();

// clean up (close the session)
session.close();

} catch (HibernateException e) {
// when an error occured, try to rollback your transaction
if (tx != null)
try {
tx.rollback();
} catch (HibernateException e1)
{
log.warn("rollback not successful");
}

//* close your session after an exception! Your session
//* is in an undefined and unstable situation so throw it away!
if (session != null)
try {
session.close();
} catch (HibernateException e2)
{
log.warn("session close not successful");
}
}

}

------------here is the error--
16344 [http-8070-Processor25] ERROR hql.parser - *** ERROR: <AST>:0:0: unexpected end of subtree
16344 [http-8070-Processor25] ERROR hql.parser - *** ERROR: <AST>:0:0: unexpected end of subtree


Top
 Profile  
 
 Post subject:
PostPosted: Wed Sep 14, 2005 1:46 am 
Pro
Pro

Joined: Fri Sep 02, 2005 4:21 am
Posts: 206
Location: Vienna
As your problem is not obvious (at least not to me) simply by reading the code, could you elaborate on what you exactly mean by
jack182 wrote:
..i cannot execute multiple delete using hibernate

Do you receive an exception? Is only the first delete successful?

Or (provocative) do multiple deletes blow up your PC? ;-)

So please provide any stack trace, any traced SQL statements that you have, and perhaps also the mapping of ItemOrder as well as the Class itself.

Never forget: the quality of the support you get is directly proportional to the quality of the information you provide...

Erik


Top
 Profile  
 
 Post subject: multiple delete...
PostPosted: Wed Sep 14, 2005 3:48 am 
Beginner
Beginner

Joined: Fri Jun 10, 2005 11:51 pm
Posts: 45
i mean i have a several checkbox in my jsp page then i check 3 checkbox to delete it from database entry...my problem is how to delete this check items from the database...eg. DELETE FROM table WHERE itemId IN (11,10,14) to delete these record from the database...that statement send an error state that:
-----stack trace---------

16344 [http-8070-Processor25] ERROR hql.parser - *** ERROR: <AST>:0:0: unexpected end of subtree
16344 [http-8070-Processor25] ERROR hql.parser - *** ERROR: <AST>:0:0: unexpected end of subtree

---the mapping for this is here----------------

<hibernate-mapping package="spider">
<class name="ItemOrder" table="itemorder">
<id column="Id" name="orderNum" type="java.lang.Integer">
<generator class="increment"/>
</id>

<property column="OrderDate" name="orderDate" type="java.lang.String"/>
<property column="ItemId" name="itemId" type="java.lang.Integer"/>
<property column="UserId" name="userId" type="java.lang.Integer"/>

</class>
</hibernate-mapping>

--------the class-----

public class ItemOrder {
private int orderNum;
private int userId;
private String orderDate;
private int itemId;

/**
* @return Returns the userId.
*/
public int getUserId() {
return userId;
}
/**
* @param userId The userId to set.
*/
public void setUserId(int userId) {
this.userId = userId;
}
/**
* @return Returns the itemId.
*/
public int getItemId() {
return itemId;
}
/**
* @return Returns the orderDate.
*/
public String getOrderDate() {
return orderDate;
}
/**
* @return Returns the orderNum.
*/
public int getOrderNum() {
return orderNum;
}

/**
* @param itemId The itemId to set.
*/
public void setItemId(int itemId) {
this.itemId = itemId;
}
/**
* @param orderDate The orderDate to set.
*/
public void setOrderDate(String orderDate) {
this.orderDate = orderDate;
}
/**
* @param orderNum The orderNum to set.
*/
public void setOrderNum(int orderNum) {
this.orderNum = orderNum;
}
}


Top
 Profile  
 
 Post subject:
PostPosted: Wed Sep 14, 2005 4:24 am 
Pro
Pro

Joined: Fri Sep 02, 2005 4:21 am
Posts: 206
Location: Vienna
I tried your code in a standalone unit test and it works. Does it also fail for you as a standalone test?

I'm using Hibernate 3.0.5.

Erik


Top
 Profile  
 
 Post subject: multiple delete...
PostPosted: Wed Sep 14, 2005 4:35 am 
Beginner
Beginner

Joined: Fri Jun 10, 2005 11:51 pm
Posts: 45
..yes i tried several code like: session.createSQLQuery(qry).executeUpate();
and nothing happens....i just know im using version 3 of hibernate...why my codes are not working in my project while in your testing it is working?what should i do to know my version of hibernate?do i have to download another version of hibernate to get my project working...pls..help..tnx..


Top
 Profile  
 
 Post subject:
PostPosted: Wed Sep 14, 2005 4:43 am 
Pro
Pro

Joined: Fri Sep 02, 2005 4:21 am
Posts: 206
Location: Vienna
You can read the Hibernate version from the manifest file in the hibernate3.jar you are using.

If it is an older version than 3.0.5, download 3.0.5.

Erik


Top
 Profile  
 
 Post subject: ...i try different codes to delete
PostPosted: Wed Sep 14, 2005 11:57 am 
Beginner
Beginner

Joined: Fri Jun 10, 2005 11:51 pm
Posts: 45
ErikFK,tnx buddy for the help and time...i just tried another way to delete from the database using session.delete(Object) ...and i just finished solving my problem..tnx a lot


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