我想实现如下映射。我有一些产品和文章。一个产品关联了许多文章。此外,这种关系具有特殊属性,例如返回码。所以关联表应该是这样的。
Product_FK | Article_FK | return code
1 | 1 | 0
1 | 1 | 1
1 | 2 | 1
目前,我通过两个协会意识到了这一点。“一(产品)对多(关系表)”和“多(关系表)对一(文章)”关联。
这些实体被注释为休闲:
产品实体:
@Entity(name = "product")
@Table(name = "PRODUCT")
public class ProductDTO implements BaseEntityDTO {
private static final long serialVersionUID = -6613561719098236228L;
@Id
@Column(name = "PRODUCT_PK")
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "product_pk_seq")
@SequenceGenerator(name = "product_pk_seq", sequenceName = "product_pk_seq", allocationSize = 1)
protected Long pk;
@OneToMany(cascade = CascadeType.ALL, mappedBy="product")
private List<ProductArticleRelationDTO> product2article = new ArrayList<ProductArticleRelationDTO>();
...
}
ProductArticleRelation 实体:
@Entity(name = "productArticleRelation")
@Table(name = "PRODUCT_2_ARTICLE")
public class ProductArticleRelationDTO implements BaseEntityDTO {
private static final long serialVersionUID = -1134854397447263839L;
@Id
@Column(name = "PRODUCT_2_ARTICLE_PK")
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "product2article_pk_seq")
@SequenceGenerator(name = "product2article_pk_seq", sequenceName = "product2artikle_pk_seq", allocationSize = 1)
protected Long pk;
@Override
public Long getPk() {
return pk;
}
@Override
public void setPk(long pk) {
this.pk = Long.valueOf(pk);
}
@ManyToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "PRODUCT_FK")
private ProductDTO product = null;
@ManyToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "ARTICLE_FK")
private ArticleDTO article = null;
@Column
private int returnCode;
...
}
文章实体:
@Entity(name = "article")
@Table(name = "ARTICLE")
public class ArticleDTO implements BaseEntityDTO {
private static final long serialVersionUID = 4397348044985703865L;
@Id
@Column(name = "ARTICLE_PK")
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "article_pk_seq")
@SequenceGenerator(name = "article_pk_seq", sequenceName = "article_pk_seq", allocationSize = 1)
protected Long pk;
@Override
public Long getPk() {
return pk;
}
@Override
public void setPk(long pk) {
this.pk = Long.valueOf(pk);
}
@OneToMany(cascade = CascadeType.ALL, mappedBy = "article")
private final List<ProductArticleRelationDTO> product2article = new ArrayList<ProductArticleRelationDTO>();
...
}
我很确定注释是错误的。如果我想保留一个添加了一些 productArticleRelations 的产品,我会得到一个唯一的约束违规。
for(ProductArticleRelationDTO relation : newProduct2Article) {
product.addProduct2Article(relation);
if(!em.contains(product))
if(product.getPK() == null)
em.persist(product);
else
product = em.merge(product);
else
em.flush();
例外:
Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.3.2.v20111125-r10461): org.eclipse.persistence.exceptions.DatabaseException
Internal Exception: java.sql.SQLIntegrityConstraintViolationException: ORA-00001: Unique Constraint (BC7_BPV.PK_ARTICLE) violated
Error Code: 1
Query: InsertObjectQuery(bc7.bpv.common.dto.ProductDTO@38f2f)
我不太确定的是,如果我需要 productArticleRelation 实体。这意味着是否可以通过一个关联“一个(产品)到多个(文章)”和一个具有“返回代码”作为属性的特殊连接表来处理这种类型?
编辑:我忘了,这种关系应该是双向的。
我在数据库中的表是休闲的(为了简化只显示重要的列):
产品:
| PRODUCT_PK | ... |
文章:
| ARTICLE_PK | ... |
PRODUCT_2_文章:
| PRODUCT_2_ARTICLE_PK | PRODUCT_FK | ARTICLE_FK | RETURNCODE