title: OpenJPA Transaction Exception
body: <p>I am trying to persist members of a class, BorderPoint. After persisting a certain number of objects, openJPA throws the following exception:</p>
  
  <p>[ERROR]  org.apache.openjpa.persistence.ArgumentException: Attempt to assign id "0" to new instance "org.jason.mapmaker.shared.model.BorderPoint@3134a2" failed; there is already an object in the L1 cache with this id. You must delete this object (in a previous transaction or the current one) before reusing its id.  This error can also occur when a horizontally or vertically mapped classes uses auto-increment application identity and does not use a hierarchy of application identity classes.</p>
  
  <p>The method to persist the BorderPoints accepts a List of BorderPoint objects and attempts to break the persist into batches. Here is the code:</p>
  
  <pre><code>public void persistList(List&lt;BorderPoint&gt; objectList) throws RepositoryException {
  
      EntityManager em = entityManagerFactory.createEntityManager();
  
      try {
          em.getTransaction().begin();
          int i = 1;
          for (BorderPoint bp : objectList) {
              em.persist(bp);
              if (i % 100 == 0) {
                  em.flush();
                  em.clear();
              }
              i++;
          }
          em.getTransaction().commit();
      } catch (EntityExistsException ex) {
          // need to log this somehow
          //log.warning("persist() threw EntityExistsException: " + ex.getMessage());
          ex.printStackTrace();
          throw new RepositoryException(ex);
      }
      catch (Exception e) {
          e.printStackTrace();
      } finally {
          em.close();
      }
  }
  </code></pre>
  
  <p>Just for completeness, here is the calling method:</p>
  
  <pre><code>/**
   * Cycle through and save the borderpoints saved in the Shapefile's geometry
   *
   * @param featureCollection
   * @throws ServiceException
   */
  private void saveBorderPoints(FeatureCollection featureCollection) throws ServiceException {
  
      FeatureIterator iterator = featureCollection.features();
      while (iterator.hasNext()) {
  
          SimpleFeatureImpl feature = (SimpleFeatureImpl) iterator.next();
  
          String geoId = (String) feature.getAttribute("GEOID10");
  
          Location location = locationRepository.getByGeoId(geoId);
          if (location == null) {
              throw new ServiceException("saveBorderPoints() threw ServiceException due to null Location");
          }
          MultiPolygon multiPolygon = (MultiPolygon) feature.getDefaultGeometry();
          Geometry geometry = multiPolygon.getBoundary();
  
          // Create the result list. Set initial capacity size to number of actual points in the geometry to avoid some
          // overhead when dealing with the list
          List&lt;BorderPoint&gt; borderPointList = new ArrayList&lt;BorderPoint&gt;(geometry.getNumPoints());
  
          // cycle through the coordinates to create the border points
          Coordinate[] coordinates = geometry.getCoordinates();
          for (Coordinate c : coordinates) {
              BorderPoint borderPoint = new BorderPoint();
              borderPoint.setLocation(location);
              borderPoint.setLng(new BigDecimal(c.x));
              borderPoint.setLat(new BigDecimal(c.y));
  
              borderPointList.add(borderPoint);
          }
  
          try {
              borderPointRepository.persistList(borderPointList);
          } catch (RepositoryException e) {
              throw new ServiceException("saveBorderPoints() threw RepositoryException", e);
          }
      }
  
      iterator.close();
  }
  </code></pre>
  
  <p>Any ideas out there?</p>
  
  <p>EDIT:</p>
  
  <p>Here is the BorderPoint class, as requested: </p>
  
  <pre><code>@Entity
  @Table(name = "BORDERPOINT")
  public class BorderPoint implements Serializable {
  
  private Long id;
  private Location location;
  private BigDecimal lat;
  private BigDecimal lng;
  
  @Id
  @GeneratedValue(strategy = GenerationType.IDENTITY)
  public Long getId() {
      return id;
  }
  
  public void setId(Long id) {
      this.id = id;
  }
  
  @Column(name="LAT")
  public BigDecimal getLat() {
      return lat;
  }
  
  public void setLat(BigDecimal lat) {
      this.lat = lat;
  }
  
  @Column(name="LNG")
  public BigDecimal getLng() {
      return lng;
  }
  
  public void setLng(BigDecimal lng) {
      this.lng = lng;
  }
  
  @ManyToOne(cascade = CascadeType.ALL, targetEntity = Location.class)
  @JoinColumn(name="LOCATIONID")
  public Location getLocation() {
      return location;
  }
  
  public void setLocation(Location location) {
      this.location = location;
  }
  </code></pre>
  
  <p>}</p>