OpenJPA的交易异常

我想坚持一个类,BorderPoint的成员。 坚持一定数量的对象后,OpenJPA的抛出以下异常:

[错误] org.apache.openjpa.persistence.ArgumentException:尝试给ID为“0”,以新的实例“org.jason.mapmaker.shared.model.BorderPoint@3134a2”失败; 有已经在L1高速缓存具有该ID的对象。 重用其ID之前,必须删除此对象(在之前的交易或当前的)。 当水平或垂直映射类采用自动递增应用程序标识,并且不使用应用程序标识类的层次结构也可能出现此错误。

坚持在BorderPoints的方法接受BorderPoint对象名单,并试图打破持续到批次。 下面是代码:

public void persistList(List<BorderPoint> 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(); } }

只是为了完整性,这里是调用的方法:

/** * 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<BorderPoint> borderPointList = new ArrayList<BorderPoint>(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(cx)); borderPoint.setLat(new BigDecimal(cy)); borderPointList.add(borderPoint); } try { borderPointRepository.persistList(borderPointList); } catch (RepositoryException e) { throw new ServiceException("saveBorderPoints() threw RepositoryException", e); } } iterator.close(); }

任何想法了吗?

编辑:

这里是BorderPoint类,如要求:

@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; }

}

分类:java的 时间:2015-03-15 人气:0
本文关键词: Java中,OpenJPA的
分享到:

相关文章

Copyright (C) 55228885.com, All Rights Reserved.

55228885 版权所有 京ICP备15002868号

processed in 1.137 (s). 10 q(s)