-->
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.  [ 4 posts ] 
Author Message
 Post subject: Getting Error For update while using Hibernate with Trinidad
PostPosted: Tue Sep 11, 2007 1:09 am 
Regular
Regular

Joined: Mon Apr 02, 2007 3:54 am
Posts: 67
Location: Hyderabad
Hi All,

I had tried the update functionality like this.
public boolean editFunction(final ServiceBean service) {
getHibernateTemplate().execute(new HibernateCallback() {
public Object doInHibernate(Session session) {
int Id = service.getID();
String name = service.getName();

return session.createQuery("UPDATE Bean SET name = :name WHERE ID = :Id")
.setString("name", name)
.setInteger("Id", Id)
.executeUpdate();
}
});
}

upto Hibernate and Spring Layer, This is working fine but when I am tryi ng to execute the same thing with trinidad for view Layer purpose. I am getting following error.

org.springframework.orm.hibernate3.HibernateQueryException: query must begin with SELECT or FROM: UPDATE [UPDATE com.persistence.Bean SET name = :name WHERE ID = :Id]; nested exception is org.hibernate.QueryException: query must begin with SELECT or FROM: UPDATE [UPDATE com.persistence.Bean SET name = :name WHERE ID = :Id]

But If I am trying the same thing with another method like this. I am getting result with trinidad also. method is :

public boolean editFunction(ServiceBean service) {
Bean b = new Bean();

service.getEvalFormID());
b.setName(service.getName());
b.setCreateDate(Calendar.getInstance());
b.setUpdateDate(Calendar.getInstance());
b.setID((Integer)service.getID());
getHibernateTemplate().update(b);
return true;
}

Here I am getting result.
But the main problem is.
Here I don't want the createDate and updateDate to be set. If I am not setting these, I am getting the following exception....

Caused by: java.sql.SQLException: Cannot insert the value NULL into column 'CREATE_DATE', table 'DBName.dbo.tableName'; column does not allow nulls. INSERT fails.

I am using SQl Server 2005 for DB. getDate() function is set as default function for getting the system date and time as current date in the DB. This createDate field is of type DateTime in DB. but I am getting above mentioned error.
I tried the same field by changing its type to String and putting a default value as some String for that. but the error remains same.


Here 2 more fields are also in table called createBy and updateBy. when ever I am doing a update like this. These field are taking default value set in DB while I am not setting these values.

This update query is getting all fields updated except primary key and have primary key in where condition.

1. Now I want to update only few fields on the basis of a field except primary key. How will I do that ?


Can AnyOne have a idea about this ?

Thanks,
Gopal


Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 11, 2007 4:40 am 
Expert
Expert

Joined: Tue Jan 30, 2007 12:45 am
Posts: 283
Location: India
Hi Gopal,

For Dynamic Update use
dynamic-update ="true" and select-before-update="true" on class tag

_________________
Dharmendra Pandey


Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 11, 2007 6:54 am 
Regular
Regular

Joined: Mon Apr 02, 2007 3:54 am
Posts: 67
Location: Hyderabad
dharmendra.pandey wrote:
Hi Gopal,

For Dynamic Update use
dynamic-update ="true" and select-before-update="true" on class tag


Hi Dharmendra,

I tried that. Now I am getting this exception :
Hibernate: select evalformbe_.ID, evalformbe_.NAME as EVALUATION2_14_, evalformbe_.DESC as EVALUATION3_14_, evalformbe_.ALIAS as EVALUATION4_14_, evalformbe_.STATUS as STATUS14_, evalformbe_.CREATE_BY as CREATE6_14_, evalformbe_.CREATE_DATE as CREATE7_14_, evalformbe_.UPDATE_BY as UPDATE8_14_, evalformbe_.UPDATE_DATE as UPDATE9_14_ from evalform evalformbe_ where evalformbe_.ID=?
Hibernate: update evalform set NAME=?, CREATE_DATE=?, UPDATE_DATE=? where ID=?
Exception in thread "main" org.springframework.dao.DataIntegrityViolationException: could not update: [com.persistence.Bean#107]; nested exception is org.hibernate.exception.ConstraintViolationException: could not update: [com.persistence.Bean#107]
Caused by: org.hibernate.exception.ConstraintViolationException: could not update: [com.persistence.Bean#107]
at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:71)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
at org.hibernate.persister.entity.AbstractEntityPersister.update(AbstractEntityPersister.java:2425)
at org.hibernate.persister.entity.AbstractEntityPersister.updateOrInsert(AbstractEntityPersister.java:2307)
at org.hibernate.persister.entity.AbstractEntityPersister.update(AbstractEntityPersister.java:2607)
at org.hibernate.action.EntityUpdateAction.execute(EntityUpdateAction.java:92)
at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:250)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:234)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:142)
at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:298)
at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:27)
at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1000)
at org.springframework.orm.hibernate3.HibernateAccessor.flushIfNecessary(HibernateAccessor.java:390)
at org.springframework.orm.hibernate3.HibernateTemplate.execute(HibernateTemplate.java:373)
at org.springframework.orm.hibernate3.HibernateTemplate.update(HibernateTemplate.java:654)
at org.springframework.orm.hibernate3.HibernateTemplate.update(HibernateTemplate.java:650)
at com.knoahsoft.daosupportimpl.formbuilder.EvalFormDaoDataImpl.editEvalFormData(EvalFormDaoDataImpl.java:302)
at com.knoahsoft.serviceimpl.formbuilder.EvalFormDetailServiceImpl.editEvalFormService(EvalFormDetailServiceImpl.java:114)
at com.knoahsoft.util.JavaMainClass.main(JavaMainClass.java:264)
Caused by: java.sql.SQLException: Cannot insert the value NULL into column 'CREATE_DATE', table 'DBName.dbo.tableName'; column does not allow nulls. UPDATE fails.
at net.sourceforge.jtds.jdbc.SQLDiagnostic.addDiagnostic(SQLDiagnostic.java:365)
at net.sourceforge.jtds.jdbc.TdsCore.tdsErrorToken(TdsCore.java:2781)
at net.sourceforge.jtds.jdbc.TdsCore.nextToken(TdsCore.java:2224)
at net.sourceforge.jtds.jdbc.TdsCore.getMoreResults(TdsCore.java:628)
at net.sourceforge.jtds.jdbc.JtdsStatement.processResults(JtdsStatement.java:525)
at net.sourceforge.jtds.jdbc.JtdsStatement.executeSQL(JtdsStatement.java:487)
at net.sourceforge.jtds.jdbc.JtdsPreparedStatement.executeUpdate(JtdsPreparedStatement.java:421)
at org.hibernate.jdbc.NonBatchingBatcher.addToBatch(NonBatchingBatcher.java:23)
at org.hibernate.persister.entity.AbstractEntityPersister.update(AbstractEntityPersister.java:2403)
... 16 more


I am not setting createDate and updateDate fields and in update query why do I am getting these fields ? I am setting only

name
desc
status
id

why do I am not getting these fields in update query ?
please look for this problem ASAP.

Thanks,
Gopal


Top
 Profile  
 
 Post subject:
PostPosted: Wed Sep 12, 2007 12:44 am 
Regular
Regular

Joined: Mon Apr 02, 2007 3:54 am
Posts: 67
Location: Hyderabad
Hi Dharmendra,

In this case, one update query is generated which is having its primary key i.e. Id of the table in where condition.
Now I want name and status in where condition and on the basis of these I want to update desc. or I want to update all rows without having a where condition. How will I do that ?

Can AnyOne help me for this problem ?

Thanks,
Gopal


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