how to persist an object twice into two diff. collections of two diff. classes

#1

ok my problem, 

 

i persist a Squad Object into a Cup Object at the beginning, after i have to create and persist a competition object in which i have to put some of the already created/persisted squads. the final method looks like that:

public void actionPerformed(ActionEvent e) {
    List<Squad> selectedSquads = new ArrayList<Squad>();
        if (tableSquads.getSelectedRows().length > 0) {
             int[] rows = tableSquads.getSelectedRows();
             DBConnection db = new DBConnection();
             Competition tmpComp = db.getEm().find(Competition.class, actualCompetition.getId());
             for (int i : rows) {
                  int modelRow = tableSquads.convertRowIndexToModel(i);
                  Squad tmpSquad = gymCupController.getGymCup().getUnallocatedSquads().get(modelRow);
                  db.persist(tmpSquad);
                  tmpComp.addSquad(tmpSquad);
                  selectedSquads.add(tmpSquad);
                  gymCupController.getGymCup().getUnallocatedSquads().remove(tmpSquad);
                  actualCompetition.addSquad(tmpSquad);
                  db.persist(tmpComp);
             }
             db.commit();
             db.closeConnection();        

             squadSelectionFrame.dispose();
        }

}

 

This code will throw this exception:

Exception in thread "AWT-EventQueue-0" [ObjectDB 2.2.0] javax.persistence.RollbackException
Failed to commit transaction: Attempt to reuse an existing primary key value (ch.hsr.gymtastic.domain.Squad:1) (error 613)
    at com.objectdb.jpa.EMImpl.commit(EMImpl.java:259)
    at ch.hsr.gymtastic.technicalServices.database.DBConnection.commit(DBConnection.java:46)
     at ch.hsr.gymtastic.server.presentation.frames.SquadsSelectionFrame$2.actionPerformed(SquadsSelectionFrame.java:106)

so how can i trick the DB?

 

 

i apreciate ur ideas!!!!¨

#2

Just delete the db.persist(tmpComp); line.

No need to call persist in order to update an already managed entity object.

See the Updating Entity Objects page in the manual and this forum thread.

ObjectDB Support

Reply