• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Channaveer Biradar
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Print the exception's stack trace. This will give you a clue what is going wrong.
 
Channaveer Biradar
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 661
Eclipse IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Channaveer Biradar
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3913
9
Chrome Fedora Hibernate
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Channaveer Biradar,

Please UseCodeTags when posting code in the forums.

Best thing is to check with the API:executeUpdate()
 
surbhi bhasin
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am facing the similar issue. Were you able to resolve it?
If yes, can you please tell me the solution?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic