Question

I am working on an Java/J2EE web application. The persistence of data is made by 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 :

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. 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.

Any help is appreciated.

Était-ce utile?

La solution

Ensure you are setting the id values in the articlecatalogueHasArticlePK, if you do not set them, then they will be null.

You could also remove the articlecatalogueHasArticlePK, and use a IdClass instead of an EmbeddedId. Then you only need to put @Id on your ManyToOne, and don't need any duplicates.

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.

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

Autres conseils

You must use the @MapsId annotation on the two ManyToOne associations.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top