> Mykola Ostapchuk wrote:
>
> >Hello,
> >
> >I'm having problem updating records in 1-m relationship.
> >I have 2 tables - Users and UsersRoles. 1 User can have 1-m UserRoles.
When
> >I delete user - it automatically deletes all the UserRoles - so it works
> >well. But when I update - as I understand, it should first delete all
> >UsersRoles and then insert new ones. Now it doesn't delete - only
inserts
> >new UserRoles.
> >Should I delete all UsersRoles by myself before updating User or there's
> >something wrong with my configuration/files?
> >
> >repository.xml:
> >
> ><class-descriptor class="com.name.wos.users.UsersVO" table="users">
> > <field-descriptor id="1" name="userId" column="US_id" jdbc-type="BIGINT"
> >primarykey="true" autoincrement="true"/>
> > <field-descriptor id="2" name="username" column="US_username"
> >jdbc-type="VARCHAR"/>
> > <field-descriptor id="3" name="password" column="US_password"
> >jdbc-type="VARCHAR"/>
> > <collection-descriptor name ="userUR"
> >element-class-ref="com.name.wos.users.UsersRolesVO" auto-retrieve="true"
> >auto-update="true" auto-delete="true">
> > <inverse-foreignkey field-id-ref="2"/>
> > </collection-descriptor>
> ></class-descriptor>
> >
> ><class-descriptor class="com.name.wos.users.UsersRolesVO"
> >table="usersroles">
> > <field-descriptor id="1" name="urId" column="UR_id" jdbc-type="BIGINT"
> >primarykey="true" autoincrement="true"/>
> > <field-descriptor id="2" name="urUSIdFK" column="UR_US_id_FK"
> >jdbc-type="BIGINT"/>
> > <field-descriptor id="3" name="urRLIdFK" column="UR_RL_id_FK"
> >jdbc-type="BIGINT"/>
> ></class-descriptor>
> >
> >
> >update DAO:
> >
> > public void update(ValueObject updateRecord) throws DataAccessException
{
> > PersistenceBroker broker = null;
> > try {
> > UsersVO usersVO = (UsersVO) updateRecord;
> > broker = ServiceLocator.getInstance().findBroker();
> > broker.beginTransaction();
> > broker.store(usersVO);
> > broker.commitTransaction();
> > } catch (PersistenceBrokerException e) {
> > broker.abortTransaction();
> > e.printStackTrace();
> > throw new DataAccessException("Error in UsersDAO.update()",
e);
> > } catch (ServiceLocatorException e) {
> > throw new DataAccessException("ServiceLocatorException error
in
> >UsersDAO.update()",e);
> > } finally {
> > if (broker != null) broker.close();
> > }
> > }
> >
> >
> >UsersVO.java:
> >
> >import java.util.*;
> >import com.name.wos.common.ValueObject;
> >
> >public class UsersVO extends ValueObject {
> >
> > private Long userId;
> > private String username;
> > private String password;
> > private Vector userUR = new Vector(); // of type UsersRolesVO
> >
> > public Long getUserId() {
> > return userId;
> > }
> > public void setUserId(Long userId) {
> > this.userId = userId;
> > }
> >
> > public String getUsername() {
> > return username;
> > }
> > public void setUsername(String username) {
> > this.username = username;
> > }
> >
> > public String getPassword() {
> > return password;
> > }
> > public void setPassword(String password) {
> > this.password = password;
> > }
> >
> > public Vector getUserUR() {
> > return userUR;
> > }
> > public void setUserUR(Vector userUR) {
> > this.userUR=userUR;
> > }
> >
> >}
> >
> >
> I would try to do all the modifications to the userUR - Collection that
> is given to you by the system because it is a 'RemovalAwareCollection'.
> this would be my setUserUR method:
>
> public void setUserUR(Vector userUR) {
> this.userUR.clear();
> this.userUR.addAll(userUR);
> }
> and then persist the whole thing.
>
> Matthias Laube.
>
In setter method I'm sending as a parameter Vector of new objects of type
UsersRoles - so If you want to clear all existing UsersRoles - you can't
based on new Vector. The only way I see it - to return a Vector of existing
UserRoles based on UserID - and delete these objects. Then - insert new
ones. But I'll better do it in DAO class, not in VO's setter. What do you
think?
I'm really wondering if (or why) it isn't done automatically by OJB? It's so
common situation. I think this routine must be accomplished by OJB.
Regards,
Mykola Ostapchuk
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]