hibernate加入了子类重复键

时间:2014-03-20 10:31:35

标签: hibernate joined-subclass

谢谢你的好奇心,我需要你的帮助:)。

因此,我创建了一个联接继承:父类被称为' Action',子类被称为' AwardAction'和' LevelUpAction'。我用hibernate注释和3个表创建了3个类。它工作好几个月,但最近出现了问题。

问题是我们发现了两个条目&Level; LevelUpAction'和' AwardAction'具有相同ID,与“行动”中的一个条目相关!
     [行动id = 502]
    / \
   / \
[AwardAction id = 502] [LevelUpAction id = 502]

这种情况不应该发生。每个levelUpAction和AwardAction都应该有自己的id ...

我们检查代码,但没有发现任何问题。实际上在这种情况下不应该创建AwardAction。我们认为已经创建了LevelUpAction条目(如预期的那样),并且hibernate(由于未知原因)也创建了AwardAction条目。

其实我想对你的表和课程有所了解。

类:

@Entity
@Inheritance(strategy = InheritanceType.JOINED)
public abstract class Action extends AbstractKeyEntity {

@Column
@NotEmpty
private String userKey;

@Column
@NotNull
private int points = 0;

protected Action() {
}

protected Action(String userKey, int points) {
    super();
    this.userKey = userKey;
    this.points = points;
}

public int getPoints() {
    return points;
}

public String getUserKey(){
    return userKey;
}
}

@Entity
public class AwardAction extends Action {
@ForeignKey(name = "fk_achievement_award_action_to_award")
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "badge_id")
@NotNull
private Badge badge;

public Badge getBadge() {
    return badge;
}

AwardAction() {

}

AwardAction(String userKey, int points, Badge badge) {
    super(userKey, points);
    this.badge = badge;
}
}

@Entity
public class LevelUpAction extends Action {

@ForeignKey(name = "fk_achievement_level_up_action_achievement_level")
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "achievement_level_id")
@NotNull
private AchievementLevel achievementLevel;

@Column(columnDefinition = "BIT", length = 1)
@NotNull
private boolean fast = false;

@Column(columnDefinition = "BIT", length = 1)
@NotNull
private boolean rich = false;

@Column
private Long fastDuration = 0L;

@Column
private Long richPoints = 0L;

public LevelUpAction() {

}

public LevelUpAction(String userKey, int points, AchievementLevel achievementLevel) {
    super(userKey, points);
    this.achievementLevel = achievementLevel;
}

public boolean isFast() {
    return fast;
}

public void setFast(boolean fast) {
    this.fast = fast;
}

public boolean isRich() {
    return rich;
}

public void setRich(boolean rich) {
    this.rich = rich;
}

public Long getFastDuration() {
    return fastDuration;
}

public void setFastDuration(Long fastDuration) {
    this.fastDuration = fastDuration;
}

public Long getRichPoints() {
    return richPoints;
}

public void setRichPoints(Long richPoints) {
    this.richPoints = richPoints;
}

}

表:

CREATE TABLE `level_up_action` (
`fast` tinyint(1) NOT NULL,
`fast_duration` bigint(20) DEFAULT NULL,
`rich` tinyint(1) NOT NULL,
`rich_points` bigint(20) DEFAULT NULL,
`id` bigint(20) NOT NULL,
`achievement_level_id` bigint(20) NOT NULL,
PRIMARY KEY (`id`),
KEY `fk_achievement_level_up_action_achievement_level` (`achievement_level_id`),
KEY `FK_achievement_level_up_action_to_achievement_action` (`id`),
CONSTRAINT `fk_achievement_level_up_action_achievement_level` FOREIGN KEY     (`achievement_level_id`) REFERENCES `achievement_level` (`id`),
CONSTRAINT `FK_achievement_level_up_action_to_action` FOREIGN KEY (`id`)     REFERENCES `action` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin;

CREATE TABLE `action` (
  `id` bigint(20) NOT NULL AUTO_INCREMENT,
  `created_at` datetime NOT NULL,
  `created_by` varchar(255) NOT NULL,
  `entity_key` varchar(255) NOT NULL,
  `modified_at` datetime NOT NULL,
  `modified_by` varchar(255) NOT NULL,
  `rs_deleted` bit(1) NOT NULL,
  `version` int(11) NOT NULL,
  `points` int(11) NOT NULL,
  `user_key` varchar(255) NOT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `UK_action_entity_key` (`entity_key`)
) ENGINE=InnoDB AUTO_INCREMENT=507 DEFAULT CHARSET=utf8;

CREATE TABLE `award_action` (
  `id` bigint(20) NOT NULL,
  `badge_id` bigint(20) NOT NULL,
  PRIMARY KEY (`id`),
  KEY `fk_award_action_to_badge` (`badge_id`),
  KEY `FK_award_action_to_achievement_action` (`id`),
  CONSTRAINT `fk_award_action_to_badge` FOREIGN KEY (`badge_id`) REFERENCES `badge` (`id`),
  CONSTRAINT `FK_award_action_to_action` FOREIGN KEY (`id`)         REFERENCES `action` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin;

0 个答案:

没有答案