-1

美好的一天,请告知我为什么会出现以下异常。我是 EclipseLink 初学者,我使用的是 jdk1.7.0_05,MySQL 服务器 5.5.25a。

Internal Exception: com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Cannot add or update a child row: a foreign key constraint fails (`stats`.`version`, CONSTRAINT `fk_version_activity1` FOREIGN KEY (`activity_id`, `activity_license_id`) REFERENCES `activity` (`id`, `license_id`) ON DELETE NO ACTION ON UPDATE NO ACTION)
Error Code: 1452
Caused by: javax.persistence.PersistenceException: Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.3.0.v20110604-r9504): org.eclipse.persistence.exceptions.DatabaseException
Internal Exception: com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Cannot add or update a child row: a foreign key constraint fails (`stats`.`version`, CONSTRAINT `fk_version_activity1` FOREIGN KEY (`activity_id`, `activity_license_id`) REFERENCES `activity` (`id`, `license_id`) ON DELETE NO ACTION ON UPDATE NO ACTION)
Error Code: 1452
Call: INSERT INTO version (version, activity_id, product, activity_license_id) VALUES (?, ?, ?, ?)
bind => [4 parameters bound]
Query: InsertObjectQuery(cz.ryvo.stats.database.Version[ versionPK=cz.ryvo.stats.database.VersionPK[ product=AP, activityId=0, activityLicenseId=0 ] ])
at org.eclipse.persistence.internal.jpa.EntityManagerImpl.flush(EntityManagerImpl.java:786)
at cz.audatex.audaupdateloader.DatabaseHelper.registerActivity(DatabaseHelper.java:104)
... 3 more

表许可证如下所示:

CREATE TABLE `license` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `license` varchar(20) NOT NULL,
  `organisation_id` int(11) DEFAULT NULL,
  `user_id` int(11) DEFAULT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `license_UNIQUE` (`license`),
  KEY `fk_license_user1` (`user_id`),
  KEY `fk_license_organisation1` (`organisation_id`),
  CONSTRAINT `fk_license_organisation1` FOREIGN KEY (`organisation_id`) REFERENCES `organisation` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION,
  CONSTRAINT `fk_license_user1` FOREIGN KEY (`user_id`) REFERENCES `user` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION
) ENGINE=InnoDB AUTO_INCREMENT=7 DEFAULT CHARSET=latin1;

表活动:

CREATE TABLE `activity` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `license_id` int(11) NOT NULL,
  `time` datetime NOT NULL,
  `type` char(1) NOT NULL,
  `result` int(1) DEFAULT NULL,
  PRIMARY KEY (`id`,`license_id`),
  UNIQUE KEY `activity_UQ01` (`license_id`,`time`,`type`),
  KEY `fk_activity_license1` (`license_id`),
  CONSTRAINT `fk_activity_license1` FOREIGN KEY (`license_id`) REFERENCES `license` (`id`) ON DELETE CASCADE ON UPDATE NO ACTION
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=latin1;

和表版本:

CREATE TABLE `version` (
  `product` varchar(45) NOT NULL,
  `activity_id` int(11) NOT NULL,
  `activity_license_id` int(11) NOT NULL,
  `version` varchar(45) DEFAULT NULL,
  PRIMARY KEY (`product`,`activity_id`,`activity_license_id`),
  KEY `fk_version_activity1` (`activity_id`,`activity_license_id`),
  CONSTRAINT `fk_version_activity1` FOREIGN KEY (`activity_id`, `activity_license_id`) REFERENCES `activity` (`id`, `license_id`) ON DELETE NO ACTION ON UPDATE NO ACTION
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

我使用数据库中的 New->Entity Classes 在 NetBeans 中生成的类如下所示:

班级版本:

@Entity
@Table(name = "version")
@XmlRootElement
public class Version implements Serializable {
    private static final long serialVersionUID = 1L;
    @EmbeddedId
    protected VersionPK versionPK;
    @Column(name = "version")
    private String version;
    @JoinColumns({
        @JoinColumn(name = "activity_id", referencedColumnName = "id", insertable = false, updatable = false),
        @JoinColumn(name = "activity_license_id", referencedColumnName = "license_id", insertable = false, updatable = false)})
    @ManyToOne(optional = false, fetch = FetchType.LAZY)
    private Activity activity;
    ...

类版本PK:

@Embeddable
public class VersionPK implements Serializable {
    @Basic(optional = false)
    @Column(name = "product")
    private String product;
    @Basic(optional = false)
    @Column(name = "activity_id")
    private int activityId;
    @Basic(optional = false)
    @Column(name = "activity_license_id")
    private int activityLicenseId;
    ...

当我执行以下代码时,我遇到了异常:

...
ActivityPK apk = new ActivityPK();
apk.setLicenseId(activity.getLicense().getId());
activity.setActivityPK(apk);
em.persist(activity);
em.flush(); <- Here the exception is thrown
...

我怀疑这是因为活动尚未分配 ID,而 VerionPK 使用此 ID。我对吗?持久化此类数据的正确方法是什么?在将版本集合设置为 null 的持久化活动之后,我应该单独持久化版本集合吗?提前谢谢了。沃杰科技

能效比图

4

1 回答 1

1

查看 JPA 2.0 的派生 ID。您可以将关系标记为 ID,或者使用映射 Id 指定关系 id 字段,而不必自己手动设置值。这可以在创建新树并且根实体使用排序时解决问题 - 在根被持久化之前,子实体无法将 id 用作外键。

否则,需要对根实体进行持久化和刷新,以便分配 Id。在这种情况下,Activity 实体需要主键值,然后您才能尝试保存引用它的新版本对象,因为您必须手动设置所有 version.versionpk 值。

于 2012-09-11T18:31:47.850 回答