aspose file tools*
The moose likes Object Relational Mapping and the fly likes delete query in hibernate giving error as null, and not deleting the record Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "delete query in hibernate giving error as null, and not deleting the record" Watch "delete query in hibernate giving error as null, and not deleting the record" New topic
Author

delete query in hibernate giving error as null, and not deleting the record

Channaveer Biradar
Greenhorn

Joined: Jan 05, 2011
Posts: 5
Hi,
below is my FirstExample.java file and Contact.hbm.xml file
when run FirstExample.java it is giving error as null, and not deleting the record

FirstExample.java file


import java.sql.Date;



import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.Query;
import utils.Insurence;



public class FirstExample {
public static void main(String[] args) {
Session session = null;

try{

SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
session =sessionFactory.openSession();
String hql = "delete from Book CONTACT1 where id = 2";
Query query = (Query) session.createQuery(hql);
int row = query.executeUpate();
if (row == 0){
System.out.println("Doesn't deleted any row!");
}
else
{
System.out.println("DeletedRow: " + row);
}

}catch(Exception e){
System.out.println("error=="+e.getMessage());
}finally{
session.flush();
session.close();

}

}
}

Contact.hbm.xml file

<?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="utils.Book" table="CONTACT1">
<id name="lngBookId" type="long" column="ID">
<generator class="increment"/>
</id>

<property name="strBookName">
<column name="Bookname"/>
</property>
</class>
</hibernate-mapping>

output

log4j:WARN No appenders could be found for logger (org.hibernate.cfg.Environment).
log4j:WARN Please initialize the log4j system properly.
error==null

please any help is helpfull , i dont know why it is coming
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

Print the exception's stack trace. This will give you a clue what is going wrong.


JavaRanch FAQ HowToAskQuestionsOnJavaRanch
Channaveer Biradar
Greenhorn

Joined: Jan 05, 2011
Posts: 5
Hi,
on stacktrace it is showing Nullpointerexception on
query.excuteupate()

i think it is returning null, but why it is so, is anything wrong with the return query

please help me.
Arun Kumarr
Ranch Hand

Joined: May 16, 2005
Posts: 513

Channaveer Biradar wrote:
int row = query.executeUpate();


Assuming you've copy pasted the code, isn't it executeUpdate()?

Also, can you post the entire stack trace. Query can't be null. may be something is wrong in the executeUpdate method.


If you are not laughing at yourself, then you just didn't get the joke.
Channaveer Biradar
Greenhorn

Joined: Jan 05, 2011
Posts: 5
Hi,
i am practising the hibernate, so refering rose india and some of the code i copied .
below is the stack trace

log4j:WARN No appenders could be found for logger (org.hibernate.cfg.Environment).
log4j:WARN Please initialize the log4j system properly.
java.lang.NullPointerException
at org.hibernate.hql.antlr.HqlSqlBaseWalker.path(HqlSqlBaseWalker.java:2193)
at org.hibernate.hql.antlr.HqlSqlBaseWalker.fromElement(HqlSqlBaseWalker.java:2281)
at org.hibernate.hql.antlr.HqlSqlBaseWalker.fromElementList(HqlSqlBaseWalker.java:2232)
at org.hibernate.hql.antlr.HqlSqlBaseWalker.fromClause(HqlSqlBaseWalker.java:498)
at org.hibernate.hql.antlr.HqlSqlBaseWalker.deleteStatement(HqlSqlBaseWalker.java:276)
at org.hibernate.hql.antlr.HqlSqlBaseWalker.statement(HqlSqlBaseWalker.java:156)
at org.hibernate.hql.ast.QueryTranslatorImpl.analyze(QueryTranslatorImpl.java:189)
at org.hibernate.hql.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:130)
at org.hibernate.hql.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:83)
at org.hibernate.impl.SessionFactoryImpl.getQuery(SessionFactoryImpl.java:414)
at org.hibernate.impl.SessionImpl.getQueries(SessionImpl.java:814)
at org.hibernate.impl.SessionImpl.executeUpdate(SessionImpl.java:801)
at org.hibernate.impl.QueryImpl.executeUpate(QueryImpl.java:89)
at isoft.FirstExample.main(FirstExample.java:58)


but the thing is if i change that query.executeupate() in my code to query.excuteUpdate() to rose india code it is showing the error that method query.executeupdate() is undefined for the query and solution for that is change it to query.executeupate()

thanks in advance for the help
Vijitha Kumara
Bartender

Joined: Mar 24, 2008
Posts: 3855

Hi Channaveer Biradar,

Please UseCodeTags when posting code in the forums.

Best thing is to check with the API:executeUpdate()


SCJP 5 | SCWCD 5
[How to ask questions] [Twitter]
surbhi bhasin
Greenhorn

Joined: May 19, 2011
Posts: 1
I am facing the similar issue. Were you able to resolve it?
If yes, can you please tell me the solution?
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: delete query in hibernate giving error as null, and not deleting the record