【发布时间】:2011-06-04 23:38:53
【问题描述】:
我有 4 张桌子 -- store, catalog_galleries, catalog_images, and catalog_financials。
当我从store --> catalog_galleries --> catalog_images 遍历关系时,换句话说:store.getCatalogGallery().getCatalogImages() 我得到重复的记录。有谁知道这可能是什么原因?有什么建议去哪里看吗?
store 表与catalog_galleries 具有OneToOne 关系,而catalog_galleries 又与catalog_images 具有OneToMany 关系和一个急切的获取类型。 store 表还具有与 catalog_financials 的 OneToMany 关系。
以下是相关实体:
商店实体
@Entity
@Table(name="store")
public class Store {
...
private CatalogGallery gallery;
...
@OneToOne(mappedBy="store")
public CatalogGallery getGallery() {
return gallery;
}
}
目录库实体
@Entity
@Table(name="catalog_galleries")
public class CatalogGallery {
...
private Store store;
private Collection<CatalogImage> catalogImages;
...
@OneToOne
@PrimaryKeyJoinColumn
public Store getStore() {
return store;
}
@OneToMany(mappedBy="catalogGallery", fetch=FetchType.EAGER)
public Collection<CatalogImage> getCatalogImages {
return catalogImages;
}
}
CatalogImage 实体
@Entity
@Table(name="catalog_images")
public class CatalogImage {
...
private CatalogGallery catalogGallery;
...
@ManyToOne
@JoinColumn(name="gallery_id", insertable=false, updatable=false)
public CatalogGallery getCatalogGallery() {
return catalogGallery;
}
}
【问题讨论】:
-
信息不足,请为您的 JPA 类提供映射。您是否尝试过查看生成的 SQL?
-
所有记录都是重复的吗?您是否检查过您的连接列是否有重复条目?
-
是的,所有记录都有重复。在
catalog_images中,join列中有多个具有相同值的条目,即对相关数据/图像进行分组。 -
那么,您在数据库中有重复记录,对吧?另外,为什么在 join 列中需要
insertable=false, updatable=false? -
@axtavt:数据库本身没有重复记录;但是,
catalog_images的列中的一个字段可能具有相同的值,即对字段进行分组。例如,表中的两行类似于 -- 1 1 comp & 2 1 cat5 -- 第一列是 id,第二列是 gallery_id,第三列是名称。 gallery_id 是连接列,某些行可能具有与该列相同的值。至于是否需要insertable=false,updatable=false,我自己也不太确定;当我运行代码时,我看到一条错误消息,表明我需要添加这些规定