JPA:外键,多个主键和多对一关系的问题

时间:2012-02-01 17:05:53

标签: java hibernate java-ee jpa toplink

我正在开发Java / J2EE Web应用程序。数据的持久性由JPA / TopLink完成。我对这个实体有一个问题:

    @Entity
@Table(name = "articlecatalogue_has_article", catalog = "artisance", schema = "public")
@NamedQueries({@NamedQuery(name = "ArticlecatalogueHasArticle.findAll", query = "SELECT a FROM ArticlecatalogueHasArticle a"), @NamedQuery(name = "ArticlecatalogueHasArticle.findByArcIntId", query = "SELECT a FROM ArticlecatalogueHasArticle a WHERE a.articlecatalogueHasArticlePK.arcIntId = :arcIntId"), @NamedQuery(name = "ArticlecatalogueHasArticle.findByArtIntId", query = "SELECT a FROM ArticlecatalogueHasArticle a WHERE a.articlecatalogueHasArticlePK.artIntId = :artIntId"), @NamedQuery(name = "ArticlecatalogueHasArticle.findByAhaDecQuantite", query = "SELECT a FROM ArticlecatalogueHasArticle a WHERE a.ahaDecQuantite = :ahaDecQuantite"), @NamedQuery(name = "ArticlecatalogueHasArticle.findByAhaDecPrixvente", query = "SELECT a FROM ArticlecatalogueHasArticle a WHERE a.ahaDecPrixvente = :ahaDecPrixvente")})
public class ArticlecatalogueHasArticle implements Serializable {
    private static final long serialVersionUID = 1L;
    @EmbeddedId
    protected ArticlecatalogueHasArticlePK articlecatalogueHasArticlePK;
    @Column(name = "aha_dec_quantite")
    private BigDecimal ahaDecQuantite;
    @Column(name = "aha_dec_prixvente")
    private BigDecimal ahaDecPrixvente;
    @JoinColumn(name = "art_int_id", referencedColumnName = "art_int_id", insertable = false, updatable = false)
    @ManyToOne(optional = false, fetch = FetchType.LAZY)
    private Article article;
    @JoinColumn(name = "arc_int_id", referencedColumnName = "arc_int_id", insertable = false, updatable = false)
    @ManyToOne(optional = false, fetch = FetchType.LAZY)
    private Articlecatalogue articlecatalogue;

多个主键:

@Embeddable
public class ArticlecatalogueHasArticlePK implements Serializable {
    @Basic(optional = false)
    @Column(name = "arc_int_id")
    private int arcIntId;
    @Basic(optional = false)
    @Column(name = "art_int_id")
    private int artIntId;

当我尝试制作一个ArticlecatalogueHasArticle实体时,我有以下错误:

Local Exception Stack: 
Exception [TOPLINK-4002] (Oracle TopLink Essentials - 2.1 (Build b60e-fcs (12/23/2008))): oracle.toplink.essentials.exceptions.DatabaseException
Internal Exception: org.postgresql.util.PSQLException: ERREUR: une valeur NULL viole la contrainte NOT NULL de la colonne « arc_int_id »
Error Code: 0
Call: INSERT INTO artisance.public.articlecatalogue_has_article (aha_dec_prixvente, aha_dec_quantite, art_int_id, arc_int_id) VALUES (?, ?, ?, ?)
    bind => [null, 1, null, null]

而在我想要持久化的实体中,字段arcIntId和artIntId不为空。我认为问题是由于ArticlecatalogueHasArticlePK和ArticlecatalogueHasArticle @JoinColumn中的列“art_int_id”和“arc_int_id”的双重实例,但我不确定,我不知道如何解决问题。

感谢任何帮助。

2 个答案:

答案 0 :(得分:0)

您必须在两个ManyToOne关联上使用@MapsId注释。

答案 1 :(得分:0)

确保您在articlecatalogueHasArticlePK中设置id值,如果您没有设置它们,那么它们将为null。

您还可以删除articlecatalogueHasArticlePK,并使用IdClass而不是EmbeddedId。然后你只需要在你的ManyToOne上放置@Id,不需要任何重复。

如果你想从关系中选择id,你也可以在基础知识上输入可插入/可更新的false,在ManyToOne上输入true。

请参阅, http://en.wikibooks.org/wiki/Java_Persistence/Identity_and_Sequencing#Primary_Keys_through_OneToOne_and_ManyToOne_Relationships