You are viewing a plain text version of this content. The canonical link for it is here.
Posted to ojb-user@db.apache.org by Info <in...@crestax.ca> on 2004/04/27 06:00:05 UTC
Update Problem
Hello,
I am using following code to update a record:
broker = PersistenceBrokerFactory.defaultPersistenceBroker();
QueryByCriteria query = new QueryByCriteria(department);
DepartmentDto result = (DepartmentDto)
broker.getObjectByQuery(query);
if (result != null){
result.setDescription(department.getDescription());
result.setName(department.getName());
broker.beginTransaction();
broker.store(result);
broker.commitTransaction();
}
but I get Exception:
*org.apache.ojb.broker.PersistenceBrokerSQLException: SQL failure while
insert object data for class com.zousys.silique.dto.DepartmentDto, PK of
the given object is [ departmentId=0], object was
com.zousys.silique.dto.DepartmentDto@6af18d, exception message is
[Invalid argument value, message from server: "Duplicate entry '0' for
key 1"]
*From online document, I read that OJB will check if the PK exist, if
yes, OJB will update the record. Anyone know why I get this error?
Thanks a lot!
Wallace
---------------------------------------------------------------------
To unsubscribe, e-mail: ojb-user-unsubscribe@db.apache.org
For additional commands, e-mail: ojb-user-help@db.apache.org
Re: Update Problem
Posted by Armin Waibel <ar...@apache.org>.
Info wrote:
> Hello,
>
> I am using following code to update a record:
>
> broker = PersistenceBrokerFactory.defaultPersistenceBroker();
> QueryByCriteria query = new QueryByCriteria(department);
> DepartmentDto result = (DepartmentDto)
> broker.getObjectByQuery(query);
> if (result != null){
> result.setDescription(department.getDescription());
> result.setName(department.getName());
> broker.beginTransaction();
> broker.store(result);
> broker.commitTransaction();
> }
> but I get Exception:
>
> *org.apache.ojb.broker.PersistenceBrokerSQLException: SQL failure while
> insert object data for class com.zousys.silique.dto.DepartmentDto, PK of
> the given object is [ departmentId=0], object was
> com.zousys.silique.dto.DepartmentDto@6af18d, exception message is
> [Invalid argument value, message from server: "Duplicate entry '0' for
> key 1"]
>
> *From online document, I read that OJB will check if the PK exist, if
> yes, OJB will update the record. Anyone know why I get this error?
>
Did you set autoincrement="true" in the field-descriptor of all PK
fields in com.zousys.silique.dto.DepartmentDto?
<field-descriptor
name="id"
column="SM_ID"
jdbc-type="INTEGER"
primarykey="true"
autoincrement="true"
/>
As a side-note if possible don't use primitive types for PK and FK
fields to avoid "0"/"null" problems (OJB assume that the primitive 0
represents "null") or never use value 0 for PK or FK fields and start
with values >0 (OJB sequence manager do this by default).
regards,
Armin
> Thanks a lot!
>
> Wallace
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: ojb-user-unsubscribe@db.apache.org
> For additional commands, e-mail: ojb-user-help@db.apache.org
>
>
>
---------------------------------------------------------------------
To unsubscribe, e-mail: ojb-user-unsubscribe@db.apache.org
For additional commands, e-mail: ojb-user-help@db.apache.org