1

我正在尝试在三个表中实现加载(映射问题的开始

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 时,我正在设置productand propertyin ,并添加到集合以在andProductProperty中进行双向。ProductProperty

我在哪里会犯错?

4

1 回答 1

1

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

于 2013-04-12T18:16:34.360 回答