JPA:外键、多个主键和多对一关系的问题
我正在开发 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”的双重实例造成的,但我不确定,也不知道如何解决该问题。
任何帮助表示赞赏。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
确保您在articlecatalogueHasArticlePK中设置id值,如果不设置它们,则它们将为空。
您还可以删除articlecatalogueHasArticlePK,并使用IdClass 而不是EmbeddedId。然后你只需要把@Id放在ManyToOne上,不需要任何重复。
如果您想从关系中获取 id,您还可以将 insertable/updateable false 放在基础上,将 true 放在 ManyToOne 上。
看,
http://en.wikibooks.org/wiki/Java_Persistence/Identity_and_Sequencing#Primary_Keys_through_OneToOne_and_ManyToOne_Relationships
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
您必须使用
@MapsId
两个 ManyToOne 关联上的注释。You must use the
@MapsId
annotation on the two ManyToOne associations.