简体   繁体   English

JPA:外键,多个主键以及多对一关系

[英]JPA : Issue with foreign keys, multiple primary keys and many to one relation

I am working on an Java/J2EE web application. 我正在研究Java / J2EE Web应用程序。 The persistence of data is made by JPA/TopLink. 数据的持久性由JPA / TopLink进行。 And I have an issue with this entity : 我对此实体有疑问:

    @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;

And the multiple primary keys : 以及多个主键:

@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;

When I try to make persistent an ArticlecatalogueHasArticle entity I have this following error : 当我尝试使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]

Whereas the fields arcIntId and artIntId are not null in the entity which I want to make persistent. 而在我要保留的实体中,字段arcIntId和artIntId不为null。 I think the problem is due to the double instance of the columns "art_int_id" and "arc_int_id" in the ArticlecatalogueHasArticlePK and in the ArticlecatalogueHasArticle @JoinColumn, but I am not sure and I do not know how to solve the problem. 我认为问题是由于ArticlecatalogueHasArticlePK和ArticlecatalogueHasArticle @JoinColumn中列“ art_int_id”和“ arc_int_id”的双重实例引起的,但是我不确定,我也不知道如何解决该问题。

Any help is appreciated. 任何帮助表示赞赏。

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

Ensure you are setting the id values in the articlecatalogueHasArticlePK, if you do not set them, then they will be null. 确保您正在articlecatalogueHasArticlePK中设置id值,如果未设置它们,则它们将为null。

You could also remove the articlecatalogueHasArticlePK, and use a IdClass instead of an EmbeddedId. 您也可以删除articlecatalogueHasArticlePK,并使用IdClass代替EmbeddedId。 Then you only need to put @Id on your ManyToOne, and don't need any duplicates. 然后,您只需要在@ManyToOne上放置@Id,就不需要任何重复项。

You could also put insertable/updateable false on the basics and true on the ManyToOne if you want to pick the ids up from the relationships. 如果您想从关系中获取ID,也可以在基础知识上插入可插入/可更新的false,在ManyToOne上输入true。

See, http://en.wikibooks.org/wiki/Java_Persistence/Identity_and_Sequencing#Primary_Keys_through_OneToOne_and_ManyToOne_Relationships 参见http://en.wikibooks.org/wiki/Java_Persistence/Identity_and_Sequencing#Primary_Keys_through_OneToOne_and_ManyToOne_Relationships

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM