我正在开发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;
当我试图使ArticlecatalogyHasArticle实体持久化时,我有以下错误:
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在我想要持久化的实体中不为null。我认为问题是由于ArticlecatalogyHasArticlePK和Articlecatalog HasArticle@JoinColumn中的列"art_int_id"one_answers"arc_int_id"的双重实例造成的,但我不确定,也不知道如何解决问题。
感谢您的帮助。
确保您正在articlecatalogueHasArticlePK中设置id值,如果您不设置它们,那么它们将为null。
您也可以删除articlecatalogueHasArticlePK,并使用IdClass而不是EmbeddedId。然后你只需要把@Id放在你的ManyToOne上,不需要任何重复。
如果你想从关系中提取id,你也可以在基础上设置insertable/updateable false,在ManyToOne上设置true。
参见,http://en.wikibooks.org/wiki/Java_Persistence/Identity_and_Sequencing#Primary_Keys_through_OneToOne_and_ManyToOne_Relationships
必须在两个ManyToOne关联上使用@MapsId
注释。