在关系表中加载数据时出现JPA StackOverflowError

时间:2013-04-12 15:05:57

标签: jpa-2.0 entity-relationship stack-overflow foreign-key-relationship

我正在尝试在三个表(the beginning of the problem with mapping

中实现加载

Products

@Entity
@Table(name = "products")
public class Product implements Serializable {
    @Id
    @Column(name = "id")
    private Integer id;

    @OneToMany(mappedBy = "property", fetch = FetchType.LAZY)
    private Collection<ProductProperty> productPropertyCollection;

    ...
}

Properties

@Entity
@Table(name = "properties")
public class Property implements Serializable {
    @Id
    @Column(name = "id")
    private Integer id;

    @OneToMany(mappedBy = "property", fetch = FetchType.LAZY)
    private Collection<ProductProperty> productPropertyCollection;

    ...
}

Product_Property

@Entity
@Table(name = "product_property")
public class ProductProperty implements Serializable {
    @EmbeddedId
    protected ProductPropertyPK productPropertyPK;

    @MapsId(value = "propertyId")
    @JoinColumn(name = "property_id", referencedColumnName = "id")
    @ManyToOne()    
    private Property property;

    @MapsId(value = "productId")
    @JoinColumn(name = "product_id", referencedColumnName = "id")
    @ManyToOne()
    private Product product;

    ...
}

@Embeddable
public class ProductPropertyPK implements Serializable {
    @Basic(optional = false)
    @NotNull
    @Column(name = "product_id", insertable = false, updatable = false)
    private int productId;
    @Basic(optional = false)
    @NotNull
    @Column(name = "property_id", insertable = false, updatable = false)
    private int propertyId;
    ...
}

它适用于1,10,100个产品,但某处有错误,因为1000个以上的产品会抛出错误:

Caused by: java.lang.StackOverflowError
    at java.util.HashMap.getEntry(HashMap.java:443)
    at java.util.HashMap.containsKey(HashMap.java:434)
    at java.util.HashSet.contains(HashSet.java:201)
    at org.eclipse.persistence.internal.sessions.UnitOfWorkImpl.discoverAndPersistUnregisteredNewObjects(UnitOfWorkImpl.java:4141)
    at org.eclipse.persistence.mappings.ObjectReferenceMapping.cascadeDiscoverAndPersistUnregisteredNewObjects(ObjectReferenceMapping.java:938)
    at org.eclipse.persistence.mappings.ObjectReferenceMapping.cascadeDiscoverAndPersistUnregisteredNewObjects(ObjectReferenceMapping.java:916)
    at org.eclipse.persistence.internal.descriptors.ObjectBuilder.cascadeDiscoverAndPersistUnregisteredNewObjects(ObjectBuilder.java:1964)
    at org.eclipse.persistence.internal.sessions.UnitOfWorkImpl.discoverAndPersistUnregisteredNewObjects(UnitOfWorkImpl.java:4178)
    at org.eclipse.persistence.mappings.CollectionMapping.cascadeDiscoverAndPersistUnregisteredNewObjects(CollectionMapping.java:426)
    at org.eclipse.persistence.internal.descriptors.ObjectBuilder.cascadeDiscoverAndPersistUnregisteredNewObjects(ObjectBuilder.java:1964)
    at org.eclipse.persistence.internal.sessions.UnitOfWorkImpl.discoverAndPersistUnregisteredNewObjects(UnitOfWorkImpl.java:4178)
    at org.eclipse.persistence.mappings.ObjectReferenceMapping.cascadeDiscoverAndPersistUnregisteredNewObjects(ObjectReferenceMapping.java:938)
    at org.eclipse.persistence.mappings.ObjectReferenceMapping.cascadeDiscoverAndPersistUnregisteredNewObjects(ObjectReferenceMapping.java:916)
    at org.eclipse.persistence.internal.descriptors.ObjectBuilder.cascadeDiscoverAndPersistUnregisteredNewObjects(ObjectBuilder.java:1964)
    at org.eclipse.persistence.internal.sessions.UnitOfWorkImpl.discoverAndPersistUnregisteredNewObjects(UnitOfWorkImpl.java:4178)
    at org.eclipse.persistence.mappings.CollectionMapping.cascadeDiscoverAndPersistUnregisteredNewObjects(CollectionMapping.java:426)
    at org.eclipse.persistence.internal.descriptors.ObjectBuilder.cascadeDiscoverAndPersistUnregisteredNewObjects(ObjectBuilder.java:1964)
    at org.eclipse.persistence.internal.sessions.UnitOfWorkImpl.discoverAndPersistUnregisteredNewObjects(UnitOfWorkImpl.java:4178)
    at org.eclipse.persistence.mappings.ObjectReferenceMapping.cascadeDiscoverAndPersistUnregisteredNewObjects(ObjectReferenceMapping.java:938)
    at org.eclipse.persistence.mappings.ObjectReferenceMapping.cascadeDiscoverAndPersistUnregisteredNewObjects(ObjectReferenceMapping.java:916)
    at org.eclipse.persistence.internal.descriptors.ObjectBuilder.cascadeDiscoverAndPersistUnregisteredNewObjects(ObjectBuilder.java:1964)
    at org.eclipse.persistence.internal.sessions.UnitOfWorkImpl.discoverAndPersistUnregisteredNewObjects(UnitOfWorkImpl.java:4178)
...

当我创建ProductProperty时,我在product中设置propertyProductProperty,并在ProductProperty中添加到双向收集集

我可能犯错误?

1 个答案:

答案 0 :(得分:1)

看起来您的对象模型复杂性或深度很难在JVM的堆栈限制内遍历。实际上,每个实体似乎都可以从每个其他实体到达,这会在递归遍历时导致问题。尝试增加-Xss设置。您还可能会降低互连性,例如删除其中一个OneToMany映射并直接查询,而不是将其存储在Product或Property映射中。您也可以使用EclipseLink提交增强功能,以使用堆栈而不是递归遍历对象图。